mirror of
https://github.com/the-programmers-hangout/Mimic.git
synced 2026-09-07 23:45:29 +02:00
tidy up of config commands
This commit is contained in:
@@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
namespace App\SlashCommands;
|
namespace App\SlashCommands;
|
||||||
|
|
||||||
|
use App\Traits\SlashCommandHelpers;
|
||||||
use Discord\Parts\Interactions\Command\Option;
|
use Discord\Parts\Interactions\Command\Option;
|
||||||
use Discord\Parts\Interactions\Interaction;
|
|
||||||
use Laracord\Commands\SlashCommand;
|
use Laracord\Commands\SlashCommand;
|
||||||
|
|
||||||
class Config extends SlashCommand
|
class Config extends SlashCommand
|
||||||
{
|
{
|
||||||
|
use SlashCommandHelpers;
|
||||||
/**
|
/**
|
||||||
* The command name.
|
* The command name.
|
||||||
*
|
*
|
||||||
@@ -86,26 +87,10 @@ class Config extends SlashCommand
|
|||||||
$key = $this->value('key');
|
$key = $this->value('key');
|
||||||
$value = $this->value('value');
|
$value = $this->value('value');
|
||||||
|
|
||||||
if ($value) {
|
if ($value !== null) {
|
||||||
\App\Models\Config::set($key, $value);
|
return $this->setConfig($interaction, $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()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->viewConfig($interaction, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+61
-68
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
namespace App\SlashCommands;
|
namespace App\SlashCommands;
|
||||||
|
|
||||||
|
use App\Traits\SlashCommandHelpers;
|
||||||
use Discord\Parts\Interactions\Command\Option;
|
use Discord\Parts\Interactions\Command\Option;
|
||||||
use Laracord\Commands\SlashCommand;
|
use Laracord\Commands\SlashCommand;
|
||||||
|
|
||||||
class Mime extends SlashCommand
|
class Mime extends SlashCommand
|
||||||
{
|
{
|
||||||
|
use SlashCommandHelpers;
|
||||||
/**
|
/**
|
||||||
* The command name.
|
* The command name.
|
||||||
*
|
*
|
||||||
@@ -117,78 +119,69 @@ class Mime extends SlashCommand
|
|||||||
{
|
{
|
||||||
$interaction->acknowledge();
|
$interaction->acknowledge();
|
||||||
|
|
||||||
$actions = ['add', 'remove', 'view'];
|
$operation = collect(['add', 'remove', 'view'])
|
||||||
|
->first(fn($action) => $this->value("manage.$action.mime") !== null);
|
||||||
|
|
||||||
$operation = null;
|
if (!$operation) {
|
||||||
|
return $this->replyWithError($interaction, 'Invalid Operation', 'No valid operation provided.');
|
||||||
foreach ($actions as $action) {
|
|
||||||
if ($this->value("manage.$action.mime") !== null) {
|
|
||||||
$operation = $action;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($operation) {
|
$method = 'handle' . ucfirst($operation);
|
||||||
case 'add':
|
|
||||||
$mime = $this->value("manage.add.mime");
|
|
||||||
$handling = $this->value("manage.add.handling");
|
|
||||||
|
|
||||||
$this->console()->log("the handling is $handling for mime $mime");
|
if (method_exists($this, $method)) {
|
||||||
|
return $this->$method($interaction);
|
||||||
\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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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`."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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`."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user