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.
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.
dpp::message m{"Test"};
std::string id{event.command.id.str()};
)
);
return b.custom_id == id;
}
);
click_event.reply();
}
});
if (dpp::run_once<struct register_bot_commands>()) {
bot.global_command_create(command);
}
});
return 0;
}
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.
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
event_router_t< button_click_t > on_button_click
Called when a button is clicked attached to a message.
Definition: cluster.h:525
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
void edit_original_response(const message &m, command_completion_event_t callback=utility::log_error()) const
Edit original response message for this interaction.
Definition: dispatcher.cpp:184
@ st_wait
Wait forever on a condition variable.
Definition: cluster.h:101
class dpp::cluster * creator
Owning cluster.
Definition: discordclient.h:275
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
discord_client * from
Shard the event came from.
Definition: dispatcher.h:110
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
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
@ 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 b[]
Definition: unicode_emoji.h:5255
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