|
11 | 11 | use Ragnarok\Fenrir\Parts\ApplicationCommand; |
12 | 12 | use Ragnarok\Fenrir\Rest\Helpers\Command\CommandBuilder; |
13 | 13 | use Ragnarok\Fenrir\Rest\Helpers\Command\CommandOptionBuilder; |
| 14 | +use React\Promise\PromiseInterface; |
14 | 15 |
|
15 | 16 | use function React\Promise\all; |
16 | 17 |
|
|
19 | 20 | $log = new Logger('stability-bot-command-registration'); |
20 | 21 | $log->pushHandler(new StreamHandler('php://stdout')); |
21 | 22 |
|
| 23 | +$devGuild = env('DEV_GUILD'); |
| 24 | + |
22 | 25 | $discord = (new Discord(env('TOKEN'), $log)) |
23 | 26 | ->withGateway(new Bitwise()) |
24 | 27 | ->withRest(); |
25 | 28 |
|
26 | | -$discord->gateway->events->once(Events::READY, function (Ready $ready) use ($discord) { |
| 29 | +$discord->gateway->events->once(Events::READY, function (Ready $ready) use ($devGuild, $discord) { |
| 30 | + /** @var Discord $discord */ |
| 31 | + |
27 | 32 | $applicationId = $ready->application->id; |
28 | 33 |
|
29 | | - $discord->rest->globalCommand->getCommands($applicationId)->then(function (array $commands) use ($applicationId, $discord) { |
| 34 | + $discord->rest->{$devGuild ? 'guildCommand' : 'globalCommand'}->getCommands( |
| 35 | + ...array_filter([$devGuild, $applicationId]) |
| 36 | + )->then(function (array $commands) use ($applicationId, $devGuild, $discord) { |
| 37 | + $delete = $devGuild ? function (string $commandId) use ($applicationId, $devGuild, $discord): PromiseInterface { |
| 38 | + return $discord->rest->guildCommand->deleteApplicationCommand($applicationId, $devGuild, $commandId); |
| 39 | + } : function (string $commandId) use ($applicationId, $devGuild, $discord): PromiseInterface { |
| 40 | + return $discord->rest->globalCommand->deleteApplicationCommand($applicationId, $commandId); |
| 41 | + }; |
| 42 | + |
30 | 43 | $deletions = array_map( |
31 | | - fn (ApplicationCommand $command) => $discord->rest->globalCommand->deleteApplicationCommand($applicationId, $command->id), |
| 44 | + fn (ApplicationCommand $command) => $delete($command->id), |
32 | 45 | $commands |
33 | 46 | ); |
34 | 47 |
|
35 | 48 | return all($deletions); |
36 | | - })->then(function () use ($applicationId, $discord) { |
| 49 | + })->then(function () use ($applicationId, $devGuild, $discord) { |
37 | 50 | $commandRegistrations = []; |
38 | | - $commandRegistrations[] = $discord->rest->globalCommand->createApplicationCommand( |
39 | | - $applicationId, |
| 51 | + |
| 52 | + $register = $devGuild ? function (CommandBuilder $commandBuilder) use ($discord, $devGuild, $applicationId, &$commandRegistrations) { |
| 53 | + $commandRegistrations[] = $discord->rest->guildCommand->createApplicationCommand($applicationId, $devGuild, $commandBuilder); |
| 54 | + } : function (CommandBuilder $commandBuilder) use ($discord, $applicationId, &$commandRegistrations) { |
| 55 | + $commandRegistrations[] = $discord->rest->globalCommand->createApplicationCommand($applicationId, $commandBuilder); |
| 56 | + }; |
| 57 | + |
| 58 | + $register( |
40 | 59 | CommandBuilder::new() |
41 | 60 | ->setName('status') |
42 | 61 | ->setDescription('Generate a status report') |
43 | 62 | ->setType(ApplicationCommandTypes::CHAT_INPUT), |
44 | 63 | ); |
45 | 64 |
|
46 | | - $commandRegistrations[] = $discord->rest->globalCommand->createApplicationCommand( |
47 | | - $applicationId, |
| 65 | + $register( |
48 | 66 | CommandBuilder::new() |
49 | 67 | ->setName('cat') |
50 | 68 | ->setDescription('Cat') |
|
57 | 75 | ), |
58 | 76 | ); |
59 | 77 |
|
60 | | - $commandRegistrations[] = $discord->rest->globalCommand->createApplicationCommand( |
61 | | - $applicationId, |
| 78 | + $register( |
62 | 79 | CommandBuilder::new() |
63 | 80 | ->setName('duck') |
64 | 81 | ->setDescription('Quack') |
|
0 commit comments