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_slashcommand
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 August 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>
int main()
{
std::string animal = std::get<std::string>(event.get_parameter("animal"));
event.reply(std::string("Blep! You chose") + animal);
}
});
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand newcommand(
"blep",
"Send a random adorable animal photo", bot.me.id);
newcommand.add_option(
)
)
);
bot.global_command_create(newcommand);
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:276
std::string get_command_name() const
Get the command name for a command interaction.
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:988
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ co_string
Definition: appcommand.h:55
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:259
This struct represents choices in a multiple choice option for a command parameter....
Definition: appcommand.h:93
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: appcommand.h:154
interaction command
command interaction
Definition: dispatcher.h:488
Session ready.
Definition: dispatcher.h:783
User has issued a slash command.
Definition: dispatcher.h:499
- Note
- For demonstration purposes, and small bots, this code is OK, but in the real world once your bot gets big, it's not recommended to create slash commands in the
on_ready
event because it gets called often (discord forces reconnections and sometimes these do not resume). You could for example add a commandline parameter to your bot (argc
, argv
) so that if you want the bot to register commands it must be launched with a specific command line argument.