-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Command groups always emitted the first command in group (#85)
* Parse at runtime rather than startup * Fix up Guild commands & add AllCommandExtension * Fix class name * Fix up tests * Add test for allCOmmandExtension
- Loading branch information
Showing
8 changed files
with
358 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ragnarok\Fenrir\Command; | ||
|
||
use Ragnarok\Fenrir\Gateway\Events\InteractionCreate; | ||
|
||
/** | ||
* Emits an event for Guild Commands and Global Commands | ||
*/ | ||
class AllCommandExtension extends CommandExtension | ||
{ | ||
protected function emitInteraction(InteractionCreate $interaction): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Ragnarok\Fenrir\Command; | ||
|
||
use Fakes\Ragnarok\Fenrir\DiscordFake; | ||
use PHPUnit\Framework\TestCase; | ||
use Ragnarok\Fenrir\Command\AllCommandExtension; | ||
use Ragnarok\Fenrir\Constants\Events; | ||
use Ragnarok\Fenrir\Discord; | ||
use Ragnarok\Fenrir\Enums\ApplicationCommandOptionType; | ||
use Ragnarok\Fenrir\Enums\InteractionType; | ||
use Ragnarok\Fenrir\Gateway\Events\InteractionCreate; | ||
use Ragnarok\Fenrir\Interaction\CommandInteraction; | ||
use Ragnarok\Fenrir\Parts\ApplicationCommandInteractionDataOptionStructure; | ||
use Ragnarok\Fenrir\Parts\InteractionData; | ||
|
||
class AllCommandExtensionTest extends TestCase | ||
{ | ||
private Discord $discord; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->discord = DiscordFake::get(); | ||
} | ||
|
||
public function testItEmitsEventsForApplicationCommands() | ||
{ | ||
$extension = new AllCommandExtension(); | ||
$extension->initialize($this->discord); | ||
|
||
$hasRun = [false, false, false]; | ||
|
||
$extension->on('command-1', function (CommandInteraction $firedCommand) use (&$hasRun) { | ||
$hasRun[0] = true; | ||
}); | ||
|
||
$extension->on('command-2', function (CommandInteraction $firedCommand) use (&$hasRun) { | ||
$hasRun[1] = true; | ||
}); | ||
|
||
$extension->on('command-3', function (CommandInteraction $firedCommand) use (&$hasRun) { | ||
$hasRun[2] = true; | ||
}); | ||
|
||
$interaction = new InteractionCreate(); | ||
$interaction->type = InteractionType::APPLICATION_COMMAND; | ||
$interaction->data = new InteractionData(); | ||
$interaction->data->name = 'command-1'; | ||
|
||
$this->discord->gateway->events->emit( | ||
Events::INTERACTION_CREATE, | ||
[$interaction] | ||
); | ||
|
||
$interaction->data->name = 'command-2'; | ||
$interaction->data->guild_id = '::guild id::'; | ||
|
||
$this->discord->gateway->events->emit( | ||
Events::INTERACTION_CREATE, | ||
[$interaction] | ||
); | ||
|
||
$this->assertTrue($hasRun[0], 'Command 1 did not run'); | ||
$this->assertTrue($hasRun[1], 'Command 2 did not run'); | ||
$this->assertFalse($hasRun[2], 'Command 3 should not have been run'); | ||
} | ||
|
||
public function testItEmitsEventsForGuildCommands() | ||
{ | ||
$extension = new AllCommandExtension(); | ||
$extension->initialize($this->discord); | ||
|
||
$hasRun = [false, false, false]; | ||
|
||
$extension->on('command-1', function (CommandInteraction $firedCommand) use (&$hasRun) { | ||
$hasRun[0] = true; | ||
}); | ||
|
||
$extension->on('command-2', function (CommandInteraction $firedCommand) use (&$hasRun) { | ||
$hasRun[1] = true; | ||
}); | ||
|
||
$extension->on('command-3', function (CommandInteraction $firedCommand) use (&$hasRun) { | ||
$hasRun[2] = true; | ||
}); | ||
|
||
$interaction = new InteractionCreate(); | ||
$interaction->type = InteractionType::APPLICATION_COMMAND; | ||
$interaction->data = new InteractionData(); | ||
$interaction->data->name = 'command-1'; | ||
|
||
$this->discord->gateway->events->emit( | ||
Events::INTERACTION_CREATE, | ||
[$interaction] | ||
); | ||
|
||
$interaction->data->name = 'command-2'; | ||
|
||
$this->discord->gateway->events->emit( | ||
Events::INTERACTION_CREATE, | ||
[$interaction] | ||
); | ||
|
||
$this->assertTrue($hasRun[0], 'Command 1 did not run'); | ||
$this->assertTrue($hasRun[1], 'Command 2 did not run'); | ||
$this->assertFalse($hasRun[2], 'Command 3 should not have been run'); | ||
} | ||
|
||
public static function nameMappingProvider(): array | ||
{ | ||
return [ | ||
'Plain name' => [ | ||
'interaction' => (function () { | ||
$command = new InteractionCreate(); | ||
$command->type = InteractionType::APPLICATION_COMMAND; | ||
$command->data = new InteractionData(); | ||
$command->data->name = 'command-name'; | ||
|
||
return $command; | ||
})(), | ||
'expectedName' => 'command-name' | ||
], | ||
|
||
'Nested 1 layer' => [ | ||
'interaction' => (function () { | ||
$command = new InteractionCreate(); | ||
$command->type = InteractionType::APPLICATION_COMMAND; | ||
$command->data = new InteractionData(); | ||
$command->data->name = 'command-name'; | ||
|
||
$command->data->options = [new ApplicationCommandInteractionDataOptionStructure()]; | ||
$command->data->options[0]->name = 'sub'; | ||
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND; | ||
|
||
return $command; | ||
})(), | ||
'expectedName' => 'command-name.sub' | ||
], | ||
|
||
'Nested 2 layer' => [ | ||
'interaction' => (function () { | ||
$command = new InteractionCreate(); | ||
$command->type = InteractionType::APPLICATION_COMMAND; | ||
$command->data = new InteractionData(); | ||
$command->data->name = 'command-name'; | ||
|
||
$command->data->options = [new ApplicationCommandInteractionDataOptionStructure()]; | ||
$command->data->options[0]->name = 'double'; | ||
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP; | ||
|
||
$command->data->options[0]->options = [new ApplicationCommandInteractionDataOptionStructure()]; | ||
$command->data->options[0]->options[0]->name = 'sub'; | ||
$command->data->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP; | ||
|
||
return $command; | ||
})(), | ||
'expectedName' => 'command-name.double.sub' | ||
], | ||
|
||
'Nested 3 layer' => [ // NOTE: Not supported by Discord | ||
'interaction' => (function () { | ||
$command = new InteractionCreate(); | ||
$command->type = InteractionType::APPLICATION_COMMAND; | ||
$command->data = new InteractionData(); | ||
$command->data->name = 'command-name'; | ||
|
||
$command->data->options = [new ApplicationCommandInteractionDataOptionStructure()]; | ||
$command->data->options[0]->name = 'double'; | ||
$command->data->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP; | ||
|
||
$command->data->options[0]->options = [new ApplicationCommandInteractionDataOptionStructure()]; | ||
$command->data->options[0]->options[0]->name = 'sub'; | ||
$command->data->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP; | ||
|
||
$command->data->options[0]->options[0]->options = [new ApplicationCommandInteractionDataOptionStructure()]; | ||
$command->data->options[0]->options[0]->options[0]->name = 'dub'; | ||
$command->data->options[0]->options[0]->options[0]->type = ApplicationCommandOptionType::SUB_COMMAND_GROUP; | ||
|
||
return $command; | ||
})(), | ||
'expectedName' => 'command-name.double.sub.dub' | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.