From 1709cc97d19830cd73d24eb4c0c0c5a7945a75e9 Mon Sep 17 00:00:00 2001 From: Exanlv Date: Sun, 19 Mar 2023 13:57:11 +0100 Subject: [PATCH] Allow providing own driver for REST --- src/CommandHandler.php | 97 ------------------------------------------ src/Discord.php | 14 +++--- 2 files changed, 9 insertions(+), 102 deletions(-) delete mode 100644 src/CommandHandler.php diff --git a/src/CommandHandler.php b/src/CommandHandler.php deleted file mode 100644 index 1c64a9c5..00000000 --- a/src/CommandHandler.php +++ /dev/null @@ -1,97 +0,0 @@ - */ - private array $commands = []; - - private bool $devMode = false; - - public function __construct(private Discord $discord, private ?string $devGuildId = null) - { - if (!is_null($this->devGuildId)) { - $this->devMode = true; - } - } - - public function registerCommand(CommandBuilder $commandBuilder, callable $handler): void - { - if ($this->devMode) { - $this->registerGuildCommand($commandBuilder, $this->devGuildId, $handler); - } else { - $this->registerGlobalCommand($commandBuilder, $handler); - } - } - - public function registerGuildCommand(CommandBuilder $commandBuilder, string $guildId, callable $handler): void - { - $this->activateListener(); - - /** Ready event includes Application ID */ - $this->discord->gateway->events->once( - Events::READY, - function (Ready $ready) use ($commandBuilder, $guildId, $handler) { - $this->discord->rest->guildCommand->createApplicationCommand( - $ready->user->id, - $guildId, - $commandBuilder - )->then( - fn (ApplicationCommand $applicationCommand) => $this->commands[$applicationCommand->id] = $handler - ); - } - ); - } - - public function registerGlobalCommand(CommandBuilder $commandBuilder, callable $handler): void - { - $this->activateListener(); - - /** Ready event includes Application ID */ - $this->discord->gateway->events->once(Events::READY, function (Ready $ready) use ($commandBuilder, $handler) { - $this->discord->rest->globalCommand->createApplicationCommand( - $ready->user->id, - $commandBuilder - )->then( - fn (ApplicationCommand $applicationCommand) => $this->commands[$applicationCommand->id] = $handler - ); - }); - } - - private function activateListener() - { - if ($this->activated) { - return; - } - - $this->activated = true; - - $this->discord->gateway->events->on( - Events::INTERACTION_CREATE, - $this->handleInteraction(...) - ); - } - - private function handleInteraction(InteractionCreate $interactionCreate) - { - if (!isset($this->commands[$interactionCreate->data->id])) { - return; - } - - $firedCommand = new FiredCommand($interactionCreate, $this->discord); - - $this->commands[$interactionCreate->data->id]($firedCommand); - } -} diff --git a/src/Discord.php b/src/Discord.php index 00c3f8a9..cc6217e2 100644 --- a/src/Discord.php +++ b/src/Discord.php @@ -4,6 +4,7 @@ namespace Exan\Fenrir; +use Discord\Http\DriverInterface; use Discord\Http\Drivers\Guzzle; use Discord\Http\Http; use Exan\Fenrir\Bitwise\Bitwise; @@ -55,15 +56,18 @@ public function withGateway( return $this; } - public function withRest() - { + public function withRest( + DriverInterface $driver = null, + ) { + $driver ??= new Guzzle( + $this->loop + ); + $this->http = new Http( 'Bot ' . $this->token, $this->loop, $this->logger, - new Guzzle( - $this->loop - ) + $driver ); $this->rest = new Rest($this->http, $this->mapper);