D++ (DPP)
C++ Discord API Bot Library
Sending private messages

Sometimes it's simply not enough to ping someone in a server with a message, and we get that. That's why you can private message people! This tutorial will cover how to make a command that will either message the author of the command or message a specified user!

Note
This tutorial makes use of callbacks. For more information about that, visit Using Callback Functions.
#include <dpp/dpp.h>
int main() {
/* Create the bot */
dpp::cluster bot("token");
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
/* Check which command they ran */
if (event.command.get_command_name() == "pm") {
dpp::snowflake user;
/* If there was no specified user, we set the "user" variable to the command author (issuing user). */
if (event.get_parameter("user").index() == 0) {
user = event.command.get_issuing_user().id;
} else { /* Otherwise, we set it to the specified user! */
user = std::get<dpp::snowflake>(event.get_parameter("user"));
}
/* Send a message to the user set above. */
bot.direct_message_create(user, dpp::message("Here's a private message!"), [event, user](const dpp::confirmation_callback_t& callback){
/* If the callback errors, we want to send a message telling the author that something went wrong. */
if (callback.is_error()) {
/* Here, we want the error message to be different if the user we're trying to send a message to is the command author. */
if (user == event.command.get_issuing_user().id) {
event.reply(dpp::message("I couldn't send you a message.").set_flags(dpp::m_ephemeral));
} else {
event.reply(dpp::message("I couldn't send a message to that user. Please check that is a valid user!").set_flags(dpp::m_ephemeral));
}
return;
}
/* We do the same here, so the message is different if it's to the command author or if it's to a specified user. */
if (user == event.command.get_issuing_user().id) {
event.reply(dpp::message("I've sent you a private message.").set_flags(dpp::m_ephemeral));
} else {
event.reply(dpp::message("I've sent a message to that user.").set_flags(dpp::m_ephemeral));
}
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Register the command */
dpp::slashcommand command("pm", "Send a private message.", bot.me.id);
/* Add the option for a user mention that isn't required */
command.add_option(dpp::command_option(dpp::co_mentionable, "user", "The user to message", false));
/* Register the command */
bot.global_command_create(command);
}
});
bot.start(dpp::st_wait);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:80
const dpp::user & get_issuing_user() const
Get the user who issued this command.
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:1339
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ co_mentionable
A mentionable (users and roles).
Definition: appcommand.h:94
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:63
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: appcommand.h:203
The results of a REST call wrapped in a convenient struct.
Definition: restresults.h:255
interaction command
command interaction
Definition: dispatcher.h:678
Represents messages sent and received on Discord.
Definition: message.h:1739
Session ready.
Definition: dispatcher.h:961
User has issued a slash command.
Definition: dispatcher.h:695

That's it! Now, you should have something like this:

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0