Skip to content

Commit

Permalink
docs(coro): update docs to reflect changes to event routers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Oct 20, 2023
1 parent 25ed803 commit a48a2c2
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docpages/example_code/coro_awaiting_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main() {

bot.on_log(dpp::utility::cout_logger());

bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job {
bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> {
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"};
Expand All @@ -32,7 +32,7 @@ int main() {
}
});

bot.on_ready([&bot](const dpp::ready_t & event) {
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand command{"test", "Test awaiting for an event", bot.me.id};

Expand Down
4 changes: 2 additions & 2 deletions docpages/example_code/coro_expiring_buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main() {

bot.on_log(dpp::utility::cout_logger());

bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job {
bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> {
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"};
Expand Down Expand Up @@ -38,7 +38,7 @@ int main() {
}
});

bot.on_ready([&bot](const dpp::ready_t & event) {
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand command{"test", "Test awaiting for an event", bot.me.id};

Expand Down
3 changes: 1 addition & 2 deletions docpages/example_code/coro_intro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ int main() {
bot.on_log(dpp::utility::cout_logger());

/* The event is fired when someone issues your commands */
/* Make note of passing the event by value, this is important (explained below) */
bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job {
bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> {
if (event.command.get_command_name() == "file") {
/* Request the image from the URL specified and co_await the response */
dpp::http_request_completion_t result = co_await event.from->creator->co_request("https://dpp.dev/DPP-Logo.png", dpp::m_get);
Expand Down
4 changes: 2 additions & 2 deletions docpages/example_code/coro_simple_commands1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main() {

bot.on_log(dpp::utility::cout_logger());

bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job {
bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> {
if (event.command.get_command_name() == "addemoji") {
dpp::cluster *cluster = event.from->creator;
// Retrieve parameter values
Expand Down Expand Up @@ -48,7 +48,7 @@ int main() {
}
});

bot.on_ready([&bot](const dpp::ready_t & event) {
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand command("addemoji", "Add an emoji", bot.me.id);
// Add file and name as required parameters
Expand Down
4 changes: 2 additions & 2 deletions docpages/example_code/coro_simple_commands2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main() {

bot.on_log(dpp::utility::cout_logger());

bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job {
bot.on_slashcommand([](const dpp::slashcommand_t& event) -> dpp::task<void> {
if (event.command.get_command_name() == "avatar") {
// Make a nested coroutine to fetch the guild member requested, that returns it as an optional
constexpr auto resolve_member = [](const dpp::slashcommand_t &event) -> dpp::task<std::optional<dpp::guild_member>> {
Expand Down Expand Up @@ -70,7 +70,7 @@ int main() {
});


bot.on_ready([&bot](const dpp::ready_t & event) {
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand command("avatar", "Get your or another user's avatar image", bot.me.id);
command.add_option(dpp::command_option(dpp::co_user, "user", "User to fetch the avatar from"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Let's revisit \ref attach-file "attaching a downloaded file", but this time with

Coroutines can make commands simpler by eliminating callbacks, which can be very handy in the case of complex commands that rely on a lot of different data or steps.

In order to be a coroutine, a function has to return a special type with special functions; D++ offers dpp::job, dpp::task, and dpp::coroutine, which are designed to work seamlessly with asynchronous calls through dpp::async, which all the functions starting with `co_` such as dpp::cluster::co_message_create return. Event routers can have a dpp::job attached to them, as this object allows to create coroutines that can execute on their own, asynchronously. More on that and the difference between it and the other two types later. To turn a function into a coroutine, simply make it return dpp::job as seen in the example at line 10, then use `co_await` on awaitable types or `co_return`. The moment the execution encounters one of these two keywords, the function is transformed into a coroutine. Coroutines that use dpp::job can be used for event handlers, they can be attached to an event router just the same way as regular event handlers.
In order to be a coroutine, a function has to return a special type with special functions; D++ offers dpp::job, dpp::task, and dpp::coroutine, which are designed to work seamlessly with asynchronous calls through dpp::async, which all the functions starting with `co_` such as dpp::cluster::co_message_create return. Event routers can have a dpp::task coroutine attached to them, as this object allows to create coroutines that can execute on their own, asynchronously. More on that and the difference between it and the other two types later. To turn a function into a coroutine, simply make it return dpp::task<void> as seen in the example at line 10, then use `co_await` on awaitable types or `co_return`. The moment the execution encounters one of these two keywords, the function is transformed into a coroutine. Coroutines that use dpp::task<void> can be used for event handlers, they can be attached to an event router just the same way as regular event handlers.

When using a `co_*` function such as `co_message_create`, the request is sent immediately and the returned dpp::async can be `co_await`-ed, at which point the coroutine suspends (pauses) and returns back to its caller; in other words, the program is free to go and do other things while the data is being retrieved and D++ will resume your coroutine when it has the data you need, which will be returned from the `co_await` expression.

Expand Down
22 changes: 11 additions & 11 deletions include/dpp/event_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ template<class T> class event_router_t {
#ifdef _DOXYGEN_
/**
* @brief Attach a callable to the event, adding a listener.
* The callable should either be of the form `void(const T &)` or
* `dpp::job(T)` (the latter requires DPP_CORO to be defined),
* The callable should either be of the form `void(const T&)` or
* `dpp::task<void>(const T&)` (the latter requires DPP_CORO to be defined),
* where T is the event type for this event router.
*
* This has the exact same behavior as using \ref attach(F&&) "attach".
Expand All @@ -576,8 +576,8 @@ template<class T> class event_router_t {

/**
* @brief Attach a callable to the event, adding a listener.
* The callable should either be of the form `void(const T &)` or
* `dpp::job(T)` (the latter requires DPP_CORO to be defined),
* The callable should either be of the form `void(const T&)` or
* `dpp::task<void>(const T&)` (the latter requires DPP_CORO to be defined),
* where T is the event type for this event router.
*
* @param fun Callable to attach to event
Expand All @@ -590,8 +590,8 @@ template<class T> class event_router_t {
# ifdef DPP_CORO
/**
* @brief Attach a callable to the event, adding a listener.
* The callable should either be of the form `void(const T &)` or
* `dpp::job(T)`, where T is the event type for this event router.
* The callable should either be of the form `void(const T&)` or
* `dpp::task<void>(const T&)`, where T is the event type for this event router.
*
* @param fun Callable to attach to event
* @return event_handle An event handle unique to this event, used to
Expand All @@ -605,7 +605,7 @@ template<class T> class event_router_t {

/**
* @brief Attach a callable to the event, adding a listener.
* The callable should either be of the form `void(const T &)` or
* The callable should either be of the form `void(const T&)` or
* `dpp::task<void>(const T&)`, where T is the event type for this event router.
*
* @param fun Callable to attach to event
Expand All @@ -623,7 +623,7 @@ template<class T> class event_router_t {

/**
* @brief Attach a callable to the event, adding a listener.
* The callable should either be of the form `void(const T &)` or
* The callable should either be of the form `void(const T&)` or
* `dpp::task<void>(const T&)`, where T is the event type for this event router.
*
* @param fun Callable to attach to event
Expand All @@ -643,7 +643,7 @@ template<class T> class event_router_t {

/**
* @brief Attach a callable to the event, adding a listener.
* The callable should either be of the form `void(const T &)` or
* The callable should either be of the form `void(const T&)` or
* `dpp::task<void>(const T&)`, where T is the event type for this event router.
*
* @deprecated dpp::job event handlers are deprecated and will be removed in a future version, use dpp::task<void> instead.
Expand All @@ -665,7 +665,7 @@ template<class T> class event_router_t {
# else
/**
* @brief Attach a callable to the event, adding a listener.
* The callable should be of the form `void(const T &)`
* The callable should be of the form `void(const T&)`
* where T is the event type for this event router.
*
* @param fun Callable to attach to event
Expand All @@ -679,7 +679,7 @@ template<class T> class event_router_t {

/**
* @brief Attach a callable to the event, adding a listener.
* The callable should be of the form `void(const T &)`
* The callable should be of the form `void(const T&)`
* where T is the event type for this event router.
*
* @warning You cannot call this within an event handler.
Expand Down

0 comments on commit a48a2c2

Please sign in to comment.