D++ (DPP)
C++ Discord API Bot Library
Editing Channels and Messages

Sometimes we need to update an object, such as message or channel. At first, it might seem confusing, but it's actually really simple! You just need to use an object with identical properties you don't need to update. NOTE: your bot can't edit messages sent by others.

Note
This example uses callback functions. To see more information about them, visit Using Callback Functions.
#include <dpp/dpp.h>
int main() {
/* the second argument is a bitmask of intents - i_message_content is needed to get messages */
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
if (event.command.get_command_name() == "msg-send") {
event.reply("That's a message");
} else if (event.command.get_command_name() == "msg-edit") {
const auto content = std::get<std::string>(event.get_parameter("content"));
/* get message to edit it after */
const dpp::snowflake msg_id = std::get<std::string>(event.get_parameter("msg-id"));
/* here string will automatically be converted to snowflake */
bot.message_get(msg_id, event.command.channel_id, [&bot, content, event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("error");
return;
}
auto message = callback.get<dpp::message>();
/* change the message content and edit the message itself */
message.set_content(content);
bot.message_edit(message);
event.reply("Message content is now `" + content + "`.");
});
} else if (event.command.get_command_name() == "channel-edit") {
const auto name = std::get<std::string>(event.get_parameter("name"));
/* get the channel to edit it after */
const auto channel_id = std::get<dpp::snowflake>(event.get_parameter("channel"));
bot.channel_get(channel_id, [&bot, name, event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("error");
return;
}
auto channel = callback.get<dpp::channel>();
/* change the channel name and edit the channel itself */
channel.set_name(name);
bot.channel_edit(channel);
event.reply("Channel name is now `" + name + "`.");
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once <struct register_global_commands>()) {
dpp::slashcommand msg_edit("msg-edit", "Edit a message sent by the bot", bot.me.id);
msg_edit.add_option(dpp::command_option(dpp::co_string, "msg-id", "ID of the message to edit", true)); /* true for required option */
msg_edit.add_option(dpp::command_option(dpp::co_string, "content", "New content for the message", true)); /* same here */
dpp::slashcommand channel_edit("channel-edit", "Edit the name of channel specified", bot.me.id);
channel_edit.add_option(dpp::command_option(dpp::co_channel, "channel", "Channel to edit", true));
channel_edit.add_option(dpp::command_option(dpp::co_string, "name", "New name for the channel", true));
dpp::slashcommand msg_send("msg-send", "Send my message", bot.me.id);
bot.global_bulk_command_create({ msg_edit, channel_edit, msg_send });
}
});
bot.start(dpp::st_wait);
return 0;
}
A definition of a discord channel. There are one of these for every channel type except threads....
Definition: channel.h:257
channel & set_name(const std::string &name)
Set name of this channel object.
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:82
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:999
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:65
@ i_default_intents
Default D++ intents (all non-privileged intents)
Definition: intents.h:73
@ co_channel
Definition: appcommand.h:63
@ co_string
Definition: appcommand.h:55
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:65
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: appcommand.h:155
The results of a REST call wrapped in a convenient struct.
Definition: restresults.h:243
bool is_error() const
Returns true if the call resulted in an error rather than a legitimate value in the confirmation_call...
T get() const
Get the stored value via std::get.
Definition: restresults.h:305
void reply(command_completion_event_t callback=utility::log_error()) const
Acknowledge interaction without displaying a message to the user, for use with button and select menu...
interaction command
command interaction
Definition: dispatcher.h:643
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
Session ready.
Definition: dispatcher.h:898
User has issued a slash command.
Definition: dispatcher.h:660

Before editing:

After editing:

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