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() {
int64_t limit = std::get<int64_t>(event.
get_parameter(
"quantity"));
if (callback.is_error()) {
std::cout << callback.get_error().message << std::endl;
return;
}
std::string contents;
for (const auto& x : messages) {
contents += x.second.content + '\n';
}
event.reply(contents);
});
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()) {
bot.log(dpp::loglevel::ll_error, callback.get_error().message);
return;
}
auto channel = callback.get<dpp::channel>();
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()) + "`.");
event.reply(message);
});
return;
}
event.reply(message);
});
}
});
constexpr int64_t min_val{1};
constexpr int64_t max_val{100};
msgs_get.add_option(
.set_min_value(min_val)
.set_max_value(max_val)
);
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 });
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
snowflake channel_id
Optional: the channel it was sent from.
Definition appcommand.h:1043
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
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ i_message_content
Intent for receipt of message content.
Definition intents.h:112
@ i_default_intents
Default D++ intents (all non-privileged intents).
Definition intents.h:132
std::unordered_map< snowflake, message > message_map
A group of messages.
Definition message.h:3086
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
@ co_integer
An integer value.
Definition appcommand.h:74
@ 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
interaction command
command interaction
Definition dispatcher.h:789
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
Represents messages sent and received on Discord.
Definition message.h:2350
Session ready.
Definition dispatcher.h:1072
User has issued a slash command.
Definition dispatcher.h:806