Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coroutine support functions to message_create_t (co_send, co_reply) #1357

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading