Skip to content

Commit 9a78cb4

Browse files
committed
command handler register overhaul
1 parent d935e7a commit 9a78cb4

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

src/Discord/Discord.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,29 +1933,27 @@ public function getChannel($channel_id): ?Channel
19331933
*
19341934
* @return RegisteredCommand
19351935
*/
1936-
public function listenCommand($names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
1936+
public function listenCommand(array|string $names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
19371937
{
1938-
if (! is_array($names)) {
1938+
if (is_string($names)) {
19391939
$names = [$names];
19401940
}
19411941

1942-
// registering base command
1943-
if (count($names) === 1) {
1944-
$name = array_shift($names);
1945-
if (isset($this->application_commands[$name])) {
1946-
throw new \LogicException("The command `{$name}` already exists.");
1947-
}
1942+
$baseCommand = array_shift($names);
19481943

1949-
return $this->application_commands[$name] = new RegisteredCommand($this, $name, $callback, $autocomplete_callback);
1944+
if (isset($this->application_commands[$baseCommand])) {
1945+
throw new \LogicException("The command `{$baseCommand}` already exists.");
19501946
}
19511947

1952-
$baseCommand = array_shift($names);
1948+
// registering base command
1949+
$this->application_commands[$baseCommand] = new RegisteredCommand($this, $baseCommand, $callback, $autocomplete_callback);
19531950

1954-
if (! isset($this->application_commands[$baseCommand])) {
1955-
$this->listenCommand($baseCommand);
1951+
// register subcommands
1952+
foreach($names as $subCommand){
1953+
$this->application_commands[$baseCommand]->addSubCommand($subCommand, $callback, $autocomplete_callback);
19561954
}
19571955

1958-
return $this->application_commands[$baseCommand]->addSubCommand($names, $callback, $autocomplete_callback);
1956+
return $this->application_commands[$baseCommand];
19591957
}
19601958

19611959
/**

src/Discord/Helpers/RegisteredCommand.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,28 +177,22 @@ public function getSubCommand(string $name): ?RegisteredCommand
177177
*
178178
* @return static
179179
*/
180-
public function addSubCommand($names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
180+
public function addSubCommand(array|string $names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
181181
{
182-
if (! is_array($names)) {
182+
if (is_string($names)) {
183183
$names = [$names];
184184
}
185185

186-
if (count($names) === 1) {
187-
$name = array_shift($names);
188-
if (isset($this->subCommands[$name])) {
189-
throw new \LogicException("The command `{$name}` already exists.");
190-
}
191-
192-
return $this->subCommands[$name] = new static($this->discord, $name, $callback, $autocomplete_callback);
193-
}
186+
foreach($names as $subCommand){
194187

195-
$subCommand = array_shift($names);
188+
if (isset($this->subCommands[$subCommand])) {
189+
throw new \LogicException("The command `{$subCommand}` already exists.");
190+
}
196191

197-
if (! isset($this->subCommands[$subCommand])) {
198-
$this->addSubCommand($subCommand);
192+
$this->subCommands[$subCommand] = new static($this->discord, $subCommand, $callback, $autocomplete_callback);
199193
}
200194

201-
return $this->subCommands[$subCommand]->addSubCommand($names, $callback, $autocomplete_callback);
195+
return $this;
202196
}
203197

204198
/**

0 commit comments

Comments
 (0)