tidy up of config commands

This commit is contained in:
itsAzaria
2026-02-18 19:51:06 +00:00
parent f6f1a8f9d4
commit dc421e7a0f
3 changed files with 130 additions and 89 deletions
+6 -21
View File
@@ -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);
}
}
+61 -68
View File
@@ -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`."
);
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
namespace App\Traits;
use Discord\Parts\Interactions\Interaction;
trait SlashCommandHelpers
{
protected function reply($interaction, string $title, string $content)
{
$interaction->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`."
);
}
}