Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand Down
28 changes: 11 additions & 17 deletions src/Discord/Helpers/RegisteredCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down