If you have many commands in your bot, and want to handle commands from multiple sources (for example modern slash commands, and more regular prefixed channel messages) 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_interaction_create 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.
#include <dpp/dpp.h>
int main()
{
bot.on_ready([&command_handler](
const dpp::ready_t &event) {
"ping",
{
},
std::string got_param;
if (!parameters.empty()) {
got_param = std::get<std::string>(parameters[0].second);
}
},
"A test ping command",
819556414099554344
);
});
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:233
The commandhandler class represents a group of commands, prefixed or slash commands with handling fun...
Definition: commandhandler.h:190
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:127
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:164
@ pt_string
String value.
Definition: commandhandler.h:65
Represents the sending source of a command. This is passed to any command handler and should be passe...
Definition: commandhandler.h:137
Represents messages sent and received on Discord.
Definition: message.h:944
Details of a command parameter used in registration. Note that for non-slash commands optional parame...
Definition: commandhandler.h:79
Session ready.
Definition: dispatcher.h:432