- 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);
}
});
});
bot.global_command_create(
dpp::slashcommand(
"button",
"Send a message with a button!", bot.me.id));
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
std::string get_command_name() const
Get the command name for a command interaction.
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
@ ir_update_message
For components, edit the message the component was attached to.
Definition appcommand.h:429
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
interaction command
command interaction
Definition dispatcher.h:789
Session ready.
Definition dispatcher.h:1072
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::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);
});
bot.global_command_create(
dpp::slashcommand(
"button",
"Send a message with a button!", bot.me.id));
}
});
return 0;
}
@ ir_deferred_channel_message_with_source
Acknowledge an interaction and edit a response later, the user sees a loading state.
Definition appcommand.h:419