diff --git a/docpages/example_programs/using_coroutines/awaiting_events.md b/docpages/example_programs/using_coroutines/awaiting_events.md index 9008f4803b..b338f8934a 100644 --- a/docpages/example_programs/using_coroutines/awaiting_events.md +++ b/docpages/example_programs/using_coroutines/awaiting_events.md @@ -18,13 +18,21 @@ int main() { 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( - dpp::component{}.add_component(dpp::component{}.set_type(dpp::cot_button).set_label("Click me!").set_id(event.command.id.str())) + dpp::component{}.add_component(dpp::component{}.set_type(dpp::cot_button).set_label("Click me!").set_id(id)) ); co_await event.co_reply(m); - dpp::button_click_t click_event = co_await event.from->creator->on_button_click.when([&id](dpp::button_click_t const &b) { return b.custom_id == id; }); - event.edit_original_response(m.set_content("You clicked the button!")); + 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 + click_event.reply(dpp::ir_deferred_update_message, dpp::message{}); + event.edit_original_response(dpp::message{"You clicked the button!"}); } }); diff --git a/docpages/example_programs/using_coroutines/expiring_buttons.md b/docpages/example_programs/using_coroutines/expiring_buttons.md index dd171cc6e9..50af6e01dd 100644 --- a/docpages/example_programs/using_coroutines/expiring_buttons.md +++ b/docpages/example_programs/using_coroutines/expiring_buttons.md @@ -16,8 +16,9 @@ int main() { 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( - dpp::component{}.add_component(dpp::component{}.set_type(dpp::cot_button).set_label("Click me!").set_id(event.command.id.str())) + dpp::component{}.add_component(dpp::component{}.set_type(dpp::cot_button).set_label("Click me!").set_id(id)) ); co_await event.co_reply(m); @@ -25,10 +26,16 @@ int main() { event.from->creator->on_button_click.when([&id](const dpp::button_click_t &b) { return b.custom_id == id; }), // Button clicked event.from->creator->co_sleep(10) // Or sleep 10 seconds }; - if (result.index() == 0) // Awaitable #0 completed first, that is the button click event - event.edit_original_response(m.set_content("You clicked the button with the id " + result.get<0>().custom_id)); - else // Here index() is 1 + // Note!! Due to a bug in g++11 and g++12, id must be captured as a reference above or the compiler will destroy it twice. This is fixed in g++13 + if (result.index() == 0) { // Awaitable #0 completed first, that is the button click event + // Acknowledge the click with an empty message and edit the original response, removing the button + auto &click_event = result.get<0>(); + click_event.reply(dpp::ir_deferred_update_message, dpp::message{}); + event.edit_original_response(m.set_content("You clicked the button with the id " + click_event.custom_id)); + } + else { // Here index() is 1, the timer expired event.edit_original_response(dpp::message{"I haven't got all day..."}); + } } }); diff --git a/include/dpp/coro/when_any.h b/include/dpp/coro/when_any.h index 19229f9d4e..96952c17e7 100644 --- a/include/dpp/coro/when_any.h +++ b/include/dpp/coro/when_any.h @@ -425,6 +425,9 @@ class when_any { * This object will swallow the exception and return cleanly. Any other exception will be thrown back to the resumer. */ ~when_any() { + if (!my_state) + return; + my_state->owner_state = detail::when_any::await_state::dangling; [](when_any *self, std::index_sequence) constexpr { diff --git a/include/dpp/event_router.h b/include/dpp/event_router.h index 6b4787b98d..f6dd3b4664 100644 --- a/include/dpp/event_router.h +++ b/include/dpp/event_router.h @@ -421,10 +421,10 @@ template class event_router_t { #endif auto when(Predicate&& pred) #ifndef _DOXYGEN_ - noexcept(noexcept(std::function{pred})) + noexcept(noexcept(std::function{std::declval()})) #endif { - return detail::event_router::awaitable{this, pred}; + return detail::event_router::awaitable{this, std::forward(pred)}; } /**