| 1 | #include <dpp/dpp.h> |
| 2 | |
| 3 | int main() { |
| 4 | /* the second argument is a bitmask of intents - i_message_content is needed to get messages */ |
| 5 | dpp::cluster bot("Token", dpp::i_default_intents | dpp::i_message_content); |
| 6 | |
| 7 | bot.on_log(dpp::utility::cout_logger()); |
| 8 | |
| 9 | /* The event is fired when someone issues your commands */ |
| 10 | bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { |
| 11 | if (event.command.get_command_name() == "embed-send") { |
| 12 | dpp::embed embed = dpp::embed() |
| 13 | .set_color(dpp::colors::sti_blue) |
| 14 | .set_title("like and subscribe") |
| 15 | .set_url("https://dpp.dev/") |
| 16 | .set_author("Some author", "https://dpp.dev/", "https://dpp.dev/DPP-Logo.png") |
| 17 | .set_description("Creator is <creator name>"); |
| 18 | event.reply(embed); |
| 19 | } else if (event.command.get_command_name() == "embed-edit") { |
| 20 | const auto description = std::get<std::string>(event.get_parameter("desc")); |
| 21 | |
| 22 | /* get the message to edit its embed after. here string will automatically be converted to snowflake */ |
| 23 | const dpp::snowflake msg_id = std::get<std::string>(event.get_parameter("msg-id")); |
| 24 | |
| 25 | bot.message_get(msg_id, event.command.channel_id, [&bot, description, event](const dpp::confirmation_callback_t& callback) { |
| 26 | if (callback.is_error()) { |
| 27 | event.reply("error"); |
| 28 | return; |
| 29 | } |
| 30 | auto message = callback.get<dpp::message>(); |
| 31 | auto& embeds = message.embeds; |
| 32 | |
| 33 | /* change the embed description and edit the message itself. |
| 34 | * since we're using a reference, what changes in embeds changes in message.embeds |
| 35 | */ |
| 36 | embeds[0].set_description(description); |
| 37 | |
| 38 | bot.message_edit(message); |
| 39 | event.reply("Embed description is now `" + description + "`."); |
| 40 | }); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | bot.on_ready([&bot](const dpp::ready_t& event) { |
| 45 | if (dpp::run_once <struct register_global_commands>()) { |
| 46 | dpp::slashcommand embed_send("embed-send", "Send my embed", bot.me.id); |
| 47 | |
| 48 | dpp::slashcommand embed_edit("embed-edit", "Edit an embed sent by the bot", bot.me.id); |
| 49 | |
| 50 | embed_edit.add_option(dpp::command_option(dpp::co_string, "msg-id", "ID of the embed to edit", true)); /* true for required option */ |
| 51 | embed_edit.add_option(dpp::command_option(dpp::co_string, "desc", "New description for the embed", true)); /* same here */ |
| 52 | |
| 53 | bot.global_bulk_command_create({ embed_send, embed_edit }); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | bot.start(dpp::st_wait); |
| 58 | return 0; |
| 59 | } |