-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_example.cpp
More file actions
83 lines (75 loc) · 2.71 KB
/
command_example.cpp
File metadata and controls
83 lines (75 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* D++ multiple command example.
*/
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <map>
#include "command_example/command.h"
#include "command_example/commands/ping.h"
#include "command_example/commands/info.h"
#include "command_example/commands/help.h"
using json = nlohmann::json;
/* We define all our commands here.
* Each command has a name, a description, a command handler function and optionally a
* list of command parameters (each is of type dpp::command_option).
* To demonstrate this example we declare each of these handlers, e.g. handle_ping,
* handle_help and handle_info in separate files (see src/commands folder and
* include/commands folder) with individual header files. You can arrange this however
* you like, or you could even directly put a lambda into this map, but this would
* defeat the point of the example.
*/
std::map<std::string, command_definition> commands = {
{ "ping", { "A ping command", handle_ping }},
{ "help", {
"A help command", handle_help , {
/* One optional string parameter */
{ dpp::command_option(dpp::co_string, "term", "Help term", false) },
}
}},
{ "info", { "An info command", handle_info }},
};
int main()
{
/* Setup the bot, reading token from config.json */
json configdocument;
std::ifstream configfile("../config.json");
configfile >> configdocument;
dpp::cluster bot(configdocument["token"]);
/* Default basic logger */
bot.on_log(dpp::utility::cout_logger());
/* In the on_ready we translate the commands map into a vector of dpp::slashcommand.
* The vector of dpp::slashcommand is then passed to the command registration
* method, dpp::cluster::global_bulk_command_create().
*/
bot.on_ready([&](const dpp::ready_t & event) {
if (dpp::run_once<struct bulk_register>()) {
std::vector<dpp::slashcommand> slash_commands;
for (auto & def : commands) {
dpp::slashcommand c;
c.set_name(def.first).
set_description(def.second.description).
set_application_id(bot.me.id);
c.options = def.second.parameters;
slash_commands.push_back(c);
}
bot.global_bulk_command_create(slash_commands);
}
});
/* Handle slash commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
dpp::command_interaction cmd_data = event.command.get_command_interaction();
/* Check for commands that exist in our command map */
auto command_iter = commands.find(cmd_data.name);
if (command_iter != commands.end()) {
/* If we find a matching command, call its handler function,
* passing in this event's information and the bot cluster
* as references.
*/
command_iter->second.function(bot, event);
}
});
bot.start(false);
}