Debian Codegraph @

docpages/example_code/cache_messages.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2 #include <sstream>
3
4 int main() {
5 /* Create bot */
6 dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); /* Because we're handling messages, we need to use the "i_message_content" intent! */
7
8 /* Create a cache to contain types of dpp::message */
9 dpp::cache<dpp::message> message_cache;
10
11 bot.on_log(dpp::utility::cout_logger());
12
13 /* Message handler */
14 bot.on_message_create([&message_cache](const dpp::message_create_t& event) {
15 /* Make a permanent pointer using new, for each message to be cached */
16 dpp::message* m = new dpp::message();
17
18 /* Store the message into the pointer by copying it */
19 *m = event.msg;
20
21 /* Store the new pointer to the cache using the store() method */
22 message_cache.store(m);
23 });
24
25 /* The event is fired when someone issues your commands */
26 bot.on_slashcommand([&message_cache](const dpp::slashcommand_t& event) {
27 /* Check which command they ran */
28 if (event.command.get_command_name() == "get") {
29
30 dpp::message* find_msg = message_cache.find(std::get<std::string>(event.get_parameter("message_id")));
31
32 /* If find_msg is null, tell the user and return. */
33 if (!find_msg) {
34 event.reply("There is no message cached with this ID");
35 return;
36 }
37
38 event.reply("This message had the following content: " + find_msg->content);
39 }
40 });
41
42 bot.on_ready([&bot](const dpp::ready_t& event) {
43 if (dpp::run_once<struct register_bot_commands>()) {
44
45 /* Create a new command. */
46 dpp::slashcommand newcommand("get", "Get the contents of a message that was cached via an id", bot.me.id);
47
48 /* Add a parameter option. */
49 newcommand.add_option(dpp::command_option(dpp::co_string, "message_id", "The ID of the message you want to find", true));
50
51 /* Register the command */
52 bot.global_command_create(newcommand);
53 }
54 });
55
56 /* Start bot */
57 bot.start(dpp::st_wait);
58
59 return 0;
60 }