Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Aug 30, 2023
1 parent efaca69 commit a81951a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
14 changes: 11 additions & 3 deletions docpages/example_programs/using_coroutines/awaiting_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"});
}
});

Expand Down
15 changes: 11 additions & 4 deletions docpages/example_programs/using_coroutines/expiring_buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ 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);

auto result = co_await dpp::when_any{ // Whichever completes first...
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..."});
}
}
});

Expand Down
3 changes: 3 additions & 0 deletions include/dpp/coro/when_any.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

[]<size_t... Ns>(when_any *self, std::index_sequence<Ns...>) constexpr {
Expand Down
4 changes: 2 additions & 2 deletions include/dpp/event_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ template<class T> class event_router_t {
#endif
auto when(Predicate&& pred)
#ifndef _DOXYGEN_
noexcept(noexcept(std::function<bool(const T&)>{pred}))
noexcept(noexcept(std::function<bool(const T&)>{std::declval<Predicate>()}))
#endif
{
return detail::event_router::awaitable<T>{this, pred};
return detail::event_router::awaitable<T>{this, std::forward<Predicate>(pred)};
}

/**
Expand Down

0 comments on commit a81951a

Please sign in to comment.