D++ (DPP)
10.0.30
C++ Discord API Bot Library
|
|
- 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.
In the last example we've explored how to await events using coroutines, we ran into the problem of the coroutine waiting forever if the button was never clicked. Wouldn't it be nice if we could add an "or" to our algorithm, for example wait for the button to be clicked or for a timer to expire? I'm glad you asked! D++ offers when_any which allows exactly that. It is a templated class that can take any number of awaitable objects and can be co_await
-ed itself, will resume when the first awaitable completes and return a result object that allows to retrieve which awaitable completed as well as its result, in a similar way as std::variant.
dpp::message m{"Test"};
std::string id{event.command.id.str()};
)
);
event.from->creator->on_button_click.when([&id](const dpp::button_click_t &b) {
return b.custom_id == id;
}),
event.from->creator->co_sleep(5)
};
if (result.index() == 0) {
event.edit_original_response(
dpp::message{
"You clicked the button with the id " + click_event.
custom_id});
} else {
event.edit_original_response(
dpp::message{
"I haven't got all day!"});
}
}
});
if (dpp::run_once<struct register_bot_commands>()) {
bot.global_command_create(command);
}
});
return 0;
}
Any awaitable can be used with when_any, even dpp::task, dpp::coroutine, dpp::async. When the when_any object is destroyed, any of its awaitables with a cancel() method (for example dpp::task) will have it called. With this you can easily make commands that ask for input in several steps, or maybe a timed text game, the possibilities are endless! Note that if the first awaitable completes with an exception, result.get will throw it.
- Note
- when_any will try to construct awaitable objects from the parameter you pass it, which it will own. In practice this means you can only pass it temporary objects (rvalues) as most of the coroutine-related objects in D++ are move-only.
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...
Definition: dispatcher.cpp:212
snowflake id
Unique ID of object set by Discord.
Definition: managed.h:79
component & set_type(component_type ct)
Set the type of the component.
Definition: message.cpp:145
User has issued a slash command.
Definition: dispatcher.h:715
@ st_wait
Wait forever on a condition variable.
Definition: cluster.h:101
A coroutine task. It starts immediately on construction and can be co_await-ed, making it perfect for...
Definition: coro/coro.h:166
int main()
Definition: soak.cpp:28
Represents messages sent and received on Discord.
Definition: message.h:2007
component & set_id(const std::string &id)
Set the id of the component.
Definition: message.cpp:207
interaction command
command interaction
Definition: dispatcher.h:698
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout.
Definition: dispatcher.h:228
component & set_label(const std::string &label)
Set the label of the component, e.g.
Definition: message.cpp:161
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:1358
Experimental class to co_await on a bunch of awaitable objects, resuming when the first one completes...
Definition: when_any.h:170
component & add_component(const component &c)
Add a sub-component, only valid for action rows.
Definition: message.cpp:130
std::string get_command_name() const
Get the command name for a command interaction.
Definition: slashcommand.cpp:487
void reply(command_completion_event_t callback=utility::log_error()) const
Acknowledge interaction without displaying a message to the user, for use with button and select menu...
Definition: dispatcher.cpp:152
@ cot_button
Clickable button.
Definition: message.h:86
Represents the component object.
Definition: message.h:360
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:99
constexpr const char m[]
Definition: unicode_emoji.h:5304
event_router_t< log_t > on_log
Called when a log message is to be written to the log.
Definition: cluster.h:481
Session ready.
Definition: dispatcher.h:981