Debian Codegraph @

docpages/example_code/commandhandler.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2
3 int main() {
4 /* If your bot only uses the "/" prefix, you can remove the intents here. */
5 dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
6
7 bot.on_log(dpp::utility::cout_logger());
8
9 /* Create command handler, and specify prefixes */
10 dpp::commandhandler command_handler(&bot);
11 /* 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 */
12 command_handler.add_prefix(".")
13 .add_prefix("/");
14
15 bot.on_ready([&command_handler](const dpp::ready_t& event) {
16
17 command_handler.add_command(
18 /* Command name */
19 "ping",
20
21 /* Parameters */
22 {
23 {"testparameter", dpp::param_info(dpp::pt_string, true, "Optional test parameter") }
24 },
25
26 /* Command handler */
27 [&command_handler](const std::string& command, const dpp::parameter_list_t& parameters, dpp::command_source src) {
28 std::string got_param;
29 if (!parameters.empty()) {
30 got_param = std::get<std::string>(parameters[0].second);
31 }
32 command_handler.reply(dpp::message("Pong! -> " + got_param), src);
33 },
34
35 /* Command description */
36 "A test ping command",
37
38 /* Guild id (omit for a global command) */
39 819556414099554344
40 );
41
42 /* NOTE: We must call this to ensure slash commands are registered.
43 * This does a bulk register, which will replace other commands
44 * that are registered already!
45 */
46 command_handler.register_commands();
47
48 });
49
50 bot.start(dpp::st_wait);
51
52 return 0;
53 }