Skip to content

Commit

Permalink
docs(coro): small touches
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Aug 30, 2023
1 parent 32b6759 commit ea5e486
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
6 changes: 3 additions & 3 deletions docpages/example_programs/using_coroutines/awaiting_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ D++ makes it possible to await events: simple use `co_await` on any of the event
#include <dpp/dpp.h>

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());

Expand All @@ -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!"});
}
});
Expand Down
31 changes: 17 additions & 14 deletions docpages/example_programs/using_coroutines/coro_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ Let's revisit \ref attach-file "attaching a downloaded file", but this time with
#include <dpp/dpp.h>

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<struct register_bot_commands>()) {
/* 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});
}
});

Expand Down
27 changes: 11 additions & 16 deletions docpages/example_programs/using_coroutines/coro_simple_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ With coroutines, it becomes a lot easier to do several asynchronous requests for
#include <dpp/dpp.h>

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());

Expand All @@ -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;
}
Expand All @@ -54,15 +54,14 @@ int main() {
event.edit_response("Error: could not add emoji: " + confirmation.get_error().message);
} else { // Success
event.edit_response("Successfully added " + confirmation.get<dpp::emoji>().get_mention()); // Show the new emoji
}
}
}
}
});

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
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));
Expand All @@ -87,7 +86,7 @@ Here is an example of a command making use of dpp::task to retrieve the avatar o
#include <dpp/dpp.h>

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());

Expand All @@ -97,17 +96,17 @@ int main() {
constexpr auto resolve_member = [](const dpp::slashcommand_t &event) -> dpp::task<std::optional<dpp::guild_member>> {
const dpp::command_value &user_param = event.get_parameter("user");
dpp::snowflake user_id;

if (std::holds_alternative<std::monostate>(user_param))
if (std::holds_alternative<std::monostate>(user_param)) {
user_id = event.command.usr.id; // Parameter is empty so user is sender
else if (std::holds_alternative<dpp::snowflake>(user_param))
}
else if (std::holds_alternative<dpp::snowflake>(user_param)) {
user_id = std::get<dpp::snowflake>(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) {
Expand All @@ -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<dpp::guild_member>();
}
};

// Send a "<bot> 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<dpp::guild_member> 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;
Expand All @@ -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;
Expand All @@ -161,7 +157,6 @@ int main() {
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"));

bot.global_command_create(command);
Expand Down
15 changes: 7 additions & 8 deletions docpages/example_programs/using_coroutines/expiring_buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ In the last example we've explored how to \ref awaiting-events "await events" us
#include <dpp/dpp.h>

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());

Expand All @@ -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!"});
}
}
}
});

Expand Down

0 comments on commit ea5e486

Please sign in to comment.