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

Search
Loading...
Searching...
No Matches
Editing Channels and Messages

Sometimes we need to update an object, such as a message (whether it's plain text or an embed) or a channel. At first, it might seem confusing, but it's actually really simple! You need an object with all the properties being identical to the existing one. Say you're editing a message. You need to have an object with its ID the same as the one in Discord. Then you replace what you need, such as its content.

Note
  This example uses callback functions and embeds. To see more information about them, visit Using Callback Functions and Sending Embeds.

## Editing messages

Here we send a message and edit it after. To do so, we first reply to the command msg-send with some text, "This is a message" in our case. As described above, on the next step the message object is taken and the text is replaced with whatever the user desires.

#include <dpp/dpp.h>
int main() {
/* the second argument is a bitmask of intents - i_message_content is needed to get messages */
dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content);
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() == "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 the message to edit it after. here string will automatically be converted to snowflake */
const dpp::snowflake msg_id = std::get<std::string>(event.get_parameter("msg-id"));
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 + "`.");
});
}
});
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 msg_send("msg-send", "Send my message", bot.me.id);
bot.global_bulk_command_create({ msg_edit, msg_send });
}
});
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::snowflake
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition snowflake.h:54
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::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::co_string
@ co_string
A string value.
Definition appcommand.h:69
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::get
T get() const
Get the stored value via std::get.
Definition restresults.h:329
dpp::interaction_create_t::reply
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...
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::message::set_content
message & set_content(std::string_view c)
Set the message content.
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806

Note
  Your bot can't edit messages sent by others!Sending Embeds.

Before editing the message:

After editing the message:

## Editing channels

Now we'll want to edit an existing channel - its name in this case. This works similarly to how messages are edited.

#include <dpp/dpp.h>
int main() {
/* the second argument is a bitmask of intents - i_message_content is needed to get messages */
dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content);
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() == "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 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));
bot.global_command_create(channel_edit);
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::channel
A definition of a discord channel. There are one of these for every channel type except threads....
Definition channel.h:340
dpp::channel::set_name
channel & set_name(const std::string &name)
Set name of this channel object.
dpp::co_channel
@ co_channel
A channel snowflake id. Includes all channel types and categories.
Definition appcommand.h:89
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...

Before editing the channel:

After editing the channel:

## Editing embeds

Now let's send an embed and edit it. If a message has one content field, it can have a few embed fields, up to 10 to be precise. So we first get the embed we want and edit and change its description.

#include <dpp/dpp.h>
int main() {
/* the second argument is a bitmask of intents - i_message_content is needed to get messages */
dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content);
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() == "embed-send") {
dpp::embed embed = dpp::embed()
.set_color(dpp::colors::sti_blue)
.set_title("like and subscribe")
.set_url("https://dpp.dev/")
.set_author("Some author", "https://dpp.dev/", "https://dpp.dev/DPP-Logo.png")
.set_description("Creator is <creator name>");
event.reply(embed);
} else if (event.command.get_command_name() == "embed-edit") {
const auto description = std::get<std::string>(event.get_parameter("desc"));
/* get the message to edit its embed after. here string will automatically be converted to snowflake */
const dpp::snowflake msg_id = std::get<std::string>(event.get_parameter("msg-id"));
bot.message_get(msg_id, event.command.channel_id, [&bot, description, event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
event.reply("error");
return;
}
auto message = callback.get<dpp::message>();
auto& embeds = message.embeds;
/* change the embed description and edit the message itself.
* since we're using a reference, what changes in embeds changes in message.embeds
*/
embeds[0].set_description(description);
bot.message_edit(message);
event.reply("Embed description is now `" + description + "`.");
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once <struct register_global_commands>()) {
dpp::slashcommand embed_send("embed-send", "Send my embed", bot.me.id);
dpp::slashcommand embed_edit("embed-edit", "Edit an embed sent by the bot", bot.me.id);
embed_edit.add_option(dpp::command_option(dpp::co_string, "msg-id", "ID of the embed to edit", true)); /* true for required option */
embed_edit.add_option(dpp::command_option(dpp::co_string, "desc", "New description for the embed", true)); /* same here */
bot.global_bulk_command_create({ embed_send, embed_edit });
}
});
bot.start(dpp::st_wait);
return 0;
}
dpp::colors::sti_blue
static constexpr uint32_t sti_blue
Definition colors.h:70
dpp::embed
A rich embed for display within a dpp::message.
Definition message.h:1090
dpp::embed::set_description
embed & set_description(std::string_view text)
Set embed description.
dpp::embed::set_author
embed & set_author(const dpp::embed_author &a)
Set embed author.
dpp::embed::set_color
embed & set_color(uint32_t col)
Set embed colour.
dpp::embed::set_title
embed & set_title(std::string_view text)
Set embed title.
dpp::embed::set_url
embed & set_url(std::string_view url)
Set embed url.
dpp::message::embeds
std::vector< embed > embeds
Up to 10 dpp::embed objects.
Definition message.h:2448

Before editing the embed:

Finally, after editing the embed:

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