Skip to content

Commit d500395

Browse files
committed
Fix cat
1 parent ffd1bb0 commit d500395

7 files changed

Lines changed: 92 additions & 74 deletions

File tree

‎_index.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require './_shared.php';
88

99
$token = env('TOKEN');
10+
$devGuild = env('DEV_GUILD');
1011

1112
$log = new Logger('stability-bot');
1213
$log->pushHandler(new StreamHandler('php://stdout'));

‎_register-commands.php‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Ragnarok\Fenrir\Parts\ApplicationCommand;
1212
use Ragnarok\Fenrir\Rest\Helpers\Command\CommandBuilder;
1313
use Ragnarok\Fenrir\Rest\Helpers\Command\CommandOptionBuilder;
14+
use React\Promise\PromiseInterface;
1415

1516
use function React\Promise\all;
1617

@@ -19,32 +20,49 @@
1920
$log = new Logger('stability-bot-command-registration');
2021
$log->pushHandler(new StreamHandler('php://stdout'));
2122

23+
$devGuild = env('DEV_GUILD');
24+
2225
$discord = (new Discord(env('TOKEN'), $log))
2326
->withGateway(new Bitwise())
2427
->withRest();
2528

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+
2732
$applicationId = $ready->application->id;
2833

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+
3043
$deletions = array_map(
31-
fn (ApplicationCommand $command) => $discord->rest->globalCommand->deleteApplicationCommand($applicationId, $command->id),
44+
fn (ApplicationCommand $command) => $delete($command->id),
3245
$commands
3346
);
3447

3548
return all($deletions);
36-
})->then(function () use ($applicationId, $discord) {
49+
})->then(function () use ($applicationId, $devGuild, $discord) {
3750
$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(
4059
CommandBuilder::new()
4160
->setName('status')
4261
->setDescription('Generate a status report')
4362
->setType(ApplicationCommandTypes::CHAT_INPUT),
4463
);
4564

46-
$commandRegistrations[] = $discord->rest->globalCommand->createApplicationCommand(
47-
$applicationId,
65+
$register(
4866
CommandBuilder::new()
4967
->setName('cat')
5068
->setDescription('Cat')
@@ -57,8 +75,7 @@
5775
),
5876
);
5977

60-
$commandRegistrations[] = $discord->rest->globalCommand->createApplicationCommand(
61-
$applicationId,
78+
$register(
6279
CommandBuilder::new()
6380
->setName('duck')
6481
->setDescription('Quack')

‎_shared.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$lockFile = json_decode(file_get_contents('composer.lock'), true);
88

99
$fenrirVersion = array_values(array_filter(
10-
$lockFile['packages'], fn ($package) => $package['name'] === 'exan/fenrir'
10+
$lockFile['packages'], fn ($package) => $package['name'] === 'ragnarok/fenrir'
1111
))[0]['version'];
1212

1313
$dotenv = Dotenv::createImmutable(__DIR__);

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
}
77
},
88
"require": {
9-
"exan/fenrir": "dev-feature/http@dev",
9+
"ragnarok/fenrir": "dev-feature/http-0.0.2@dev",
1010
"discord-php/http": "@dev",
1111
"vlucas/phpdotenv": "^5.5",
1212
"monolog/monolog": "^3.2",

‎composer.lock‎

Lines changed: 57 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Cat.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ private function __construct(private string $url)
2424
*/
2525
public static function fetch(?LoopInterface $loop = null): PromiseInterface
2626
{
27-
$url = self::BASE_URL . 'cat?json=true';
27+
$url = self::BASE_URL . 'cat';
2828

2929
$http = new Browser(loop: $loop);
3030

31-
return $http->get($url)->then(function (ResponseInterface $response) {
31+
return $http->get($url, ['Accept' => 'application/json'])->then(function (ResponseInterface $response) {
3232
$body = json_decode((string) $response->getBody());
3333

34-
return new Cat(self::BASE_URL . 'cat/' . $body->_id);
34+
return new Cat(self::BASE_URL . 'cat/' . $body->id);
3535
});
3636
}
3737

‎src/StabilityBot.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Ragnarok\Fenrir\Interaction\CommandInteraction;
99
use Ragnarok\Fenrir\Interaction\Helpers\InteractionCallbackBuilder;
1010
use Psr\Log\LoggerInterface;
11+
use Ragnarok\Fenrir\Command\AllCommandExtension;
1112
use Ragnarok\Fenrir\Command\CommandExtension;
12-
use Ragnarok\Fenrir\Command\GlobalCommandExtension;
1313
use Ragnarok\Fenrir\Constants\Events;
1414
use Ragnarok\Fenrir\Enums\InteractionCallbackType;
1515
use Ragnarok\Fenrir\Gateway\Events\Ready;
@@ -33,7 +33,7 @@ public function __construct(
3333
->withRest();
3434

3535
$this->discord->gateway->events->once(Events::READY, function (Ready $ready) {
36-
$commandExtension = new GlobalCommandExtension($ready->application->id);
36+
$commandExtension = new AllCommandExtension($ready->application->id);
3737

3838
$this->discord->registerExtension($commandExtension);
3939

0 commit comments

Comments
 (0)