Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit d136a20

Browse files
author
bariscodefx
committed
feat(CommandLoader): add registration system for commands
1 parent d201d9e commit d136a20

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

src/parts/CommandLoader.php

+48-19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Discord\Parts\Interactions\Interaction;
3333
use Discord\Builders\CommandBuilder;
3434
use Discord\Parts\Interactions\Command\Option;
35+
use Discord\Repository\Interaction\GlobalCommandRepository;
3536

3637
/**
3738
* CommandLoader
@@ -91,6 +92,9 @@ public function loadAllCommands()
9192
$this->loadDir($this->dir);
9293
$this->print_color("All commands has been loaded.", "green");
9394

95+
$this->print_color("Trying to register commands.", "green");
96+
$this->registerCommands();
97+
9498
$this->print_color("Bot is ready for use!", "green");
9599
}
96100

@@ -251,31 +255,56 @@ public function loadCommand($cmd)
251255
$closure,
252256
$options
253257
);
254-
255-
$this->client->application->commands->freshen()->then(function (\Discord\Repository\Interaction\GlobalCommandRepository $commands) use ($cmd, $command): void
256-
{
257-
if(!$commands->get('name', $command)) {
258-
$builder = CommandBuilder::new();
259-
260-
$builder->setName($command)
261-
->setDescription($cmd->description);
262-
263-
$builder->options = $cmd->options;
264-
265-
$this->client->application->commands->save(
266-
$this->client->application->commands->create(
267-
$builder->toArray()
268-
)
269-
);
270-
}
271-
});
272258

273-
$this->client->listenCommand($command, function(Interaction $interaction) use ($closure, $command) {
259+
$this->client->listenCommand($command, function(Interaction $interaction) use ($closure) {
274260
$respondable = new Respondable($interaction);
275261
$closure($respondable, $interaction->data->options);
276262
});
277263
}
278264

265+
/**
266+
* registerCommands
267+
*
268+
* @return void
269+
*/
270+
public function registerCommands(): void
271+
{
272+
$this->client->application->commands->freshen()->then(function(GlobalCommandRepository $commands): void {
273+
$allCommands = [];
274+
$allowedCategories = ["music", "reactions", "utility"];
275+
276+
foreach($this->categories as $cat) {
277+
foreach($cat as $command) {
278+
$allCommands[] = $command;
279+
}
280+
}
281+
foreach($allCommands as $cmd) {
282+
if(in_array($cmd->category, $allowedCategories) && !$commands->get('name', $cmd->command)) {
283+
$builder = CommandBuilder::new();
284+
285+
$builder->setName($cmd->command)
286+
->setDescription($cmd->description);
287+
288+
$builder->options = $cmd->options;
289+
290+
$this->client->application->commands->save(
291+
$this->client->application->commands->create(
292+
$builder->toArray()
293+
)
294+
);
295+
$this->print_color("Command registered: {$cmd->command}", "green");
296+
}
297+
}
298+
foreach($commands as $command) {
299+
$cmd = $this->getCmd($command->name);
300+
if(!$cmd || ($cmd && !in_array($cmd->category, $allowedCategories))) {
301+
$this->client->application->commands->delete($command->id)->done();
302+
$this->print_color("Command deleted: {$command->name}", "red");
303+
}
304+
}
305+
});
306+
}
307+
279308
/**
280309
* loaderInfo
281310
*

0 commit comments

Comments
 (0)