Skip to content

6. Application Commands

Exanlv edited this page Mar 28, 2024 · 6 revisions

Fenrir comes with a first-party extension for Application Commands.

To use it, you first have to register your commands.

Global commands

use Ragnarok\Fenrir\Enums\ApplicationCommandOptionType;
use Ragnarok\Fenrir\Rest\Helpers\Command\CommandBuilder;
use Ragnarok\Fenrir\Rest\Helpers\Command\CommandOptionBuilder;

$discord->rest->globalCommand->createApplicationCommand(
    $applicationId,
    CommandBuilder::new()
        ->setName('status')
        ->setDescription('Get the status of the bot!')
)->then(function () use ($discord, $commandExtension) {
    // Command is registered
});


$discord->rest->globalCommand->createApplicationCommand(
    $ready->application->id,
    CommandBuilder::new()
        ->setName('command')
        ->setDescription('A command')
        ->addOption(
            CommandOptionBuilder::new()
                ->setType(ApplicationCommandOptionType::SUB_COMMAND)
                ->setName('sub')
                ->setDescription('A sub command')
        )
)->then(function () use ($discord, $commandExtension) {
    // Sub command is registered
});

You can then use GlobalCommandExtension to listen for uses of these commands.

use Ragnarok\Fenrir\Command\GlobalCommandExtension;
use Ragnarok\Fenrir\Interaction\CommandInteraction;

$commandExtension = new GlobalCommandExtension();

$discord->registerExtension($commandExtension);

$commandExtension->on('status', function (CommandInteraction $commandInteraction) {
    echo 'Status command has been executed!';
});

$commandExtension->on('command.sub', function (CommandInteraction $commandInteraction) {
    echo 'Sub command has been executed!';
});

Guild commands

If you'd like to use Guild commands instead, they work quite similar, though you will have to use the GuildCommandExtension instead.

use Ragnarok\Fenrir\Rest\Helpers\Command\CommandBuilder;

$discord->rest->guildCommand->createApplicationCommand(
    'guild-id',
    'application-id',
    CommandBuilder::new()
        ->setName('guild-command')
        ->setDescription('A guild command')
)->then({
    // Command is registered
});
use Ragnarok\Fenrir\Command\GuildCommandExtension;
use Ragnarok\Fenrir\Interaction\CommandInteraction;

$commandExtension = new GuildCommandExtension('guild-id');

$discord->registerExtension($commandExtension);

$commandExtension->on('status', function (CommandInteraction $commandInteraction) {
    echo 'Status command has been executed!';
});

$commandExtension->on('command.sub', function (CommandInteraction $commandInteraction) {
    echo 'Sub command has been executed!';
});

Hosting

If you're using docker (or similar) to host your bot, you could consider making the registering of the commands a separate part of the build process.

RUN php ./register-commands.php

CMD [ "php", "./run-bot.php" ]

For a fully operational example, you can check out Lyngvi, the bot used to monitor stability & test experimental features.

Clone this wiki locally