Skip to content
SQKo edited this page Dec 9, 2023 · 19 revisions

Formerly known as slash commands, DiscordPHP v7+ supports all types of Discord Application Commands

Here is the example of /ping command:

<?php

include __DIR__.'/vendor/autoload.php';

use Discord\Builders\MessageBuilder;
use Discord\Discord;
use Discord\Parts\Interactions\Command\Command; // Please note to use this correct namespace!
use Discord\Parts\Interactions\Interaction;

$discord = new Discord(['token'=> 'your bot token']); // DiscordCommandClient works too
$discord->on('ready', function (Discord $discord) {
    // When bot is ready, attempt to create a global slash command "/ping"
    // After the command created successfully in your bot, please remove this code
    $command = new Command($discord, ['name' => 'ping', 'description' => 'pong']);
    $discord->application->commands->save($command);
});

Alternatively, you can register a command using the Discord\Builders\CommandBuilder class:

use Discord\Builders\CommandBuilder;

$discord->on('ready', function (Discord $discord) {
    // When bot is ready, attempt to create a global slash command "/ping"
    // After the command created successfully in your bot, please remove this code
    $discord->application->commands->save(
        $discord->application->commands->create(CommandBuilder::new()
            ->setName('ping')
            ->setDescription('pong')
            ->toArray()
        )
    );
});

Then:

// Handle the command
$discord->listenCommand('ping', function (Interaction $interaction) {
    // Respond the /ping command with interaction message "pong!"
    $interaction->respondWithMessage(MessageBuilder::new()->setContent('Pong!'));
});

Command is registered:

discordslashcommandping

Handle reply:

discordslashcommandpingresponse

For more information about Interaction, see the documentation.

Note: Command is part of data tied to your Bot Application and Guild, NOT your code. Therefore registering command should be only done once or when the command definition needs to be updated. NOT everytime your bot starts & ready.

Application Command scope

You may notice that your command does not show up in the guild even if it's already registered or showing in the Bot's Direct Message.

In order for your global application command to show up in a guild, you must authorize the Bot using applications.commands scope, the URL can be generated from Discord Developers Portal discordapplicationcommandsscope

And then you can double check the command in Server Settings > Integrations

discordapplicationcommandintegration

Guild Command

Just like global application commands, but you need to save it in the Guild commands repository instead.

$guildCommand = new Command($discord, ['name' => 'ping', 'description' => 'Pong!']);
$guild->commands->save($guildCommand);

Deleting command

To delete a registered command, get the command id by right clicking the command prompt (make sure your developer setting is enabled):

  • Chat input command: Type the command first (e.g. /dphp), select the command from your bot, and right click on the command at top of the input

gambar

  • All command type: Go to Server Settings > Apps: Integrations > Select the bot > below commands, right click on the command you want to remove

gambar

Run the code once (replace 123123123123123123 with the copied Command ID):

  • For Global Command:
$discord->on('ready', function (Discord $discord) {
    // When bot is ready, attempt to remove a global slash command "/blep"
    // After the command deleted successfully in your bot, please remove this code
    $discord->application->commands->delete('123123123123123123')->done();
});
  • For Guild Command:
$guild->application->commands->delete('123123123123123123')->done();

Old Slash Library

If you are previously using DiscordPHP v6.x linked with DiscordPHP-Slash, slash commands are now integrated into the main library. You no longer need the DiscordPHP-Slash library anymore!

Read how to upgrade here: https://github.com/discord-php/DiscordPHP/blob/master/V7_CONVERSION.md#slash-commands

Clone this wiki locally