From dc421e7a0fbff26f06d0bddafc9fbf8872228cc9 Mon Sep 17 00:00:00 2001 From: itsAzaria Date: Wed, 18 Feb 2026 19:51:06 +0000 Subject: [PATCH] tidy up of config commands --- app/SlashCommands/Config.php | 27 ++---- app/SlashCommands/Mime.php | 129 ++++++++++++++--------------- app/Traits/SlashCommandHelpers.php | 63 ++++++++++++++ 3 files changed, 130 insertions(+), 89 deletions(-) create mode 100644 app/Traits/SlashCommandHelpers.php diff --git a/app/SlashCommands/Config.php b/app/SlashCommands/Config.php index aeeaf1c..592decc 100644 --- a/app/SlashCommands/Config.php +++ b/app/SlashCommands/Config.php @@ -2,12 +2,13 @@ namespace App\SlashCommands; +use App\Traits\SlashCommandHelpers; use Discord\Parts\Interactions\Command\Option; -use Discord\Parts\Interactions\Interaction; use Laracord\Commands\SlashCommand; class Config extends SlashCommand { + use SlashCommandHelpers; /** * The command name. * @@ -86,26 +87,10 @@ class Config extends SlashCommand $key = $this->value('key'); $value = $this->value('value'); - if ($value) { - \App\Models\Config::set($key, $value); - - $interaction->sendFollowUpMessage( - $this - ->message() - ->title('Config Updated') - ->content("The config key `$key` has been set to `$value`.") - ->build() - ); - } else { - $currentValue = \App\Models\Config::get($key, 'Not set'); - - $interaction->sendFollowUpMessage( - $this - ->message() - ->title('Config Value') - ->content("The current value of the config key `$key` is `$currentValue`.") - ->build() - ); + if ($value !== null) { + return $this->setConfig($interaction, $key, $value); } + + return $this->viewConfig($interaction, $key); } } diff --git a/app/SlashCommands/Mime.php b/app/SlashCommands/Mime.php index f3a3efb..3ddcefe 100644 --- a/app/SlashCommands/Mime.php +++ b/app/SlashCommands/Mime.php @@ -2,11 +2,13 @@ namespace App\SlashCommands; +use App\Traits\SlashCommandHelpers; use Discord\Parts\Interactions\Command\Option; use Laracord\Commands\SlashCommand; class Mime extends SlashCommand { + use SlashCommandHelpers; /** * The command name. * @@ -117,78 +119,69 @@ class Mime extends SlashCommand { $interaction->acknowledge(); - $actions = ['add', 'remove', 'view']; + $operation = collect(['add', 'remove', 'view']) + ->first(fn($action) => $this->value("manage.$action.mime") !== null); - $operation = null; - - foreach ($actions as $action) { - if ($this->value("manage.$action.mime") !== null) { - $operation = $action; - break; - } + if (!$operation) { + return $this->replyWithError($interaction, 'Invalid Operation', 'No valid operation provided.'); } - switch ($operation) { - case 'add': - $mime = $this->value("manage.add.mime"); - $handling = $this->value("manage.add.handling"); + $method = 'handle' . ucfirst($operation); - $this->console()->log("the handling is $handling for mime $mime"); - - \App\Models\Mime::updateOrCreate( - ['mime' => $mime], - ['handling' => $handling] - ); - - $interaction->sendFollowUpMessage( - $this - ->message() - ->title('Mime Rule') - ->content("The handling rule for mime type `$mime` is set to `$handling`.") - ->build() - ); - - break; - case 'remove': - $mime = $this->value("manage.remove.mime"); - - \App\Models\Mime::where('mime', $mime)->delete(); - - $interaction->sendFollowUpMessage( - $this - ->message() - ->title('Mime Rule Removed') - ->content("The handling rule for mime type `$mime` has been removed.") - ->build() - ); - - break; - case 'view': - $mime = $this->value("manage.view.mime"); - - $rule = \App\Models\Mime::where('mime', $mime)->first(); - - if (!$rule) { - $interaction->sendFollowUpMessage( - $this - ->message() - ->title('No Rule Found') - ->content("No rule found for mime type `$mime`.") - ->build() - ); - - } else { - - $interaction->sendFollowUpMessage( - $this - ->message() - ->title('Mime Rule') - ->content("The handling rule for mime type `$mime` is `$rule->handling`.") - ->build() - ); - } - - break; + if (method_exists($this, $method)) { + return $this->$method($interaction); } } + + protected function handleAdd($interaction) + { + $mime = $this->value("manage.add.mime"); + $handling = $this->value("manage.add.handling"); + + \App\Models\Mime::updateOrCreate( + ['mime' => $mime], + ['handling' => $handling] + ); + + return $this->reply( + $interaction, + 'Mime Rule', + "The handling rule for mime type `$mime` is set to `$handling`." + ); + } + + protected function handleRemove($interaction) + { + $mime = $this->value("manage.remove.mime"); + + \App\Models\Mime::where('mime', $mime)->delete(); + + return $this->reply( + $interaction, + 'Mime Rule Removed', + "The handling rule for mime type `$mime` has been removed." + ); + } + + protected function handleView($interaction) + { + $mime = $this->value("manage.view.mime"); + + $rule = \App\Models\Mime::where('mime', $mime)->first(); + + if (!$rule) { + return $this->replyWithError( + $interaction, + 'No Rule Found', + "No rule found for mime type `$mime`." + ); + } + + return $this->reply( + $interaction, + 'Mime Rule', + "The handling rule for mime type `$mime` is `$rule->handling`." + ); + } + } \ No newline at end of file diff --git a/app/Traits/SlashCommandHelpers.php b/app/Traits/SlashCommandHelpers.php new file mode 100644 index 0000000..0c01c8c --- /dev/null +++ b/app/Traits/SlashCommandHelpers.php @@ -0,0 +1,63 @@ +sendFollowUpMessage( + $this->message() + ->title($title) + ->content($content) + ->build() + ); + } + + protected function replyWithError($interaction, string $title, string $content) + { + $interaction->sendFollowUpMessage( + $this->message() + ->title($title) + ->content($content) + ->color(0xFF0000) + ->build() + ); + } + + + protected function setConfig($interaction, string $key, $value) + { + $oldValue = \App\Models\Config::get($key); + + \App\Models\Config::set($key, $value); + + $message = $oldValue === null + ? "The config key `$key` has been set to `$value`." + : "The config key `$key` has been updated from `$oldValue` to `$value`."; + + return $this->reply($interaction, 'Config Updated', $message); + } + + + protected function viewConfig($interaction, string $key) + { + $currentValue = \App\Models\Config::get($key); + + if ($currentValue === null) { + return $this->reply( + $interaction, + 'Config Value', + "The config key `$key` is not set." + ); + } + + return $this->reply( + $interaction, + 'Config Value', + "The current value of the config key `$key` is `$currentValue`." + ); + } +}