| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
Editing The Message From a Button Click

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::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "button") {
/* Create a message */
dpp::message msg(event.command.channel_id, "this text has a button");
/* Add an action row, and then a button within the action row. */
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")
)
);
/* Reply to the user with our message. */
event.reply(msg);
}
});
/* When a user clicks your button, the on_button_click event will fire,
* containing the custom_id you defined in your button.
*/
bot.on_button_click([](const dpp::button_click_t& event) {
/* Instead of replying to the button click itself,
* we want to update the message that had the buttons on it.
*/
event.reply(dpp::ir_update_message, "You clicked: " + event.custom_id);
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::component
Represents the component object. A component is a clickable button or drop down list within a discord...
Definition message.h:497
dpp::interaction::channel_id
snowflake channel_id
Optional: the channel it was sent from.
Definition appcommand.h:1056
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1449
dpp::unicode_emoji::smile
constexpr const char smile[]
Definition unicode_emoji.h:1604
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::ir_update_message
@ ir_update_message
For components, edit the message the component was attached to.
Definition appcommand.h:442
dpp::cos_danger
@ cos_danger
Red; danger.
Definition message.h:243
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::cot_button
@ cot_button
Clickable button.
Definition message.h:48
dpp::button_click_t
Click on button.
Definition dispatcher.h:814
dpp::button_click_t::custom_id
std::string custom_id
button custom id
Definition dispatcher.h:824
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::message
Represents messages sent and received on Discord.
Definition message.h:2363
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806

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::cluster bot("token");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "button") {
/* Create a message */
dpp::message msg(event.command.channel_id, "this text has a button");
/* Add an action row, and then a button within the action row. */
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")
)
);
/* Reply to the user with our message. */
event.reply(msg);
}
});
/* When a user clicks your button, the on_button_click event will fire,
* containing the custom_id you defined in your button.
*/
bot.on_button_click([](const dpp::button_click_t& event) {
/* Instead of replying to the button click itself,
* we want to update the message that had the buttons on it.
*/
event.reply(dpp::ir_deferred_channel_message_with_source, "");
/* Pretend you're doing long calls here that may take longer than 3 seconds. */
/* Now, edit the response! */
event.edit_response("After a while, I can confirm you clicked: " + event.custom_id);
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id));
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::ir_deferred_channel_message_with_source
@ ir_deferred_channel_message_with_source
Acknowledge an interaction and edit a response later, the user sees a loading state.
Definition appcommand.h:432
D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0