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() {
std::string emoji_name = std::get<std::string>(event.
get_parameter(
"name"));
const dpp::attachment &attachment =
event.command.get_resolved_attachment(file_id);
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 {
}
}
}
});
}
});
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
void global_command_create(const slashcommand &s, command_completion_event_t callback=utility::log_error())
Create a global slash command (a bot can have a maximum of 100 of these).
async< http_request_completion_t > co_request(const std::string &url, http_method method, const std::string &postdata="", const std::string &mimetype="text/plain", const std::multimap< std::string, std::string > &headers={}, const std::string &protocol="1.1")
void start(start_type return_after=st_wait)
Start the cluster, connecting all its shards.
event_router_t< slashcommand_t > on_slashcommand
Called when a slash command is issued. Only dpp::ctxm_chat_input types of interaction are routed to t...
Definition cluster.h:734
event_router_t< ready_t > on_ready
Called when a shard is connected and ready. A set of cluster::on_guild_create events will follow this...
Definition cluster.h:842
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
async< confirmation_callback_t > co_guild_emoji_create(snowflake guild_id, const class emoji &newemoji)
Create single emoji. You must ensure that the emoji passed contained image data using the emoji::load...
dpp::user me
The details of the bot user. This is assumed to be identical across all shards in the cluster....
Definition cluster.h:255
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.
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition managed.h:39
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition snowflake.h:54
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:189
@ 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
Represents an attachment in a dpp::message.
Definition message.h:1366
std::string content_type
MIME type of the attachment, if applicable.
Definition message.h:1411
std::string url
URL which points to the attachment.
Definition message.h:1391
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:111
uint16_t status
HTTP status. e.g. 200 = OK, 404 = Not found, 429 = Rate limited, etc.
Definition queues.h:121
std::string body
Reply body.
Definition queues.h:162
interaction command
command interaction
Definition dispatcher.h:789
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
Session ready.
Definition dispatcher.h:1072
User has issued a slash command.
Definition dispatcher.h:806
#include <dpp/dpp.h>
int main() {
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;
}
if (guild) {
if (
auto member = guild->
members.find(user_id); member != guild->
members.end()) {
co_return member->second;
}
}
co_return std::nullopt;
} else {
}
};
std::optional<dpp::guild_member> member = co_await resolve_member(event);
if (!member.has_value()) {
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()) {
event.edit_original_response(
dpp::message{
"User not found!"});
co_return;
}
}
}
});
}
});
return 0;
}
Represents dpp::user membership upon a dpp::guild. This contains the user's nickname,...
Definition guild.h:407
Represents a guild on Discord (AKA a server).
Definition guild.h:999
members_container members
List of guild members.
Definition guild.h:1069
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
std::variant< std::monostate, std::string, int64_t, bool, snowflake, double > command_value
This type is a variant that can hold any of the potential native data types represented by the enum d...
Definition appcommand.h:122
@ co_user
A user snowflake id.
Definition appcommand.h:84
DPP_EXPORT class guild * find_guild(snowflake id)
Represents messages sent and received on Discord.
Definition message.h:2350