Skip to content

Commit 6c7afa2

Browse files
add comments
1 parent 3e2a6c7 commit 6c7afa2

File tree

8 files changed

+86
-15
lines changed

8 files changed

+86
-15
lines changed

include/command_example/command.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
#pragma once
22
#include <dpp/dpp.h>
33

4+
/* Definition of our function callback for a command.
5+
* As it uses std::function it can be a function pointer, a lambda,
6+
* or a std::bind to an object method.
7+
* In this example we use function pointers.
8+
* The command functions take two referneces, one to the dpp::cluster,
9+
* and one to the slashcommand event struct.
10+
*/
411
using command_function = std::function<void(dpp::cluster&, const dpp::slashcommand_t&)>;
512

13+
/**
14+
* @brief A definition of a command for the command map
15+
*/
616
struct command_definition {
17+
/**
18+
* @brief Description of the command, must be lower case, no spaces.
19+
*/
720
std::string description;
21+
/**
22+
* @brief Callback function
23+
*/
824
command_function function;
25+
/**
26+
* @brief Parameters of the slash command
27+
*/
928
std::vector<dpp::command_option> parameters = {};
10-
};
29+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#pragma once
22
#include <dpp/dpp.h>
33

4+
/* This is a forward declaration so that command_example.cpp can #include
5+
* this file, and find the handle_help function without including the
6+
* entire source of the command.
7+
*/
48
void handle_help(dpp::cluster& bot, const dpp::slashcommand_t& event);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#pragma once
22
#include <dpp/dpp.h>
33

4+
/* This is a forward declaration so that command_example.cpp can #include
5+
* this file, and find the handle_info function without including the
6+
* entire source of the command.
7+
*/
48
void handle_info(dpp::cluster& bot, const dpp::slashcommand_t& event);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#pragma once
22
#include <dpp/dpp.h>
33

4+
/* This is a forward declaration so that command_example.cpp can #include
5+
* this file, and find the handle_ping function without including the
6+
* entire source of the command.
7+
*/
48
void handle_ping(dpp::cluster& bot, const dpp::slashcommand_t& event);

src/command_example.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515

1616
using json = nlohmann::json;
1717

18+
/* We define all our commands here.
19+
* Each command has a name, a description, a command handler function and optionally a
20+
* list of command parameters (each is of type dpp::command_option).
21+
* To demonstrate this example we declare each of these handlers, e.g. handle_ping,
22+
* handle_help and handle_info in separate files (see src/commands folder and
23+
* include/commands folder) with individual header files. You can arrange this however
24+
* you like, or you could even directly put a lambda into this map, but this would
25+
* defeat the point of the example.
26+
*/
1827
std::map<std::string, command_definition> commands = {
1928
{ "ping", { "A ping command", handle_ping }},
2029
{ "help", {
2130
"A help command", handle_help , {
31+
/* One optional string parameter */
2232
{ dpp::command_option(dpp::co_string, "term", "Help term", false) },
2333
}
2434
}},
@@ -27,37 +37,47 @@ std::map<std::string, command_definition> commands = {
2737

2838
int main()
2939
{
30-
/* Setup the bot */
40+
/* Setup the bot, reading token from config.json */
3141
json configdocument;
3242
std::ifstream configfile("../config.json");
3343
configfile >> configdocument;
34-
3544
dpp::cluster bot(configdocument["token"]);
3645

46+
/* Default basic logger */
3747
bot.on_log(dpp::utility::cout_logger());
3848

49+
/* In the on_ready we translate the commands map into a vector of dpp::slashcommand.
50+
* The vector of dpp::slashcommand is then passed to the command registration
51+
* method, dpp::cluster::global_bulk_command_create().
52+
*/
3953
bot.on_ready([&](const dpp::ready_t & event) {
40-
std::vector<dpp::slashcommand> slash_commands;
41-
for (auto & def : commands) {
42-
dpp::slashcommand c;
43-
c.set_name(def.first).set_description(def.second.description).set_application_id(bot.me.id);
44-
for (auto & param : def.second.parameters) {
45-
c.add_option(param);
54+
if (dpp::run_once<struct bulk_register>()) {
55+
std::vector<dpp::slashcommand> slash_commands;
56+
for (auto & def : commands) {
57+
dpp::slashcommand c;
58+
c.set_name(def.first).
59+
set_description(def.second.description).
60+
set_application_id(bot.me.id);
61+
c.options = def.second.parameters;
62+
slash_commands.push_back(c);
4663
}
47-
slash_commands.push_back(c);
64+
bot.global_bulk_command_create(slash_commands);
4865
}
49-
bot.global_bulk_command_create(slash_commands);
5066
});
5167

68+
/* Handle slash commands */
5269
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
5370
dpp::command_interaction cmd_data = event.command.get_command_interaction();
54-
/* Check for our /dialog command */
71+
/* Check for commands that exist in our command map */
5572
auto command_iter = commands.find(cmd_data.name);
5673
if (command_iter != commands.end()) {
74+
/* If we find a matching command, call its handler function,
75+
* passing in this event's information and the bot cluster
76+
* as references.
77+
*/
5778
command_iter->second.function(bot, event);
5879
}
5980
});
6081

6182
bot.start(false);
6283
}
63-

src/commands/help.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
#include "command_example/command.h"
22
#include "command_example/commands/help.h"
33

4+
/* Example help command.
5+
* Has an optional 'term' parameter that contains a help topic.
6+
*/
47
void handle_help(dpp::cluster& bot, const dpp::slashcommand_t& event) {
58
auto parameter = event.get_parameter("term");
9+
10+
/* Check to see if the parameter is provided by the user */
611
if (std::holds_alternative<std::string>(parameter)) {
12+
13+
/* If it is, get it and check what's in it */
714
std::string search_term = std::get<std::string>(parameter);
815
if (search_term == "ping") {
16+
/* Some reply to help for PING */
917
event.reply("This help left useless as an exercise for the developer.");
1018
} else {
19+
/* Anything, something, so long as the bot replies. */
1120
event.reply("This message intentionally left blank. 🤪");
1221
}
1322
}
23+
/* Help reply */
1424
event.reply(
1525
dpp::message().add_embed(
1626
dpp::embed().
@@ -22,4 +32,4 @@ void handle_help(dpp::cluster& bot, const dpp::slashcommand_t& event) {
2232
add_field("/ping", "Pong?")
2333
)
2434
);
25-
}
35+
}

src/commands/info.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "command_example/command.h"
22
#include "command_example/commands/info.h"
33

4+
/* Example info command
5+
*/
46
void handle_info(dpp::cluster& bot, const dpp::slashcommand_t& event) {
57
event.reply(
68
dpp::message().add_embed(

src/commands/ping.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
#include "command_example/commands/ping.h"
33
#include <dpp/fmt/format.h>
44

5+
/* Example ping command
6+
* Calculates rest + websocket ping
7+
*/
58
void handle_ping(dpp::cluster& bot, const dpp::slashcommand_t& event) {
9+
/* Get websocket ping of first shard */
610
float ws_ping = bot.get_shard(0)->websocket_ping;
11+
12+
/* Ping reply */
713
event.reply(
814
dpp::message().add_embed(
915
dpp::embed().
1016
set_color(dpp::colors::yellow).
1117
set_title("PONG!").
12-
set_description(fmt::format("Ping is: {0:.02f} ms", (bot.rest_ping + ws_ping) * 1000))
18+
set_description(
19+
fmt::format("Ping is: {0:.02f} ms", (bot.rest_ping + ws_ping) * 1000)
20+
)
1321
)
1422
);
1523
}

0 commit comments

Comments
 (0)