D++ (DPP)
C++ Discord API Bot Library
Using Slash Commands and Interactions

Slash commands and interactions are a newer feature of Discord which allow bot's commands to be registered centrally within the system and for users to easily explore and get help with available commands through the client itself.

To add a slash command you should use the dpp::cluster::global_command_create method for global commands (available to all guilds) or dpp::cluster::guild_command_create to create a local command (available only to one guild).

When a user issues these commands the reply will arrive via the on_interaction_create event which you can hook, and take action when you see your commands. It is possible to reply to an interaction by using either the dpp::interaction_create_t::reply method, or by manually instantiating an object of type dpp::interaction_response and attaching a dpp::message object to it.

dpp::interaction_create_t::reply has two overloaded versions of the method, one of which accepts simple std::string replies, for basic text-only messages (if your message is 'ephemeral' you must use this) and one which accepts a dpp::message for more advanced replies. Please note that at present, Discord only supports a small subset of message and embed features within an interaction response object.

Note
You can also use the unified command handler, which lets you combine channel based message commands and slash commands under the same lambda with the same code like they were one and the same. Note that after April of 2022 Discord will be discouraging bots from using commands that are prefixed messages via means of a privileged message intent. It is advised that you exclusively use slash commands, or the unified handler with only a prefix of "/" going forward for any new bots you create and look to migrating existing bots to this setup.
#include <dpp/dpp.h>
#include <dpp/fmt/format.h>
int main()
{
dpp::cluster bot("token");
/* The interaction create event is fired when someone issues your commands */
bot.on_interaction_create([&bot](const dpp::interaction_create_t & event) {
dpp::command_interaction cmd_data = std::get<dpp::command_interaction>(event.command.data);
/* Check which command they ran */
if (cmd_data.name == "blep") {
/* Fetch a parameter value from the command parameters */
std::string animal = std::get<std::string>(event.get_parameter("animal"));
/* Reply to the command. There is an overloaded version of this
* call that accepts a dpp::message so you can send embeds.
*/
event.reply(dpp::ir_channel_message_with_source, fmt::format("Blep! You chose {}", animal));
}
}
});
bot.on_ready([&bot](const dpp::ready_t & event) {
dpp::slashcommand newcommand;
/* Create a new global command on ready event */
newcommand.set_name("blep")
.set_description("Send a random adorable animal photo")
.set_application_id(bot.me.id)
dpp::command_option(dpp::co_string, "animal", "The type of animal", true).
add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))).
add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))).
add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin")
)
)
);
/* Register the command */
bot.global_command_create(newcommand);
});
bot.start(false);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:384
uint8_t type
the type of interaction
Definition: appcommand.h:563
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:679
slashcommand & add_option(const command_option &o)
Add an option (parameter)
slashcommand & set_name(const std::string &n)
Set the name of the command.
@ it_application_command
application command (slash command)
Definition: appcommand.h:459
@ co_string
Definition: appcommand.h:47
This struct represents choices in a multiple choice option for a command parameter....
Definition: appcommand.h:80
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: appcommand.h:121
Create interaction.
Definition: dispatcher.h:259
interaction command
command interaction
Definition: dispatcher.h:351
Session ready.
Definition: dispatcher.h:552
D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0