Debian Codegraph @

docpages/example_code/editing_messages1.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2
3 int main() {
4 /* the second argument is a bitmask of intents - i_message_content is needed to get messages */
5 dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content);
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() == "msg-send") {
12 event.reply("That's a message");
13 } else if (event.command.get_command_name() == "msg-edit") {
14 const auto content = std::get<std::string>(event.get_parameter("content"));
15
16 /* get the message to edit it after. here string will automatically be converted to snowflake */
17 const dpp::snowflake msg_id = std::get<std::string>(event.get_parameter("msg-id"));
18
19 bot.message_get(msg_id, event.command.channel_id, [&bot, content, event](const dpp::confirmation_callback_t& callback) {
20 if (callback.is_error()) {
21 event.reply("error");
22 return;
23 }
24 auto message = callback.get<dpp::message>();
25
26 /* change the message content and edit the message itself */
27 message.set_content(content);
28 bot.message_edit(message);
29 event.reply("Message content is now `" + content + "`.");
30 });
31 }
32 });
33
34 bot.on_ready([&bot](const dpp::ready_t& event) {
35 if (dpp::run_once <struct register_global_commands>()) {
36 dpp::slashcommand msg_edit("msg-edit", "Edit a message sent by the bot", bot.me.id);
37
38 msg_edit.add_option(dpp::command_option(dpp::co_string, "msg-id", "ID of the message to edit", true)); /* true for required option */
39 msg_edit.add_option(dpp::command_option(dpp::co_string, "content", "New content for the message", true)); /* same here */
40
41 dpp::slashcommand msg_send("msg-send", "Send my message", bot.me.id);
42
43 bot.global_bulk_command_create({ msg_edit, msg_send });
44 }
45 });
46
47 bot.start(dpp::st_wait);
48 return 0;
49 }