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() {
event.reply("That's a message");
const auto content = std::get<std::string>(event.get_parameter("content"));
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>();
message.set_content(content);
bot.message_edit(message);
event.reply("Message content is now `" + content + "`.");
});
}
});
if (dpp::run_once <struct register_global_commands>()) {
bot.global_bulk_command_create({ msg_edit, msg_send });
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:89
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:1397
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
@ co_string
A string value.
Definition: appcommand.h:65
@ 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:204
interaction command
command interaction
Definition: dispatcher.h:688
Session ready.
Definition: dispatcher.h:971
User has issued a slash command.
Definition: dispatcher.h:705
- 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() {
const auto name = std::get<std::string>(event.get_parameter("name"));
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>();
channel.set_name(name);
bot.channel_edit(channel);
event.reply("Channel name is now `" + name + "`.");
});
}
});
if (dpp::run_once <struct register_global_commands>()) {
dpp::slashcommand channel_edit(
"channel-edit",
"Edit the name of channel specified", bot.me.id);
bot.global_command_create(channel_edit);
}
});
return 0;
}
@ co_channel
A channel snowflake id. Includes all channel types and categories.
Definition: appcommand.h:85
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() {
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);
const auto description = std::get<std::string>(event.get_parameter("desc"));
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;
embeds[0].set_description(description);
bot.message_edit(message);
event.reply("Embed description is now `" + description + "`.");
});
}
});
if (dpp::run_once <struct register_global_commands>()) {
dpp::slashcommand embed_edit(
"embed-edit",
"Edit an embed sent by the bot", bot.me.id);
bot.global_bulk_command_create({ embed_send, embed_edit });
}
});
return 0;
}
Before editing the embed:
Finally, after editing the embed: