diff --git a/src/Discord/Discord.php b/src/Discord/Discord.php index bdecc048c..8d1d08789 100644 --- a/src/Discord/Discord.php +++ b/src/Discord/Discord.php @@ -1925,37 +1925,31 @@ 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 * * @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."); - } - - return $this->application_commands[$name] = new RegisteredCommand($this, $name, $callback, $autocomplete_callback); - } - $baseCommand = array_shift($names); - if (! isset($this->application_commands[$baseCommand])) { - $this->listenCommand($baseCommand); + // registering base command + if (!isset($this->application_commands[$baseCommand])) { + $this->application_commands[$baseCommand] = new RegisteredCommand($this, $baseCommand, $callback, $autocomplete_callback); } - return $this->application_commands[$baseCommand]->addSubCommand($names, $callback, $autocomplete_callback); + // register subcommands + $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..679fd129a 100644 --- a/src/Discord/Helpers/RegisteredCommand.php +++ b/src/Discord/Helpers/RegisteredCommand.php @@ -169,36 +169,30 @@ 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 * * @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."); - } + foreach($names as $subCommand){ - return $this->subCommands[$name] = new static($this->discord, $name, $callback, $autocomplete_callback); - } - - $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; } /**