diff --git a/src/Commands/AbstractCommand.php b/src/Commands/AbstractCommand.php index eb967c0..9444bfa 100644 --- a/src/Commands/AbstractCommand.php +++ b/src/Commands/AbstractCommand.php @@ -6,12 +6,13 @@ use Discord\Parts\Interactions\Command\Command; use Discord\Parts\User\User; use Illuminate\Support\Str; +use Laracord\Concerns\HasHandler; use Laracord\Discord\Concerns\HasModal; use Laracord\Laracord; abstract class AbstractCommand { - use HasModal; + use HasHandler, HasModal; /** * The bot instance. @@ -185,7 +186,7 @@ public function getUser($user) throw new Exception('The user model could not be found.'); } - return $this->user = $model::firstOrCreate(['discord_id' => $user->id], [ + return $model::firstOrCreate(['discord_id' => $user->id], [ 'discord_id' => $user->id, 'username' => $user->username, ]) ?? null; diff --git a/src/Commands/ApplicationCommand.php b/src/Commands/ApplicationCommand.php index e0f27ca..f3699a5 100644 --- a/src/Commands/ApplicationCommand.php +++ b/src/Commands/ApplicationCommand.php @@ -2,6 +2,7 @@ namespace Laracord\Commands; +use Discord\Parts\Interactions\Interaction; use Discord\Parts\Permissions\RolePermission; abstract class ApplicationCommand extends AbstractCommand @@ -41,4 +42,16 @@ public function isNsfw(): bool { return $this->nsfw; } + + /** + * Handle the denied command. + */ + public function handleDenied(Interaction $interaction): void + { + $this + ->message('You do not have permission to run this command.') + ->title('Permission Denied') + ->error() + ->reply($interaction, ephemeral: true); + } } diff --git a/src/Commands/Command.php b/src/Commands/Command.php index d21ba39..08b7688 100644 --- a/src/Commands/Command.php +++ b/src/Commands/Command.php @@ -2,6 +2,7 @@ namespace Laracord\Commands; +use Discord\Parts\Channel\Message; use Illuminate\Support\Str; use Laracord\Commands\Contracts\Command as CommandContract; @@ -37,12 +38,8 @@ abstract class Command extends AbstractCommand implements CommandContract /** * Maybe handle the command. - * - * @param \Discord\Parts\Channel\Message $message - * @param array $args - * @return mixed */ - public function maybeHandle($message, $args) + public function maybeHandle(Message $message, array $args): void { if (! $this->canDirectMessage() && ! $message->guild_id) { return; @@ -53,7 +50,10 @@ public function maybeHandle($message, $args) } if (! $this->isAdminCommand()) { - $this->handle($message, $args); + $this->resolveHandler([ + 'message' => $message, + 'args' => $args, + ]); return; } @@ -62,18 +62,12 @@ public function maybeHandle($message, $args) return; } - $this->handle($message, $args); + $this->resolveHandler([ + 'message' => $message, + 'args' => $args, + ]); } - /** - * Handle the command. - * - * @param \Discord\Parts\Channel\Message $message - * @param array $args - * @return mixed - */ - abstract public function handle($message, $args); - /** * Retrieve the command cooldown. * diff --git a/src/Commands/ContextMenu.php b/src/Commands/ContextMenu.php index 37de1fd..685b29d 100644 --- a/src/Commands/ContextMenu.php +++ b/src/Commands/ContextMenu.php @@ -2,10 +2,8 @@ namespace Laracord\Commands; -use Discord\Parts\Channel\Message; use Discord\Parts\Interactions\Command\Command as DiscordCommand; use Discord\Parts\Interactions\Interaction; -use Discord\Parts\User\User; use Laracord\Commands\Contracts\ContextMenu as ContextMenuContract; abstract class ContextMenu extends ApplicationCommand implements ContextMenuContract @@ -33,18 +31,10 @@ public function create(): DiscordCommand return new DiscordCommand($this->discord(), $menu->all()); } - /** - * Handle the context menu interaction. - */ - abstract public function handle(Interaction $interaction, Message|User|null $target): mixed; - /** * Maybe handle the context menu interaction. - * - * @param \Discord\Parts\Interactions\Interaction $interaction - * @return mixed */ - public function maybeHandle($interaction) + public function maybeHandle(Interaction $interaction): void { $target = match ($this->getType()) { DiscordCommand::USER => $interaction->data->resolved->users?->first(), @@ -53,23 +43,24 @@ public function maybeHandle($interaction) }; if (! $this->isAdminCommand()) { - $this->handle($interaction, $target); + $this->resolveHandler([ + 'interaction' => $interaction, + 'target' => $target, + ]); return; } if ($this->isAdminCommand() && ! $this->isAdmin($interaction->member->user)) { - return $interaction->respondWithMessage( - $this - ->message('You do not have permission to run this command.') - ->title('Permission Denied') - ->error() - ->build(), - ephemeral: true - ); + $this->handleDenied($interaction); + + return; } - $this->handle($interaction, $target); + $this->resolveHandler([ + 'interaction' => $interaction, + 'target' => $target, + ]); } /** diff --git a/src/Commands/Contracts/Command.php b/src/Commands/Contracts/Command.php index f07a934..72bf4f4 100644 --- a/src/Commands/Contracts/Command.php +++ b/src/Commands/Contracts/Command.php @@ -2,12 +2,7 @@ namespace Laracord\Commands\Contracts; -use Discord\Parts\Channel\Message; - interface Command { - /** - * Handle the command. - */ - public function handle(Message $message, array $args); + // } diff --git a/src/Commands/Contracts/ContextMenu.php b/src/Commands/Contracts/ContextMenu.php index d45c664..229dd45 100644 --- a/src/Commands/Contracts/ContextMenu.php +++ b/src/Commands/Contracts/ContextMenu.php @@ -2,14 +2,7 @@ namespace Laracord\Commands\Contracts; -use Discord\Parts\Channel\Message; -use Discord\Parts\Interactions\Interaction; -use Discord\Parts\User\User; - interface ContextMenu { - /** - * Handle the context menu interaction. - */ - public function handle(Interaction $interaction, Message|User|null $target): mixed; + // } diff --git a/src/Commands/Contracts/SlashCommand.php b/src/Commands/Contracts/SlashCommand.php index 2796534..6ee422e 100644 --- a/src/Commands/Contracts/SlashCommand.php +++ b/src/Commands/Contracts/SlashCommand.php @@ -2,12 +2,7 @@ namespace Laracord\Commands\Contracts; -use Discord\Parts\Interactions\Interaction; - interface SlashCommand { - /** - * Handle the slash command. - */ - public function handle(Interaction $interaction); + // } diff --git a/src/Commands/SlashCommand.php b/src/Commands/SlashCommand.php index b0c4dc4..56d2091 100644 --- a/src/Commands/SlashCommand.php +++ b/src/Commands/SlashCommand.php @@ -65,26 +65,17 @@ public function create(): DiscordCommand return new DiscordCommand($this->discord(), $command); } - /** - * Handle the slash command. - * - * @param \Discord\Parts\Interactions\Interaction $interaction - * @return mixed - */ - abstract public function handle($interaction); - /** * Maybe handle the slash command. - * - * @param \Discord\Parts\Interactions\Interaction $interaction - * @return mixed */ - public function maybeHandle($interaction) + public function maybeHandle(Interaction $interaction): void { if (! $this->isAdminCommand()) { $this->parseOptions($interaction); - $this->handle($interaction); + $this->resolveHandler([ + 'interaction' => $interaction, + ]); $this->clearOptions(); @@ -92,19 +83,16 @@ public function maybeHandle($interaction) } if ($this->isAdminCommand() && ! $this->isAdmin($interaction->member->user)) { - return $interaction->respondWithMessage( - $this - ->message('You do not have permission to run this command.') - ->title('Permission Denied') - ->error() - ->build(), - ephemeral: true - ); + $this->handleDenied($interaction); + + return; } $this->parseOptions($interaction); - $this->handle($interaction); + $this->resolveHandler([ + 'interaction' => $interaction, + ]); $this->clearOptions(); } diff --git a/src/Concerns/HasHandler.php b/src/Concerns/HasHandler.php new file mode 100644 index 0000000..f5824e1 --- /dev/null +++ b/src/Concerns/HasHandler.php @@ -0,0 +1,22 @@ +bot->app->call([$this, 'handle'], $parameters); + } +} diff --git a/src/Console/Commands/stubs/command.stub b/src/Console/Commands/stubs/command.stub index 1b9ed01..74d3dde 100644 --- a/src/Console/Commands/stubs/command.stub +++ b/src/Console/Commands/stubs/command.stub @@ -2,6 +2,7 @@ namespace {{ namespace }}; +use Discord\Parts\Channel\Message; use Discord\Parts\Interactions\Interaction; use Laracord\Commands\Command; @@ -37,19 +38,15 @@ class {{ class }} extends Command /** * Handle the command. - * - * @param \Discord\Parts\Channel\Message $message - * @param array $args - * @return void */ - public function handle($message, $args) + public function handle(Message $message, array $args): void { return $this ->message() ->title('{{ title }}') ->content('Hello world!') ->button('👋', route: 'wave') - ->send($message); + ->reply($message); } /** diff --git a/src/Console/Commands/stubs/context-menu.stub b/src/Console/Commands/stubs/context-menu.stub index 2f5dbc9..df63418 100644 --- a/src/Console/Commands/stubs/context-menu.stub +++ b/src/Console/Commands/stubs/context-menu.stub @@ -38,16 +38,14 @@ class {{ class }} extends ContextMenu /** * Handle the context menu interaction. */ - public function handle(Interaction $interaction, Message|User|null $target): mixed + public function handle(Interaction $interaction, Message|User|null $target): void { - $interaction->respondWithMessage( - $this - ->message() - ->title('{{ title }}') - ->content('Hello world!') - ->button('👋', route: 'wave') - ->build() - ); + $this + ->message() + ->title('{{ title }}') + ->content('Hello world!') + ->button('👋', route: 'wave') + ->reply($interaction); } /** diff --git a/src/Console/Commands/stubs/event.stub b/src/Console/Commands/stubs/event.stub index 0f03934..39445b0 100644 --- a/src/Console/Commands/stubs/event.stub +++ b/src/Console/Commands/stubs/event.stub @@ -17,7 +17,7 @@ class {{ class }} extends Event /** * Handle the event. */ - public function handle({{ attributes }}) + public function handle({{ attributes }}): void { $this->console()->log('The {{ eventName }} event has fired!'); } diff --git a/src/Console/Commands/stubs/service.stub b/src/Console/Commands/stubs/service.stub index 4abe148..2d03c5b 100644 --- a/src/Console/Commands/stubs/service.stub +++ b/src/Console/Commands/stubs/service.stub @@ -11,10 +11,15 @@ class {{ class }} extends Service */ protected int $interval = 5; + /** + * Determine if the service handler should execute during boot. + */ + protected bool $eager = false; + /** * Handle the service. */ - public function handle(): mixed + public function handle(): void { $this->console()->log('Hello world.'); } diff --git a/src/Console/Commands/stubs/slash-command.stub b/src/Console/Commands/stubs/slash-command.stub index 81e4467..04bc895 100644 --- a/src/Console/Commands/stubs/slash-command.stub +++ b/src/Console/Commands/stubs/slash-command.stub @@ -51,20 +51,15 @@ class {{ class }} extends SlashCommand /** * Handle the slash command. - * - * @param \Discord\Parts\Interactions\Interaction $interaction - * @return mixed */ - public function handle($interaction) + public function handle(Interaction $interaction): void { - $interaction->respondWithMessage( - $this - ->message() - ->title('{{ title }}') - ->content('Hello world!') - ->button('👋', route: 'wave') - ->build() - ); + $this + ->message() + ->title('{{ title }}') + ->content('Hello world!') + ->button('👋', route: 'wave') + ->reply($interaction); } /** diff --git a/src/Laracord.php b/src/Laracord.php index 5e16620..2c2c20a 100644 --- a/src/Laracord.php +++ b/src/Laracord.php @@ -48,10 +48,8 @@ class Laracord /** * The application instance. - * - * @var \Illuminate\Contracts\Foundation\Application */ - protected $app; + public Application $app; /** * The console instance. @@ -1325,11 +1323,8 @@ public function handleSafe(string $name, callable $callback): mixed /** * Build an embed for use in a Discord message. - * - * @param string $content - * @return \Laracord\Discord\Message */ - public function message($content = '') + public function message(string $content = ''): Message { return Message::make($this) ->content($content); diff --git a/src/Services/Contracts/Service.php b/src/Services/Contracts/Service.php index 1d03229..ffb1926 100644 --- a/src/Services/Contracts/Service.php +++ b/src/Services/Contracts/Service.php @@ -4,10 +4,5 @@ interface Service { - /** - * Handle the service. - * - * @return mixed - */ - public function handle(); + // } diff --git a/src/Services/Service.php b/src/Services/Service.php index 153e5c2..1b44ef6 100644 --- a/src/Services/Service.php +++ b/src/Services/Service.php @@ -3,6 +3,7 @@ namespace Laracord\Services; use Discord\DiscordCommandClient as Discord; +use Laracord\Concerns\HasHandler; use Laracord\Console\Commands\BootCommand as Console; use Laracord\Laracord; use Laracord\Services\Contracts\Service as ServiceContract; @@ -10,6 +11,8 @@ abstract class Service implements ServiceContract { + use HasHandler; + /** * The bot instance. * @@ -41,6 +44,11 @@ abstract class Service implements ServiceContract */ protected int $interval = 5; + /** + * Determine if the service handler should execute during boot. + */ + protected bool $eager = false; + /** * Determine if the service is enabled. * @@ -68,13 +76,6 @@ public static function make(Laracord $bot): self return new static($bot); } - /** - * Handle the service. - * - * @return mixed - */ - abstract public function handle(); - /** * Boot the service. */ @@ -84,9 +85,13 @@ public function boot(): self throw new InvalidServiceInterval($this->getName()); } + if ($this->eager) { + $this->resolveHandler(); + } + $this->bot->getLoop()->addPeriodicTimer( $this->getInterval(), - fn () => $this->handle() + fn () => $this->resolveHandler() ); return $this;