- Warning
- D++ Coroutines are currently only supported by D++ on g++ 13, clang/LLVM 14, and MSVC 19.37 or above. Additionally, your program has to support C++20.
Several steps in one
- Note
- The next example assumes you are already familiar with how to use slash commands, parameters, and sending files through a command.
With coroutines, it becomes a lot easier to do several asynchronous requests for one task. As an example an "addemoji" command taking a file and a name as a parameter. This means downloading the emoji, submitting it to Discord, and finally replying, with some error handling along the way. Normally we would have to use callbacks and some sort of object keeping track of our state, but with coroutines, the function can simply pause and be resumed when we receive the response to our request :
#include <dpp/dpp.h>
int main() {
dpp::cluster *cluster = event.owner;
dpp::snowflake file_id = std::get<dpp::snowflake>(event.get_parameter("file"));
std::string emoji_name = std::get<std::string>(event.get_parameter("name"));
const dpp::attachment &attachment = event.command.get_resolved_attachment(file_id);
if (attachment.content_type != "image/png") {
event.reply("Error: type " + attachment.content_type + " not supported");
co_return;
}
co_await thinking;
event.edit_response("Error: could not download the attachment");
} else {
co_await thinking;
event.edit_response(
"Error: could not add emoji: " + confirmation.
get_error().
message);
} else {
}
}
}
});
bot.global_command_create(command);
}
});
return 0;
}
A co_await-able object handling an API call in parallel with the caller.
Definition async.h:107
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
event_router_t< log_t > on_log
Called when a log message is to be written to the log. You can attach any logging system here you wis...
Definition cluster.h:692
Represents an emoji for a dpp::guild.
Definition emoji.h:64
static std::string get_mention(std::string_view name, snowflake id, bool is_animated=false)
Create a mentionable emoji.
snowflake guild_id
Optional: the guild it was sent from.
Definition appcommand.h:1038
std::string get_command_name() const
Get the command name for a command interaction.
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
A coroutine task. It starts immediately on construction and can be co_await-ed, making it perfect for...
Definition task.h:95
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
@ m_get
GET.
Definition queues.h:190
@ i_png
image/png
Definition misc-enum.h:36
@ co_attachment
File attachment type.
Definition appcommand.h:109
@ co_string
A string value.
Definition appcommand.h:69
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition appcommand.h:208
The results of a REST call wrapped in a convenient struct.
Definition restresults.h:261
bool is_error() const
Returns true if the call resulted in an error rather than a legitimate value in the confirmation_call...
error_info get_error() const
Get the error_info object. The error_info object contains the details of any REST error,...
T get() const
Get the stored value via std::get.
Definition restresults.h:329
std::string message
Error message.
Definition restresults.h:245
The result of any HTTP request. Contains the headers, vital rate limit figures, and returned request ...
Definition queues.h:112
uint16_t status
HTTP status. e.g. 200 = OK, 404 = Not found, 429 = Rate limited, etc.
Definition queues.h:122
std::string body
Reply body.
Definition queues.h:163
interaction command
command interaction
Definition dispatcher.h:789
dpp::async< dpp::confirmation_callback_t > co_thinking(bool ephemeral=false) const
Set the bot to 'thinking' state where you have up to 15 minutes to respond.
Session ready.
Definition dispatcher.h:1072
User has issued a slash command.
Definition dispatcher.h:806
I heard you liked tasks
- Note
- This next example is fairly advanced and makes use of many of both C++ and D++'s advanced features.
Earlier we mentioned two other types of coroutines provided by dpp: dpp::coroutine and dpp::task. They both take their return type as a template parameter, which may be void. Both dpp::job and dpp::task start on the constructor for asynchronous execution, however only the latter can be co_await-ed, this allows you to retrieve its return value. If a dpp::task is destroyed before it ends, it is cancelled and will stop when it is resumed from the next co_await. dpp::coroutine also has a return value and can be co_await-ed, however it only starts when co_await-ing, meaning it is executed synchronously.
Here is an example of a command making use of dpp::task to retrieve the avatar of a specified user, or if missing, the sender:
#include <dpp/dpp.h>
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)) {
user_id = event.command.usr.id;
}
else if (std::holds_alternative<dpp::snowflake>(user_param)) {
user_id = std::get<dpp::snowflake>(user_param);
}
const auto &member_map = event.command.resolved.members;
if (auto member = member_map.find(user_id); member != member_map.end()) {
co_return member->second;
}
dpp::guild *guild = dpp::find_guild(event.command.guild_id);
if (guild) {
if (auto member = guild->members.find(user_id); member != guild->members.end()) {
co_return member->second;
}
}
dpp::confirmation_callback_t confirmation = co_await event.owner->co_guild_get_member(event.command.guild_id, user_id);
if (confirmation.is_error()) {
co_return std::nullopt;
} else {
co_return confirmation.get<dpp::guild_member>();
}
};
std::optional<dpp::guild_member> member = co_await resolve_member(event);
if (!member.has_value()) {
co_await thinking;
event.edit_original_response(
dpp::message{
"User not found in this server!"});
co_return;
}
std::string avatar_url = member->get_avatar_url(512);
if (avatar_url.empty()) {
co_await thinking;
event.edit_original_response(
dpp::message{
"User not found!"});
co_return;
}
}
}
});
dpp::slashcommand command(
"avatar",
"Get your or another user's avatar image", bot.me.id);
bot.global_command_create(command);
}
});
return 0;
}
A user with additional fields only available via the oauth2 identify scope. These are not included in...
Definition user.h:470
constexpr const char thinking[]
Definition unicode_emoji.h:1691
@ co_user
A user snowflake id.
Definition appcommand.h:84
Represents messages sent and received on Discord.
Definition message.h:2350