D++ (DPP)
C++ Discord API Bot Library
Using a Command Handler Object

If you have many commands in your bot, and want to handle commands from multiple sources, you should consider instantiating a dpp::commandhandler object. This object can be used to automatically route commands and their parameters to functions in your program. A simple example of using this object to route commands is shown below, and will route both the /ping (global slash command) and .ping (prefixed channel message command) to a lambda where a reply can be generated.

Note
This example automatically hooks the dpp::cluster::on_message_create and dpp::cluster::on_slashcommand events. This can be overridden if needed to allow you to still make use of these functions for your own code, if you need to do this please see the constructor documentation for dpp::commandhandler.

Note that because the dpp::commandhandler::add_command method accepts a std::function as the command handler, you may point a command handler at a simple lambda (as shown in this example), a function pointer, or an instantiated class method of an object. This is extremely flexible and allows you to decide how and where commands should be routed, either to an object oriented system or to a lambda based system.

Warning
As of August 30th, 2022, you are advised to only be using slash commands, not messages for commands. To prevent the command handler from handling commands with messages, you should only use the "/" prefix. If you wish to still use messages for commands, this tutorial will still cover it but, again, it is discouraged by Discord.
#include <dpp/dpp.h>
int main() {
/* If your bot only uses the "/" prefix, you can remove the intents here. */
bot.on_log(dpp::utility::cout_logger());
/* Create command handler, and specify prefixes */
/* Specifying a prefix of "/" tells the command handler it should also expect slash commands. Remove the .add_prefix(".") if you wish to only make it a slash command */
command_handler.add_prefix(".")
.add_prefix("/");
bot.on_ready([&command_handler](const dpp::ready_t &event) {
command_handler.add_command(
/* Command name */
"ping",
/* Parameters */
{
{"testparameter", dpp::param_info(dpp::pt_string, true, "Optional test parameter") }
},
/* Command handler */
[&command_handler](const std::string& command, const dpp::parameter_list_t& parameters, dpp::command_source src) {
std::string got_param;
if (!parameters.empty()) {
got_param = std::get<std::string>(parameters[0].second);
}
command_handler.reply(dpp::message("Pong! -> " + got_param), src);
},
/* Command description */
"A test ping command",
/* Guild id (omit for a guild command) */
819556414099554344
);
/* NOTE: We must call this to ensure slash commands are registered.
* This does a bulk register, which will replace other commands
* that are registered already!
*/
command_handler.register_commands();
});
bot.start(dpp::st_wait);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:80
The commandhandler class represents a group of commands, prefixed or slash commands with handling fun...
Definition: commandhandler.h:256
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:112
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition: intents.h:132
std::vector< std::pair< std::string, command_parameter > > parameter_list_t
Parameter list for a called command. See dpp::parameter_registration_t for an explanation as to why v...
Definition: commandhandler.h:161
std::function< void(const std::string &, const parameter_list_t &, command_source)> command_handler
The function definition for a command handler. Expects a command name string, and a list of command p...
Definition: commandhandler.h:224
@ pt_string
String parameter.
Definition: commandhandler.h:76
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:63
Represents the sending source of a command. This is passed to any command handler and should be passe...
Definition: commandhandler.h:172
Represents messages sent and received on Discord.
Definition: message.h:1739
Details of a command parameter used in registration. Note that for non-slash commands optional parame...
Definition: commandhandler.h:114
Session ready.
Definition: dispatcher.h:961
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