Skip to content

Commit

Permalink
Fix issue of commands not registering when not using localizations fo…
Browse files Browse the repository at this point in the history
…r command choices (#97)
  • Loading branch information
Exanlv authored Jul 29, 2024
1 parent 391b19b commit c1f0373
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Rest/GuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class GuildCommand extends HttpResource
{
/**
* @see https://discord.com/developers/docs/interactions/application-commands#get-guild-application-command
*
* @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\ApplicationCommand[]>
*/
public function getCommands(string $guildId, string $applicationId, bool $withLocalizations = false): ExtendedPromiseInterface
{
$endpoint = Endpoint::bind(Endpoint::GUILD_APPLICATION_COMMANDS, $applicationId, $guildId);
Expand Down
11 changes: 8 additions & 3 deletions src/Rest/Helpers/Command/CommandOptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,23 @@ public function getRequired(): ?bool
*
* @param array<string, string> $localizedNames `key => locale`, `value => description`
*/
public function addChoice(string $name, string|int|float $value, array $localizedNames = []): self
public function addChoice(string $name, string|int|float $value, ?array $localizedNames = null): self
{
if (!isset($this->data['choices'])) {
$this->data['choices'] = [];
}

$this->data['choices'][] = [
$choice = [
'name' => $name,
'name_localizations' => $localizedNames,
'value' => $value,
];

if ($localizedNames) {
$choice['name_localizations'] = $localizedNames;
}

$this->data['choices'][] = $choice;

return $this;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Rest/Helpers/Command/CommandOptionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public function testAddChoice(): void
$builder->addChoice('choice-2', 'choice-value-2');

$this->assertEquals([
['name' => 'choice-1', 'value' => 'choice-value-1', 'name_localizations' => []],
['name' => 'choice-2', 'value' => 'choice-value-2', 'name_localizations' => []]
['name' => 'choice-1', 'value' => 'choice-value-1'],
['name' => 'choice-2', 'value' => 'choice-value-2']
], $builder->getChoices());

$this->assertEquals([
['name' => 'choice-1', 'value' => 'choice-value-1', 'name_localizations' => []],
['name' => 'choice-2', 'value' => 'choice-value-2', 'name_localizations' => []]
['name' => 'choice-1', 'value' => 'choice-value-1'],
['name' => 'choice-2', 'value' => 'choice-value-2']
], $builder->get()['choices']);
}

Expand Down

0 comments on commit c1f0373

Please sign in to comment.