Debian Codegraph @

docpages/example_code/coro_awaiting_events.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2
3 int main() {
4 dpp::cluster bot{"token"};
5
6 bot.on_log(dpp::utility::cout_logger());
7
8 bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> {
9 if (event.command.get_command_name() == "test") {
10 // Make a message and add a button with its custom ID set to the command interaction's ID so we can identify it
11 dpp::message m{"Test"};
12 std::string id{event.command.id.str()};
13 m.add_component(
14 dpp::component{}.add_component(
15 dpp::component{}
16 .set_type(dpp::cot_button)
17 .set_label("Click me!")
18 .set_id(id)
19 )
20 );
21 co_await event.co_reply(m);
22
23 dpp::button_click_t click_event = co_await event.owner->on_button_click.when(
24 // 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
25 [&id] (dpp::button_click_t const &b) {
26 return b.custom_id == id;
27 }
28 );
29 // Acknowledge the click and edit the original response, removing the button
30 click_event.reply();
31 event.edit_original_response(dpp::message{"You clicked the button!"});
32 }
33 });
34
35 bot.on_ready([&bot](const dpp::ready_t& event) {
36 if (dpp::run_once<struct register_bot_commands>()) {
37 dpp::slashcommand command{"test", "Test awaiting for an event", bot.me.id};
38
39 bot.global_command_create(command);
40 }
41 });
42
43 bot.start(dpp::st_wait);
44
45 return 0;
46 }