Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an example on how to edit an embed #1128

Merged
merged 10 commits into from
May 13, 2024
47 changes: 39 additions & 8 deletions docpages/example_code/editing_messages.cpp
Henonicks marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <dpp/dpp.h>

int main() {
dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content);
/* 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());

Expand All @@ -13,9 +13,8 @@ int main() {
} 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 */
/* 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"));
/* 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()) {
Expand All @@ -27,15 +26,13 @@ int main() {
/* 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");
Expand All @@ -46,9 +43,37 @@ int main() {
/* change the channel name and edit the channel itself */
channel.set_name(name);
bot.channel_edit(channel);

event.reply("Channel name is now `" + name + "`.");
});
} else 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.
Henonicks marked this conversation as resolved.
Show resolved Hide resolved
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 + "`.");
});
}
});

Expand All @@ -67,11 +92,17 @@ int main() {

dpp::slashcommand msg_send("msg-send", "Send my message", bot.me.id);

bot.global_bulk_command_create({ msg_edit, channel_edit, msg_send });
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({ msg_edit, channel_edit, msg_send, embed_send, embed_edit });
}
});

bot.start(dpp::st_wait);

return 0;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
\page editing-channels-and-messages 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 \ref callback-functions.

\include{cpp} editing_messages.cpp

Before editing:

\image html stuff_edit1.png

After editing:

\image html stuff_edit2.png
\page editing-channels-and-messages 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 of the properties being identical to the existing one. Say you're editing a message. You need to have an object identical to the one in Discord. Then you replace what you need such as its content. NOTE: your bot can't edit messages sent by others.
Henonicks marked this conversation as resolved.
Show resolved Hide resolved

\note This example uses callback functions and embeds. To see more information about them, visit \ref callback-functions and \ref embed-message.
Henonicks marked this conversation as resolved.
Show resolved Hide resolved

\include{cpp} editing_messages.cpp

Before editing:

\image html stuff_edit1.png

After editing:

\image html stuff_edit2.png

Now let's send the embed:
Henonicks marked this conversation as resolved.
Show resolved Hide resolved

\image html stuff_edit3.png

And finally, edit it:

\image html stuff_edit4.png
Binary file added docpages/images/stuff_edit3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docpages/images/stuff_edit4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading