| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
Using Callback Functions

When you create or get an object from Discord, you send the request to its API and in return you get either an error or the object you requested/created. You can pass a function to API calls as the callback function. This means that when the request completes, and you get a response from the API, your callback function executes. You must be careful with lambda captures! Good practice would be not capturing variables by reference unless you have to, since when the request completes and the function executes, the variables can already be destructed. Advanced reference can be found here. Now, let's see callback functions in action:

#include <dpp/dpp.h>
int main() {
dpp::cluster bot("Token Was Here", dpp::i_default_intents | dpp::i_message_content);
/* the second argument is a bitmask of intents - i_message_content is needed to get messages */
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
if (event.command.get_command_name() == "msgs-get") {
int64_t limit = std::get<int64_t>(event.get_parameter("quantity"));
/* get messages using ID of the channel the command was issued in */
bot.messages_get(event.command.channel_id, 0, 0, 0, limit, [event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) { /* catching an error to log it */
std::cout << callback.get_error().message << std::endl;
return;
}
auto messages = callback.get<dpp::message_map>();
/* std::get<dpp::message_map>(callback.value) would give the same result */
std::string contents;
for (const auto& x : messages) { /* here we iterate through the dpp::message_map we got from callback... */
contents += x.second.content + '\n'; /* ...where x.first is ID of the current message and x.second is the message itself. */
}
event.reply(contents); /* we will see all those messages we got, united as one! */
});
} else if (event.command.get_command_name() == "channel-create") {
/* create a text channel */
dpp::channel channel = dpp::channel()
.set_name("test")
.set_guild_id(event.command.guild_id);
bot.channel_create(channel, [&bot, event](const dpp::confirmation_callback_t& callback) -> void {
if (callback.is_error()) { /* catching an error to log it */
bot.log(dpp::loglevel::ll_error, callback.get_error().message);
return;
}
auto channel = callback.get<dpp::channel>();
/* std::get<dpp::channel>(callback.value) would give the same result */
/* reply with the created channel information */
dpp::message message = dpp::message("The channel's name is `" + channel.name + "`, ID is `" + std::to_string(channel.id) + " and type is `" + std::to_string(channel.get_type()) + "`.");
/* note that channel types are represented as numbers */
event.reply(message);
});
} else if (event.command.get_command_name() == "msg-error") {
bot.message_get(0, 0, [event](const dpp::confirmation_callback_t& callback) -> void {
/* the error will occur since there is no message with ID '0' that is in a channel with ID '0' (I'm not explaining why) */
if (callback.is_error()) {
event.reply(callback.get_error().message);
return;
}
/* we won't be able to get here because of the return; statement */
auto message = callback.get<dpp::message>();
event.reply(message);
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once <struct register_global_commands>()) {
dpp::slashcommand msgs_get("msgs-get", "Get messages", bot.me.id);
constexpr int64_t min_val{1};
constexpr int64_t max_val{100};
msgs_get.add_option(
dpp::command_option(dpp::co_integer, "quantity", "Quantity of messages to get. Max - 100.")
.set_min_value(min_val)
.set_max_value(max_val)
);
dpp::slashcommand channel_create("channel-create", "Create a channel", bot.me.id);
dpp::slashcommand msg_error("msg-error", "Get an error instead of message :)", bot.me.id);
bot.global_bulk_command_create({ msgs_get, channel_create, msg_error });
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::interaction::channel_id
snowflake channel_id
Optional: the channel it was sent from.
Definition appcommand.h:1043
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
dpp::i_message_content
@ i_message_content
Intent for receipt of message content.
Definition intents.h:112
dpp::i_default_intents
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition intents.h:132
dpp::message_map
std::unordered_map< snowflake, message > message_map
A group of messages.
Definition message.h:3086
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::co_integer
@ co_integer
An integer value.
Definition appcommand.h:74
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::command_option
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition appcommand.h:208
dpp::confirmation_callback_t
The results of a REST call wrapped in a convenient struct.
Definition restresults.h:261
dpp::confirmation_callback_t::is_error
bool is_error() const
Returns true if the call resulted in an error rather than a legitimate value in the confirmation_call...
dpp::confirmation_callback_t::get_error
error_info get_error() const
Get the error_info object. The error_info object contains the details of any REST error,...
dpp::confirmation_callback_t::get
T get() const
Get the stored value via std::get.
Definition restresults.h:329
dpp::error_info::message
std::string message
Error message.
Definition restresults.h:245
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::interaction_create_t::get_parameter
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
dpp::message
Represents messages sent and received on Discord.
Definition message.h:2350
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806

This is the result:

D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0