- Note
- This page expects you to be familiar with Button Clicks and extends from the Using Button Components page. Please visit the Using Button Components page if you are not already familiar with Button Clicks.
Editing messages where you had a button click can be done in a couple ways.
If you want to edit the message that had the buttons on, instead of doing event.reply("message");
, you would do event.reply(dpp::ir_update_message, "message");
, like so:
- Note
- You are still limited to the default interaction time (3 seconds) this way. Read on if you need more time!
#include <dpp/dpp.h>
#include <dpp/unicode_emoji.h>
int main() {
dpp::message msg(event.command.channel_id, "this text has a button");
msg.add_component(
dpp::component().add_component(
dpp::component()
.set_label("Click me!")
.set_type(dpp::cot_button)
.set_emoji(dpp::unicode_emoji::smile)
.set_style(dpp::cos_danger)
.set_id("myid")
)
);
event.reply(msg);
}
});
});
if (dpp::run_once<struct register_bot_commands>()) {
bot.global_command_create(
dpp::slashcommand(
"button",
"Send a message with a button!", bot.me.id));
}
});
return 0;
}
However, if you're going to take longer than 3 seconds to respond, you need to tell Discord to wait. The usual method is event.thinking(true);
and then event.edit_response("I have thought long and hard about my actions.");
, however, event.thinking()
will create a new response if called from on_button_click
, meaning you can no longer respond to the original response as you already did a response!
Instead, you want to do event.reply(dpp::ir_deferred_channel_message_with_source, "");
to tell Discord that you intend on editing the message that the button click came from, but you need time. The user will be informed that you've processed the button click (as required) via a loading state and then you have 15 minutes to do everything you need. To then edit the message, you need to do event.edit_response("new message!");
, like so:
- Note
- If you want to silently acknowledge the button click (no thinking message), replace dpp::ir_deferred_channel_message_with_source with dpp::ir_deferred_update_message. You will still have 15 minutes to make a response.
#include <dpp/dpp.h>
#include <dpp/unicode_emoji.h>
int main() {
dpp::message msg(event.command.channel_id, "this text has a button");
msg.add_component(
dpp::component().add_component(
dpp::component()
.set_label("Click me!")
.set_type(dpp::cot_button)
.set_emoji(dpp::unicode_emoji::smile)
.set_style(dpp::cos_danger)
.set_id("myid")
)
);
event.reply(msg);
}
});
event.edit_response(
"After a while, I can confirm you clicked: " + event.
custom_id);
});
if (dpp::run_once<struct register_bot_commands>()) {
bot.global_command_create(
dpp::slashcommand(
"button",
"Send a message with a button!", bot.me.id));
}
});
return 0;
}