D++ (DPP)
C++ Discord API Bot Library
Waiting for events

Warning
D++ Coroutines are a very new feature and are currently only supported by D++ on g++ 11, clang/LLVM 14, and MSVC 19.37 or above. Additionally, D++ must be built with the CMake option DPP_CORO, and your program must both define the macro DPP_CORO and use C++20 or above. The feature is experimental and may have bugs or even crashes, please report any to GitHub Issues or to our Discord Server.

D++ makes it possible to await events: simple use co_await on any of the event routers, such as on_message_create, and your coroutine will be suspended until the next event fired by this event router. You can also co_await the return of an event router's when() method while passing it a predicate function object, it will only resume your coroutine when the predicate returns true. Be aware that your coroutine is attached to the event router only when you call co_await and not before, and will be detached as it is resumed.

Note
When the event router resumes your coroutine, it will give you a reference to the event object. This will likely mean it will be destroyed after your next co_await, make sure to save it in a local variable if you need it for longer.
#include <dpp/dpp.h>
int main() {
bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job {
if (event.command.get_command_name() == "test") {
// Make a message and add a button with its custom ID set to the command interaction's ID so we can identify it
dpp::message m{"Test"};
std::string id{event.command.id.str()};
m.add_component(
);
co_await event.co_reply(m);
dpp::button_click_t click_event = co_await event.from->creator->on_button_click.when(
// Note!! Due to a bug in g++11 and g++12, id must be captured as a reference here or the compiler will destroy it twice. This is fixed in g++13
[&id] (dpp::button_click_t const &b) {
return b.custom_id == id;
}
);
// Acknowledge the click with an empty message and edit the original response, removing the button
event.edit_original_response(dpp::message{"You clicked the button!"});
}
});
bot.on_ready([&bot](const dpp::ready_t & event) {
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand command{"test", "Test awaiting for an event", bot.me.id};
bot.global_command_create(command);
}
});
bot.start(dpp::st_wait);
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:82
event_router_t< button_click_t > on_button_click
Called when a button is clicked attached to a message. Button clicks are triggered by discord when bu...
Definition: cluster.h:508
Represents the component object. A component is a clickable button or drop down list within a discord...
Definition: message.h:221
component & set_id(const std::string &id)
Set the id of the component. For action rows, this field is ignored. Setting the id will auto-set the...
component & set_label(const std::string &label)
Set the label of the component, e.g. button text. For action rows, this field is ignored....
component & set_type(component_type ct)
Set the type of the component. Button components (type dpp::cot_button) should always be contained wi...
component & add_component(const component &c)
Add a sub-component, only valid for action rows. Adding subcomponents to a component will automatical...
class dpp::cluster * creator
Owning cluster.
Definition: discordclient.h:277
std::string get_command_name() const
Get the command name for a command interaction.
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition: managed.h:39
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:999
constexpr const char m[]
Definition: unicode_emoji.h:5117
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ i_message_content
Intent for receipt of message content.
Definition: intents.h:65
@ i_default_intents
Default D++ intents (all non-privileged intents)
Definition: intents.h:73
@ ir_deferred_update_message
for components, acknowledge an interaction and edit the original message later; the user does not see...
Definition: appcommand.h:295
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:65
@ cot_button
Clickable button.
Definition: message.h:42
Click on button.
Definition: dispatcher.h:619
class discord_client * from
Shard the event came from. Note that for some events, notably voice events, this may be nullptr.
Definition: dispatcher.h:83
void edit_original_response(const message &m, command_completion_event_t callback=utility::log_error()) const
Edit original response message for this interaction.
interaction command
command interaction
Definition: dispatcher.h:585
dpp::async< dpp::confirmation_callback_t > co_reply() const
Acknowledge interaction without displaying a message to the user, for use with button and select menu...
Extremely light coroutine object designed to send off a coroutine to execute on its own....
Definition: job.h:46
Represents messages sent and received on Discord.
Definition: message.h:1180
Session ready.
Definition: dispatcher.h:877
User has issued a slash command.
Definition: dispatcher.h:607

Note that there is a problem with that! If the user never clicks your button, or if the message gets deleted, your coroutine will be stuck waiting... And waiting... Forever until your bot shuts down, occupying a space in memory. This is where the next example comes into play as a solution, with a button that expires with time.

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0