diff --git a/docpages/example_programs/using_coroutines/awaiting_events.md b/docpages/example_programs/using_coroutines/awaiting_events.md index f3e28878cb..869c72e975 100644 --- a/docpages/example_programs/using_coroutines/awaiting_events.md +++ b/docpages/example_programs/using_coroutines/awaiting_events.md @@ -10,7 +10,7 @@ D++ makes it possible to await events: simple use `co_await` on any of the event #include int main() { - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + dpp::cluster bot{"token"}; bot.on_log(dpp::utility::cout_logger()); @@ -30,8 +30,8 @@ int main() { 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{}); + // Acknowledge the click and edit the original response, removing the button + click_event.reply(); event.edit_original_response(dpp::message{"You clicked the button!"}); } }); diff --git a/docpages/example_programs/using_coroutines/coro_introduction.md b/docpages/example_programs/using_coroutines/coro_introduction.md index bcb5042c92..217f7be4bf 100644 --- a/docpages/example_programs/using_coroutines/coro_introduction.md +++ b/docpages/example_programs/using_coroutines/coro_introduction.md @@ -9,29 +9,32 @@ Let's revisit \ref attach-file "attaching a downloaded file", but this time with #include int main() { - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + dpp::cluster bot{"token"}; bot.on_log(dpp::utility::cout_logger()); - /* Message handler to look for a command called !file */ + /* The event is fired when someone issues your commands */ /* Make note of passing the event by value, this is important (explained below) */ - bot.on_message_create([](dpp::message_create_t event) -> dpp::job { - dpp::cluster *cluster = event.from->creator; + bot.on_slashcommand([](dpp::slashcommand_t event) -> dpp::job { + 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); - if (event.msg.content == "!file") { - // request an image and co_await the response - dpp::http_request_completion_t result = co_await cluster->co_request("https://dpp.dev/DPP-Logo.png", dpp::m_get); - - // create a message - dpp::message msg(event.msg.channel_id, "This is my new attachment:"); - - // attach the image on success + /* Create a message and attach the image on success */ + dpp::message msg(event.command.channel_id, "This is my new attachment:"); if (result.status == 200) { msg.add_file("logo.png", result.body); } - // send the message - cluster->message_create(msg); + /* Send the message, with our attachment. */ + event.reply(msg); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + /* Create and register a command when the bot is ready */ + bot.global_command_create(dpp::slashcommand{"file", "Send a message with an image attached from the internet!", bot.me.id}); } }); diff --git a/docpages/example_programs/using_coroutines/coro_simple_commands.md b/docpages/example_programs/using_coroutines/coro_simple_commands.md index 5ac09c3939..cb47dcca17 100644 --- a/docpages/example_programs/using_coroutines/coro_simple_commands.md +++ b/docpages/example_programs/using_coroutines/coro_simple_commands.md @@ -12,7 +12,7 @@ With coroutines, it becomes a lot easier to do several asynchronous requests for #include int main() { - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + dpp::cluster bot("token"); bot.on_log(dpp::utility::cout_logger()); @@ -28,7 +28,7 @@ int main() { // For simplicity for this example we only support PNG if (attachment.content_type != "image/png") { - // While event.co_reply is available, we can just use event.reply, as we will exit the command anyway and don't need to wait on the result + // While we could use event.co_reply, we can just use event.reply, as we will exit the command anyway and don't need to wait on the result event.reply("Error: type " + attachment.content_type + " not supported"); co_return; } @@ -54,7 +54,7 @@ int main() { event.edit_response("Error: could not add emoji: " + confirmation.get_error().message); } else { // Success event.edit_response("Successfully added " + confirmation.get().get_mention()); // Show the new emoji - } + } } } }); @@ -62,7 +62,6 @@ int main() { bot.on_ready([&bot](const dpp::ready_t & event) { if (dpp::run_once()) { dpp::slashcommand command("addemoji", "Add an emoji", bot.me.id); - // Add file and name as required parameters command.add_option(dpp::command_option(dpp::co_attachment, "file", "Select an image", true)); command.add_option(dpp::command_option(dpp::co_string, "name", "Name of the emoji to add", true)); @@ -87,7 +86,7 @@ Here is an example of a command making use of dpp::task to retrieve the avatar o #include int main() { - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + dpp::cluster bot("token"); bot.on_log(dpp::utility::cout_logger()); @@ -97,17 +96,17 @@ int main() { constexpr auto resolve_member = [](const dpp::slashcommand_t &event) -> dpp::task> { const dpp::command_value &user_param = event.get_parameter("user"); dpp::snowflake user_id; - - if (std::holds_alternative(user_param)) + if (std::holds_alternative(user_param)) { user_id = event.command.usr.id; // Parameter is empty so user is sender - else if (std::holds_alternative(user_param)) + } + else if (std::holds_alternative(user_param)) { user_id = std::get(user_param); // Parameter has a user + } // If we have the guild member in the command's resolved data, return it const auto &member_map = event.command.resolved.members; if (auto member = member_map.find(user_id); member != member_map.end()) co_return member->second; - // Try looking in guild cache dpp::guild *guild = dpp::find_guild(event.command.guild_id); if (guild) { @@ -116,21 +115,19 @@ int main() { co_return member->second; } } - // Finally if everything else failed, request API dpp::confirmation_callback_t confirmation = co_await event.from->creator->co_guild_get_member(event.command.guild_id, user_id); - if (confirmation.is_error()) + if (confirmation.is_error()) { co_return std::nullopt; // Member not found, return empty - else + } else { co_return confirmation.get(); + } }; // Send a " is thinking..." message, to wait on later so we can edit dpp::async thinking = event.co_thinking(false); - // Call our coroutine defined above to retrieve the member requested std::optional member = co_await resolve_member(event); - if (!member.has_value()) { // Wait for the thinking response to arrive to make sure we can edit co_await thinking; @@ -141,7 +138,6 @@ int main() { std::string avatar_url = member->get_avatar_url(512); if (avatar_url.empty()) { // Member does not have a custom avatar for this server, get their user avatar dpp::confirmation_callback_t confirmation = co_await event.from->creator->co_user_get_cached(member->user_id); - if (confirmation.is_error()) { // Wait for the thinking response to arrive to make sure we can edit co_await thinking; @@ -161,7 +157,6 @@ int main() { bot.on_ready([&bot](const dpp::ready_t & event) { if (dpp::run_once()) { 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")); bot.global_command_create(command); diff --git a/docpages/example_programs/using_coroutines/expiring_buttons.md b/docpages/example_programs/using_coroutines/expiring_buttons.md index 0412f84097..97818a5bc3 100644 --- a/docpages/example_programs/using_coroutines/expiring_buttons.md +++ b/docpages/example_programs/using_coroutines/expiring_buttons.md @@ -8,7 +8,7 @@ In the last example we've explored how to \ref awaiting-events "await events" us #include int main() { - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + dpp::cluster bot{"token"}; bot.on_log(dpp::utility::cout_logger()); @@ -26,16 +26,15 @@ int main() { 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(5) // Or sleep 5 seconds }; - // 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 + // 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{}); + // Acknowledge the click and edit the original response, removing the button + dpp::button_click_t &click_event = result.get<0>(); + click_event.reply(); event.edit_original_response(dpp::message{"You clicked the button with the id " + click_event.custom_id}); - } - else { // Here index() is 1, the timer expired + } else { // Here index() is 1, the timer expired event.edit_original_response(dpp::message{"I haven't got all day!"}); - } + } } });