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;
}

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

dpp::managed::id
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition: managed.h:79
dpp::slashcommand_t
User has issued a slash command.
Definition: dispatcher.h:715
dpp::co_mentionable
@ co_mentionable
A mentionable (users and roles).
Definition: appcommand.h:113
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:101
dpp::interaction::get_issuing_user
const dpp::user & get_issuing_user() const
Get the user who issued this command.
dpp::message
Represents messages sent and received on Discord.
Definition: message.h:1758
dpp::interaction_create_t::command
interaction command
command interaction
Definition: dispatcher.h:698
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:1358
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.
Definition: dispatcher.h:228
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::command_option
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: appcommand.h:222
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:99
dpp::confirmation_callback_t
The results of a REST call wrapped in a convenient struct.
Definition: restresults.h:274
dpp::ready_t
Session ready.
Definition: dispatcher.h:981
D++ 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