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

Conversation

Feinq
Copy link

@Feinq Feinq commented Jan 4, 2025

The message_create_t type was missing direct coroutine function variations. So I have implemented functions and overloads for both co_send() and co_reply() functions. This PR improves code consistency and usability.

(Similar coroutine functions have already been implemented for interaction_create_t before, but not for message_create_t)

Code change checklist

  • I have ensured that all methods and functions are fully documented using doxygen style comments.
  • My code follows the coding style guide.
  • I tested that my change works before raising the PR.
  • I have ensured that I did not break any existing API calls.
  • I have not built my pull request using AI, a static analysis tool or similar without any human oversight.

Single-File Test Bot

#include <dpp/dpp.h>
#include <dpp/coro.h>
#include <coroutine>
#include <iostream>

const std::string BOT_TOKEN = "your bot token here";

int main() {
    dpp::cluster bot(BOT_TOKEN, dpp::i_default_intents | dpp::i_message_content);

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

    /*
    This example demonstrates the use of co_reply and co_send functions on the dpp::message_create_t event.
    You can test them by sending !test1, !test2, !test3, and !test4 in a channel the bot is in.
    The first 3 tests each perform a co_reply with different overloads, the 4th test performs a co_send with all overloads.
    */

    bot.on_message_create([](const dpp::message_create_t& event) -> dpp::task<void> {

        std::cout << "Received message: " << event.msg.content << std::endl;

        if (event.msg.content == "!test1") {
            dpp::confirmation_callback_t result = co_await event.co_reply("This is a test reply.", true);
            if (!result.is_error()) {
                std::cout << "1st co_reply with string succeeded!" << std::endl;
            } else {
                std::cout << "1st co_reply with string failed: " << result.get_error().message << std::endl;
            }
        }

        if (event.msg.content == "!test2") {
            dpp::message msg("This is a test reply.");
            dpp::confirmation_callback_t result = co_await event.co_reply(msg, false);
            if (!result.is_error()) {
                std::cout << "2nd co_reply with message obj succeeded!" << std::endl;
            } else {
                std::cout << "2nd co_reply with message obj failed: " << result.get_error().message << std::endl;
            }
        }

        if (event.msg.content == "!test3") {
            dpp::message msg("This is a test reply.");
            dpp::confirmation_callback_t result = co_await event.co_reply(std::move(msg), true);
            if (!result.is_error()) {
                std::cout << "3rd co_thinking with moved message obj succeeded!" << std::endl;
            } else {
                std::cout << "3rd co_thinking with moved message obj failed: " << result.get_error().message << std::endl;
            }
        }

        // Test all of the co_send overloads
        if (event.msg.content == "!test4") {
            dpp::confirmation_callback_t result1 = co_await event.co_send("This is a test send.");
            if (!result1.is_error()) {
                std::cout << "1st co_send with string succeeded!" << std::endl;
            } else {
                std::cout << "1st co_send with string failed: " << result1.get_error().message << std::endl;
            }

            dpp::message msg("This is a test send.");
            dpp::confirmation_callback_t result2 = co_await event.co_send(msg);
            if (!result2.is_error()) {
                std::cout << "2nd co_send with message obj succeeded!" << std::endl;
            } else {
                std::cout << "2nd co_send with message obj failed: " << result2.get_error().message << std::endl;
            }

            dpp::message msg2("This is a test send.");
            dpp::confirmation_callback_t result3 = co_await event.co_send(std::move(msg2));
            if (!result3.is_error()) {
                std::cout << "3rd co_send with moved message obj succeeded!" << std::endl;
            } else {
                std::cout << "3rd co_send with moved message obj failed: " << result3.get_error().message << std::endl;
            }
        }

    });

    bot.start(dpp::st_wait);
}```

@CLAassistant
Copy link

CLAassistant commented Jan 4, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

netlify bot commented Jan 4, 2025

Deploy Preview for dpp-dev ready!

Name Link
🔨 Latest commit e7ab745
🔍 Latest deploy log https://app.netlify.com/sites/dpp-dev/deploys/6779afe2292e340008d026f3
😎 Deploy Preview https://deploy-preview-1357--dpp-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added documentation Improvements or additions to documentation code Improvements or additions to code. labels Jan 4, 2025
Copy link
Contributor

@braindigitalis braindigitalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@braindigitalis braindigitalis merged commit 5888e55 into brainboxdotcc:dev Jan 5, 2025
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Improvements or additions to code. documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants