Skip to content

Commit

Permalink
Add coroutine support functions to message_create_t (co_send, co_repl…
Browse files Browse the repository at this point in the history
…y) (#1357)
  • Loading branch information
Feinq authored Jan 5, 2025
1 parent 92d1050 commit 5888e55
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
53 changes: 53 additions & 0 deletions include/dpp/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,59 @@ struct DPP_EXPORT message_create_t : public event_dispatch_t {
* @note confirmation_callback_t::value contains a message object on success. On failure, value is undefined and confirmation_callback_t::is_error() is true.
*/
void reply(message&& msg, bool mention_replied_user = false, command_completion_event_t callback = utility::log_error()) const;

#ifdef DPP_CORO
/**
* @brief Send a text to the same channel as the channel_id in received event.
*
* @param m Text to send
* On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
dpp::async<dpp::confirmation_callback_t> co_send(const std::string& m) const;

/**
* @brief Send a message to the same channel as the channel_id in received event.
*
* @param msg Message to send
* On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
dpp::async<dpp::confirmation_callback_t> co_send(const message& msg) const;

/**
* @brief Send a message to the same channel as the channel_id in received event.
*
* @param msg Message to send
* On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
dpp::async<dpp::confirmation_callback_t> co_send(message&& msg) const;

/**
* @brief Reply to the message received in the event.
*
* @param m Text to send as a reply.
* @param mention_replied_user mentions (pings) the author of message replied to, if true
* On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
dpp::async<dpp::confirmation_callback_t> co_reply(const std::string& m, bool mention_replied_user = false) const;

/**
* @brief Reply to the message received in the event.
*
* @param msg Message to send as a reply.
* @param mention_replied_user mentions (pings) the author of message replied to, if true
* On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
dpp::async<dpp::confirmation_callback_t> co_reply(const message& msg, bool mention_replied_user = false) const;

/**
* @brief Reply to the message received in the event.
*
* @param msg Message to send as a reply.
* @param mention_replied_user mentions (pings) the author of message replied to, if true
* On success the result will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
dpp::async<dpp::confirmation_callback_t> co_reply(message&& msg, bool mention_replied_user = false) const;
#endif /* DPP_CORO */
};

/**
Expand Down
26 changes: 26 additions & 0 deletions src/dpp/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ void message_create_t::reply(message&& msg, bool mention_replied_user, command_c
owner->message_create(std::move(msg), std::move(callback));
}

#ifdef DPP_CORO
async<confirmation_callback_t> message_create_t::co_send(const std::string& m) const {
return dpp::async{[&, this] <typename T> (T&& cb) { this->send(m, std::forward<T>(cb)); }};
}

async<confirmation_callback_t> message_create_t::co_send(const message& msg) const {
return dpp::async{[&, this] <typename T> (T&& cb) { this->send(msg, std::forward<T>(cb)); }};
}

async<confirmation_callback_t> message_create_t::co_send(message&& msg) const {
return dpp::async{[&, this] <typename T> (T&& cb) { this->send(std::move(msg), std::forward<T>(cb)); }};
}

async<confirmation_callback_t> message_create_t::co_reply(const std::string& m, bool mention_replied_user) const {
return dpp::async{[&, this] <typename T> (T&& cb) { this->reply(m, mention_replied_user, std::forward<T>(cb)); }};
}

async<confirmation_callback_t> message_create_t::co_reply(const message& msg, bool mention_replied_user) const {
return dpp::async{[&, this] <typename T> (T&& cb) { this->reply(msg, mention_replied_user, std::forward<T>(cb)); }};
}

async<confirmation_callback_t> message_create_t::co_reply(message&& msg, bool mention_replied_user) const {
return dpp::async{[&, this] <typename T> (T&& cb) { this->reply(std::move(msg), mention_replied_user, std::forward<T>(cb)); }};
}
#endif /* DPP_CORO */

void interaction_create_t::reply(interaction_response_type t, const message& m, command_completion_event_t callback) const {
owner->interaction_response_create(this->command.id, this->command.token, dpp::interaction_response(t, m), std::move(callback));
}
Expand Down

0 comments on commit 5888e55

Please sign in to comment.