From 9a78cb40b78402797b3781d318aa8e27dc9d5878 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 6 Oct 2025 04:22:32 +0200 Subject: [PATCH 1/4] command handler register overhaul --- src/Discord/Discord.php | 24 +++++++++++------------ src/Discord/Helpers/RegisteredCommand.php | 22 ++++++++------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/Discord/Discord.php b/src/Discord/Discord.php index bdecc048c..b633c6d42 100644 --- a/src/Discord/Discord.php +++ b/src/Discord/Discord.php @@ -1933,29 +1933,27 @@ public function getChannel($channel_id): ?Channel * * @return RegisteredCommand */ - public function listenCommand($names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand + public function listenCommand(array|string $names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand { - if (! is_array($names)) { + if (is_string($names)) { $names = [$names]; } - // registering base command - if (count($names) === 1) { - $name = array_shift($names); - if (isset($this->application_commands[$name])) { - throw new \LogicException("The command `{$name}` already exists."); - } + $baseCommand = array_shift($names); - return $this->application_commands[$name] = new RegisteredCommand($this, $name, $callback, $autocomplete_callback); + if (isset($this->application_commands[$baseCommand])) { + throw new \LogicException("The command `{$baseCommand}` already exists."); } - $baseCommand = array_shift($names); + // registering base command + $this->application_commands[$baseCommand] = new RegisteredCommand($this, $baseCommand, $callback, $autocomplete_callback); - if (! isset($this->application_commands[$baseCommand])) { - $this->listenCommand($baseCommand); + // register subcommands + foreach($names as $subCommand){ + $this->application_commands[$baseCommand]->addSubCommand($subCommand, $callback, $autocomplete_callback); } - return $this->application_commands[$baseCommand]->addSubCommand($names, $callback, $autocomplete_callback); + return $this->application_commands[$baseCommand]; } /** diff --git a/src/Discord/Helpers/RegisteredCommand.php b/src/Discord/Helpers/RegisteredCommand.php index 3cafec30e..5643c2840 100644 --- a/src/Discord/Helpers/RegisteredCommand.php +++ b/src/Discord/Helpers/RegisteredCommand.php @@ -177,28 +177,22 @@ public function getSubCommand(string $name): ?RegisteredCommand * * @return static */ - public function addSubCommand($names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand + public function addSubCommand(array|string $names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand { - if (! is_array($names)) { + if (is_string($names)) { $names = [$names]; } - if (count($names) === 1) { - $name = array_shift($names); - if (isset($this->subCommands[$name])) { - throw new \LogicException("The command `{$name}` already exists."); - } - - return $this->subCommands[$name] = new static($this->discord, $name, $callback, $autocomplete_callback); - } + foreach($names as $subCommand){ - $subCommand = array_shift($names); + if (isset($this->subCommands[$subCommand])) { + throw new \LogicException("The command `{$subCommand}` already exists."); + } - if (! isset($this->subCommands[$subCommand])) { - $this->addSubCommand($subCommand); + $this->subCommands[$subCommand] = new static($this->discord, $subCommand, $callback, $autocomplete_callback); } - return $this->subCommands[$subCommand]->addSubCommand($names, $callback, $autocomplete_callback); + return $this; } /** From 17eed16c9b08cf0544b1358d8fecec655f9a08b9 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 6 Oct 2025 04:28:35 +0200 Subject: [PATCH 2/4] adjust phpdoc types --- src/Discord/Discord.php | 6 +++--- src/Discord/Helpers/RegisteredCommand.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Discord/Discord.php b/src/Discord/Discord.php index b633c6d42..4600f6917 100644 --- a/src/Discord/Discord.php +++ b/src/Discord/Discord.php @@ -1925,9 +1925,9 @@ public function getChannel($channel_id): ?Channel /** * Add listener for incoming application command from interaction. * - * @param array|string $names - * @param callable|null $callback - * @param callable|null $autocomplete_callback + * @param string[]|string $names + * @param callable|null $callback + * @param callable|null $autocomplete_callback * * @throws \LogicException * diff --git a/src/Discord/Helpers/RegisteredCommand.php b/src/Discord/Helpers/RegisteredCommand.php index 5643c2840..679fd129a 100644 --- a/src/Discord/Helpers/RegisteredCommand.php +++ b/src/Discord/Helpers/RegisteredCommand.php @@ -169,9 +169,9 @@ public function getSubCommand(string $name): ?RegisteredCommand /** * Adds a sub-command to the command. * - * @param array|string $names - * @param callable|null $callback - * @param callable|null $autocomplete_callback + * @param string[]|string $names + * @param callable|null $callback + * @param callable|null $autocomplete_callback * * @throws \LogicException * From 17553d6cc5d7054bdb40aa2a9e0c9e0f411737c9 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 6 Oct 2025 17:31:27 +0200 Subject: [PATCH 3/4] don't throw if the command is already registered, only create it if necessary --- src/Discord/Discord.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Discord/Discord.php b/src/Discord/Discord.php index 4600f6917..746709f7d 100644 --- a/src/Discord/Discord.php +++ b/src/Discord/Discord.php @@ -1941,12 +1941,10 @@ public function listenCommand(array|string $names, ?callable $callback = null, ? $baseCommand = array_shift($names); - if (isset($this->application_commands[$baseCommand])) { - throw new \LogicException("The command `{$baseCommand}` already exists."); - } - // registering base command - $this->application_commands[$baseCommand] = new RegisteredCommand($this, $baseCommand, $callback, $autocomplete_callback); + if (!isset($this->application_commands[$baseCommand])) { + $this->application_commands[$baseCommand] = new RegisteredCommand($this, $baseCommand, $callback, $autocomplete_callback); + } // register subcommands foreach($names as $subCommand){ From 91792b2b5c42aae7a714c44830f35ceef18ee003 Mon Sep 17 00:00:00 2001 From: smiley Date: Mon, 6 Oct 2025 18:16:51 +0200 Subject: [PATCH 4/4] don't need the foreach here --- src/Discord/Discord.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Discord/Discord.php b/src/Discord/Discord.php index 746709f7d..8d1d08789 100644 --- a/src/Discord/Discord.php +++ b/src/Discord/Discord.php @@ -1947,9 +1947,7 @@ public function listenCommand(array|string $names, ?callable $callback = null, ? } // register subcommands - foreach($names as $subCommand){ - $this->application_commands[$baseCommand]->addSubCommand($subCommand, $callback, $autocomplete_callback); - } + $this->application_commands[$baseCommand]->addSubCommand($names, $callback, $autocomplete_callback); return $this->application_commands[$baseCommand]; }