Debian Codegraph @

docpages/example_code/editing_message_after_click2.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2 #include <dpp/unicode_emoji.h>
3
4 int main() {
5 dpp::cluster bot("token");
6
7 bot.on_log(dpp::utility::cout_logger());
8
9 /* The event is fired when someone issues your commands */
10 bot.on_slashcommand([](const dpp::slashcommand_t& event) {
11 /* Check which command they ran */
12 if (event.command.get_command_name() == "button") {
13 /* Create a message */
14 dpp::message msg(event.command.channel_id, "this text has a button");
15
16 /* Add an action row, and then a button within the action row. */
17 msg.add_component(
18 dpp::component().add_component(
19 dpp::component()
20 .set_label("Click me!")
21 .set_type(dpp::cot_button)
22 .set_emoji(dpp::unicode_emoji::smile)
23 .set_style(dpp::cos_danger)
24 .set_id("myid")
25 )
26 );
27
28 /* Reply to the user with our message. */
29 event.reply(msg);
30 }
31 });
32
33 /* When a user clicks your button, the on_button_click event will fire,
34 * containing the custom_id you defined in your button.
35 */
36 bot.on_button_click([](const dpp::button_click_t& event) {
37 /* Instead of replying to the button click itself,
38 * we want to update the message that had the buttons on it.
39 */
40 event.reply(dpp::ir_deferred_channel_message_with_source, "");
41
42 /* Pretend you're doing long calls here that may take longer than 3 seconds. */
43
44 /* Now, edit the response! */
45 event.edit_response("After a while, I can confirm you clicked: " + event.custom_id);
46 });
47
48 bot.on_ready([&bot](const dpp::ready_t& event) {
49 if (dpp::run_once<struct register_bot_commands>()) {
50 /* Create and register a command when the bot is ready */
51 bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id));
52 }
53 });
54
55 bot.start(dpp::st_wait);
56
57 return 0;
58 }