-
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.
* Initial setup for better command extension * Finish up initial implementation * Fix deprecation notice * Add unit tests
- Loading branch information
Showing
11 changed files
with
619 additions
and
21 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
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ragnarok\Fenrir\Command; | ||
|
||
use Evenement\EventEmitter; | ||
use Ragnarok\Fenrir\Constants\Events; | ||
use Ragnarok\Fenrir\Discord; | ||
use Ragnarok\Fenrir\Enums\ApplicationCommandOptionType; | ||
use Ragnarok\Fenrir\Enums\InteractionType; | ||
use Ragnarok\Fenrir\Extension\Extension; | ||
use Ragnarok\Fenrir\FilteredEventEmitter; | ||
use Ragnarok\Fenrir\Gateway\Events\InteractionCreate; | ||
use Ragnarok\Fenrir\Interaction\CommandInteraction; | ||
use Ragnarok\Fenrir\Parts\ApplicationCommand; | ||
use Ragnarok\Fenrir\Parts\ApplicationCommandOptionStructure; | ||
|
||
use function Freezemage\ArrayUtils\find; | ||
|
||
abstract class CommandExtension extends EventEmitter implements Extension | ||
{ | ||
protected array $commandMappings = []; | ||
|
||
protected FilteredEventEmitter $commandListener; | ||
|
||
abstract protected function loadExistingCommands(Discord $discord): void; | ||
|
||
public function initialize(Discord $discord): void | ||
{ | ||
$this->loadExistingCommands($discord); | ||
|
||
$this->registerListener($discord); | ||
} | ||
|
||
private function registerListener(Discord $discord): void | ||
{ | ||
$this->commandListener = new FilteredEventEmitter( | ||
$discord->gateway->events, | ||
Events::INTERACTION_CREATE, | ||
fn (InteractionCreate $interactionCreate) => | ||
isset($interactionCreate->type) | ||
&& $interactionCreate->type === InteractionType::APPLICATION_COMMAND | ||
&& isset($this->commandMappings[$interactionCreate->data->id]) | ||
); | ||
|
||
$this->commandListener->on(Events::INTERACTION_CREATE, function (InteractionCreate $interaction) use ($discord) { | ||
$this->handleInteraction($interaction, $discord); | ||
}); | ||
|
||
$this->commandListener->start(); | ||
} | ||
|
||
private function handleInteraction(InteractionCreate $interaction, Discord $discord) | ||
{ | ||
$firedCommand = new CommandInteraction($interaction, $discord); | ||
|
||
$this->emit($this->commandMappings[$interaction->data->id], [$firedCommand]); | ||
} | ||
|
||
protected function getFullNameByCommand(ApplicationCommand $command): string | ||
{ | ||
$names = [$command->name]; | ||
|
||
$this->drillName($command->options ?? [], $names); | ||
|
||
return implode('.', $names); | ||
} | ||
|
||
private function drillName(array $options, array &$names) | ||
{ | ||
/** @var ?ApplicationCommandOptionStructure */ | ||
$subCommand = find($options ?? [], function (ApplicationCommandOptionStructure $option) { | ||
return in_array( | ||
$option->type, | ||
[ | ||
ApplicationCommandOptionType::SUB_COMMAND, | ||
ApplicationCommandOptionType::SUB_COMMAND_GROUP, | ||
] | ||
); | ||
}); | ||
|
||
if (!is_null($subCommand)) { | ||
$names[] = $subCommand->name; | ||
|
||
$this->drillName($subCommand->options ?? [], $names); | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ragnarok\Fenrir\Command; | ||
|
||
use Ragnarok\Fenrir\Discord; | ||
use Ragnarok\Fenrir\Parts\ApplicationCommand; | ||
|
||
class GlobalCommandExtension extends CommandExtension | ||
{ | ||
public function __construct(protected readonly string $applicationId) | ||
{ | ||
} | ||
|
||
protected function loadExistingCommands(Discord $discord): void | ||
{ | ||
$discord->rest->globalCommand->getCommands($this->applicationId) | ||
->then(function (array $commands) { | ||
/** @var ApplicationCommand $command */ | ||
foreach ($commands as $command) { | ||
$this->commandMappings[$command->id] = $this->getFullNameByCommand($command); | ||
} | ||
}); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ragnarok\Fenrir\Command; | ||
|
||
use Ragnarok\Fenrir\Discord; | ||
use Ragnarok\Fenrir\Parts\ApplicationCommand; | ||
|
||
class GuildCommandExtension extends CommandExtension | ||
{ | ||
public function __construct(protected readonly string $applicationId, protected readonly string $guildId) | ||
{ | ||
} | ||
|
||
protected function loadExistingCommands(Discord $discord): void | ||
{ | ||
$discord->rest->guildCommand->getCommands($this->guildId, $this->applicationId) | ||
->then(function (array $commands) { | ||
/** @var ApplicationCommand $command */ | ||
foreach ($commands as $command) { | ||
$this->commandMappings[$command->id] = $this->getFullNameByCommand($command); | ||
} | ||
}); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.