Debian Codegraph @

docpages/example_code/callbacks.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2
3 int main() {
4 dpp::cluster bot("Token Was Here", dpp::i_default_intents | dpp::i_message_content);
5 /* the second argument is a bitmask of intents - i_message_content is needed to get messages */
6
7 bot.on_log(dpp::utility::cout_logger());
8
9 /* The event is fired when someone issues your commands */
10 bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
11 if (event.command.get_command_name() == "msgs-get") {
12 int64_t limit = std::get<int64_t>(event.get_parameter("quantity"));
13
14 /* get messages using ID of the channel the command was issued in */
15 bot.messages_get(event.command.channel_id, 0, 0, 0, limit, [event](const dpp::confirmation_callback_t& callback) {
16 if (callback.is_error()) { /* catching an error to log it */
17 std::cout << callback.get_error().message << std::endl;
18 return;
19 }
20
21 auto messages = callback.get<dpp::message_map>();
22 /* std::get<dpp::message_map>(callback.value) would give the same result */
23
24 std::string contents;
25 for (const auto& x : messages) { /* here we iterate through the dpp::message_map we got from callback... */
26 contents += x.second.content + '\n'; /* ...where x.first is ID of the current message and x.second is the message itself. */
27 }
28
29 event.reply(contents); /* we will see all those messages we got, united as one! */
30 });
31 } else if (event.command.get_command_name() == "channel-create") {
32 /* create a text channel */
33 dpp::channel channel = dpp::channel()
34 .set_name("test")
35 .set_guild_id(event.command.guild_id);
36
37 bot.channel_create(channel, [&bot, event](const dpp::confirmation_callback_t& callback) -> void {
38 if (callback.is_error()) { /* catching an error to log it */
39 bot.log(dpp::loglevel::ll_error, callback.get_error().message);
40 return;
41 }
42
43 auto channel = callback.get<dpp::channel>();
44 /* std::get<dpp::channel>(callback.value) would give the same result */
45
46 /* reply with the created channel information */
47 dpp::message message = dpp::message("The channel's name is `" + channel.name + "`, ID is `" + std::to_string(channel.id) + " and type is `" + std::to_string(channel.get_type()) + "`.");
48 /* note that channel types are represented as numbers */
49 event.reply(message);
50 });
51 } else if (event.command.get_command_name() == "msg-error") {
52 bot.message_get(0, 0, [event](const dpp::confirmation_callback_t& callback) -> void {
53 /* the error will occur since there is no message with ID '0' that is in a channel with ID '0' (I'm not explaining why) */
54 if (callback.is_error()) {
55 event.reply(callback.get_error().message);
56 return;
57 }
58
59 /* we won't be able to get here because of the return; statement */
60 auto message = callback.get<dpp::message>();
61 event.reply(message);
62 });
63 }
64 });
65
66 bot.on_ready([&bot](const dpp::ready_t& event) {
67 if (dpp::run_once <struct register_global_commands>()) {
68 dpp::slashcommand msgs_get("msgs-get", "Get messages", bot.me.id);
69
70 constexpr int64_t min_val{1};
71 constexpr int64_t max_val{100};
72
73 msgs_get.add_option(
74 dpp::command_option(dpp::co_integer, "quantity", "Quantity of messages to get. Max - 100.")
75 .set_min_value(min_val)
76 .set_max_value(max_val)
77 );
78
79 dpp::slashcommand channel_create("channel-create", "Create a channel", bot.me.id);
80 dpp::slashcommand msg_error("msg-error", "Get an error instead of message :)", bot.me.id);
81
82 bot.global_bulk_command_create({ msgs_get, channel_create, msg_error });
83 }
84 });
85
86 bot.start(dpp::st_wait);
87
88 return 0;
89 }