|
32 | 32 | use Discord\Parts\Interactions\Interaction;
|
33 | 33 | use Discord\Builders\CommandBuilder;
|
34 | 34 | use Discord\Parts\Interactions\Command\Option;
|
| 35 | +use Discord\Repository\Interaction\GlobalCommandRepository; |
35 | 36 |
|
36 | 37 | /**
|
37 | 38 | * CommandLoader
|
@@ -91,6 +92,9 @@ public function loadAllCommands()
|
91 | 92 | $this->loadDir($this->dir);
|
92 | 93 | $this->print_color("All commands has been loaded.", "green");
|
93 | 94 |
|
| 95 | + $this->print_color("Trying to register commands.", "green"); |
| 96 | + $this->registerCommands(); |
| 97 | + |
94 | 98 | $this->print_color("Bot is ready for use!", "green");
|
95 | 99 | }
|
96 | 100 |
|
@@ -251,31 +255,56 @@ public function loadCommand($cmd)
|
251 | 255 | $closure,
|
252 | 256 | $options
|
253 | 257 | );
|
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 |
| - }); |
272 | 258 |
|
273 |
| - $this->client->listenCommand($command, function(Interaction $interaction) use ($closure, $command) { |
| 259 | + $this->client->listenCommand($command, function(Interaction $interaction) use ($closure) { |
274 | 260 | $respondable = new Respondable($interaction);
|
275 | 261 | $closure($respondable, $interaction->data->options);
|
276 | 262 | });
|
277 | 263 | }
|
278 | 264 |
|
| 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 | + |
279 | 308 | /**
|
280 | 309 | * loaderInfo
|
281 | 310 | *
|
|
0 commit comments