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 avaialble 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
- A later version of the library will include a built in command handler system which will be able to integrate with slash commands and interactions, making this process even more seamless.
#include <dpp/dpp.h>
#include <dpp/fmt/format.h>
int main()
{
dpp::command_interaction cmd_data = std::get<dpp::command_interaction>(event.command.data);
if (cmd_data.name == "blep") {
std::string animal = std::get<std::string>(event.get_parameter("animal"));
event.reply(dpp::ir_channel_message_with_source, fmt::format("Blep! You chose {}", animal));
}
}
});
.set_description("Send a random adorable animal photo")
.set_application_id(bot.me.id)
)
)
);
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:216
uint8_t type
the type of interaction
Definition: slashcommand.h:317
Represents an application command, created by your bot either globally, or on a guild.
Definition: slashcommand.h:415
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: slashcommand.h:260
@ co_string
Definition: slashcommand.h:39
This struct represents choices in a multiple choice option for a command parameter....
Definition: slashcommand.h:66
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: slashcommand.h:102
Create interaction.
Definition: dispatcher.h:135
interaction command
Definition: dispatcher.h:182
Session ready.
Definition: dispatcher.h:279