From 97c59df63593b704fca9caa755c24a83e57eab51 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Thu, 14 Sep 2023 00:14:47 +0000 Subject: [PATCH] docs: interactions and components examples split --- docpages/example_code/commandhandler.cpp | 53 +++++ docpages/example_code/components.cpp | 57 ++++++ docpages/example_code/components2.cpp | 62 ++++++ docpages/example_code/components3.cpp | 57 ++++++ docpages/example_code/context_menus.cpp | 39 ++++ docpages/example_code/detecting_messages.cpp | 24 +++ docpages/example_code/ephemeral.cpp | 32 +++ docpages/example_code/making_threads1.cpp | 36 ++++ docpages/example_code/making_threads2.cpp | 57 ++++++ .../modal_dialog_interactions.cpp | 73 +++++++ docpages/example_code/private_messaging.cpp | 66 ++++++ docpages/example_code/slashcommands1.cpp | 47 +++++ docpages/example_code/slashcommands2.cpp | 47 +++++ docpages/example_code/slashcommands3.cpp | 41 ++++ docpages/example_code/slashcommands4.cpp | 42 ++++ docpages/example_code/subcommands.cpp | 72 +++++++ docpages/example_code/upload_parameter.cpp | 43 ++++ .../commandhandler.md | 57 +----- .../interactions_and_components/components.md | 60 +----- .../components2.md | 65 +----- .../components3.md | 60 +----- .../context_menus.md | 42 +--- .../detecting-messages.md | 27 +-- .../making_threads.md | 98 +-------- .../modal_dialog_interactions.md | 76 +------ .../private-messaging.md | 69 +------ .../slashcommands.md | 189 +----------------- .../subcommands.md | 75 +------ .../upload_parameter.md | 46 +---- .../user-only-messages.md | 35 +--- 30 files changed, 865 insertions(+), 882 deletions(-) create mode 100644 docpages/example_code/commandhandler.cpp create mode 100644 docpages/example_code/components.cpp create mode 100644 docpages/example_code/components2.cpp create mode 100644 docpages/example_code/components3.cpp create mode 100644 docpages/example_code/context_menus.cpp create mode 100644 docpages/example_code/detecting_messages.cpp create mode 100644 docpages/example_code/ephemeral.cpp create mode 100644 docpages/example_code/making_threads1.cpp create mode 100644 docpages/example_code/making_threads2.cpp create mode 100644 docpages/example_code/modal_dialog_interactions.cpp create mode 100644 docpages/example_code/private_messaging.cpp create mode 100644 docpages/example_code/slashcommands1.cpp create mode 100644 docpages/example_code/slashcommands2.cpp create mode 100644 docpages/example_code/slashcommands3.cpp create mode 100644 docpages/example_code/slashcommands4.cpp create mode 100644 docpages/example_code/subcommands.cpp create mode 100644 docpages/example_code/upload_parameter.cpp diff --git a/docpages/example_code/commandhandler.cpp b/docpages/example_code/commandhandler.cpp new file mode 100644 index 0000000000..ce7c6fb378 --- /dev/null +++ b/docpages/example_code/commandhandler.cpp @@ -0,0 +1,53 @@ +#include + +int main() +{ + /* If your bot only uses the "/" prefix, you can remove the intents here. */ + dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); + + bot.on_log(dpp::utility::cout_logger()); + + /* Create command handler, and specify prefixes */ + dpp::commandhandler command_handler(&bot); + /* Specifying a prefix of "/" tells the command handler it should also expect slash commands. Remove the .add_prefix(".") if you wish to only make it a slash command */ + command_handler.add_prefix(".").add_prefix("/"); + + bot.on_ready([&command_handler](const dpp::ready_t &event) { + + command_handler.add_command( + /* Command name */ + "ping", + + /* Parameters */ + { + {"testparameter", dpp::param_info(dpp::pt_string, true, "Optional test parameter") } + }, + + /* Command handler */ + [&command_handler](const std::string& command, const dpp::parameter_list_t& parameters, dpp::command_source src) { + std::string got_param; + if (!parameters.empty()) { + got_param = std::get(parameters[0].second); + } + command_handler.reply(dpp::message("Pong! -> " + got_param), src); + }, + + /* Command description */ + "A test ping command", + + /* Guild id (omit for a guild command) */ + 819556414099554344 + ); + + /* NOTE: We must call this to ensure slash commands are registered. + * This does a bulk register, which will replace other commands + * that are registered already! + */ + command_handler.register_commands(); + + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/components.cpp b/docpages/example_code/components.cpp new file mode 100644 index 0000000000..5caa90b23b --- /dev/null +++ b/docpages/example_code/components.cpp @@ -0,0 +1,57 @@ +#include +#include + +int main() { + + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "button") { + + /* Create a message */ + dpp::message msg(event.command.channel_id, "this text has a button"); + + /* Add an action row, and then a button within the action row. */ + msg.add_component( + dpp::component().add_component( + dpp::component(). + set_label("Click me!"). + set_type(dpp::cot_button). + set_emoji(dpp::unicode_emoji::smile). + set_style(dpp::cos_danger). + set_id("myid") + ) + ); + + /* Reply to the user with our message. */ + event.reply(msg); + } + }); + + /* When a user clicks your button, the on_button_click event will fire, + * containing the custom_id you defined in your button. + */ + bot.on_button_click([&bot](const dpp::button_click_t& event) { + /* Button clicks are still interactions, and must be replied to in some form to + * prevent the "this interaction has failed" message from Discord to the user. + */ + event.reply("You clicked: " + event.custom_id); + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + + /* Create and register a command when the bot is ready */ + bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id)); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/components2.cpp b/docpages/example_code/components2.cpp new file mode 100644 index 0000000000..d5d610bf20 --- /dev/null +++ b/docpages/example_code/components2.cpp @@ -0,0 +1,62 @@ +#include + +int main() { + + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "math") { + + /* Create a message */ + dpp::message msg(event.command.channel_id, "What is 5+5?"); + + /* Add an action row, and then 3 buttons within the action row. */ + msg.add_component( + dpp::component().add_component( + dpp::component(). + set_label("9"). + set_style(dpp::cos_primary). + set_id("9") + ).add_component( + dpp::component(). + set_label("10"). + set_style(dpp::cos_primary). + set_id("10") + ).add_component( + dpp::component(). + set_label("11"). + set_style(dpp::cos_primary). + set_id("11") + ) + ); + + /* Reply to the user with our message. */ + event.reply(msg); + } + }); + + bot.on_button_click([&bot](const dpp::button_click_t & event) { + if (event.custom_id == "10") { + event.reply(dpp::message("You got it right!").set_flags(dpp::m_ephemeral)); + } else { + event.reply(dpp::message("Wrong! Try again.").set_flags(dpp::m_ephemeral)); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + + /* Create and register a command when the bot is ready */ + bot.global_command_create(dpp::slashcommand("math", "A quick maths question!", bot.me.id)); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/components3.cpp b/docpages/example_code/components3.cpp new file mode 100644 index 0000000000..b69d48899d --- /dev/null +++ b/docpages/example_code/components3.cpp @@ -0,0 +1,57 @@ +#include +#include + +int main() { + + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "select") { + + /* Create a message */ + dpp::message msg(event.command.channel_id, "This text has a select menu!"); + + /* Add an action row, and a select menu within the action row. */ + msg.add_component( + dpp::component().add_component( + dpp::component(). + set_type(dpp::cot_selectmenu). + set_placeholder("Pick something"). + add_select_option(dpp::select_option("label1","value1","description1").set_emoji(dpp::unicode_emoji::smile)). + add_select_option(dpp::select_option("label2","value2","description2").set_emoji(dpp::unicode_emoji::slight_smile)). + set_id("myselectid") + ) + ); + + /* Reply to the user with our message. */ + event.reply(msg); + } + }); + + /* When a user clicks your select menu , the on_select_click event will fire, + * containing the custom_id you defined in your select menu. + */ + bot.on_select_click([&bot](const dpp::select_click_t & event) { + /* Select clicks are still interactions, and must be replied to in some form to + * prevent the "this interaction has failed" message from Discord to the user. + */ + event.reply("You clicked " + event.custom_id + " and chose: " + event.values[0]); + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + + /* Create and register a command when the bot is ready */ + bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id)); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/context_menus.cpp b/docpages/example_code/context_menus.cpp new file mode 100644 index 0000000000..ff7a32a555 --- /dev/null +++ b/docpages/example_code/context_menus.cpp @@ -0,0 +1,39 @@ +#include +#include + +int main() +{ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + /* Use the on_user_context_menu event to look for user context menu actions */ + bot.on_user_context_menu([&](const dpp::user_context_menu_t& event) { + + /* check if the context menu name is High Five */ + if (event.command.get_command_name() == "high five") { + dpp::user user = event.get_user(); // the user who the command has been issued on + dpp::user author = event.command.get_issuing_user(); // the user who clicked on the context menu + event.reply(author.get_mention() + " slapped " + user.get_mention()); + } + }); + + bot.on_ready([&bot](const dpp::ready_t &event) { + if (dpp::run_once()) { + + /* Create the command */ + dpp::slashcommand command; + command.set_name("High Five"); + command.set_application_id(bot.me.id); + command.set_type(dpp::ctxm_user); + + /* Register the command */ + bot.guild_command_create(command, 857692897221033129); /* Replace this with the guild id you want */ + } + }); + + /* Start bot */ + bot.start(dpp::st_wait); + + return 0; +} \ No newline at end of file diff --git a/docpages/example_code/detecting_messages.cpp b/docpages/example_code/detecting_messages.cpp new file mode 100644 index 0000000000..ecb0805230 --- /dev/null +++ b/docpages/example_code/detecting_messages.cpp @@ -0,0 +1,24 @@ +#include + +int main() +{ + /* Create the bot, but with our intents so we can use 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 the bot detects a message in any server and any channel it has access to. */ + bot.on_message_create([&bot](const dpp::message_create_t& event) { + + /* See if the message contains the phrase we want to check for. + * If there's at least a single match, we reply and say it's not allowed. + */ + if (event.msg.content.find("bad word") != std::string::npos) { + event.reply("That is not allowed here. Please, mind your language!", true); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} \ No newline at end of file diff --git a/docpages/example_code/ephemeral.cpp b/docpages/example_code/ephemeral.cpp new file mode 100644 index 0000000000..5fb4f7dc0c --- /dev/null +++ b/docpages/example_code/ephemeral.cpp @@ -0,0 +1,32 @@ +#include + +int main() +{ + /* Create the bot */ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "hello") { + + /* Reply to the user, but only let them see the response. */ + event.reply(dpp::message("Hello! How are you today?").set_flags(dpp::m_ephemeral)); + } + }); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create and Register the command */ + bot.global_command_create(dpp::slashcommand("hello", "Hello there!", bot.me.id)); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/making_threads1.cpp b/docpages/example_code/making_threads1.cpp new file mode 100644 index 0000000000..36f6db627d --- /dev/null +++ b/docpages/example_code/making_threads1.cpp @@ -0,0 +1,36 @@ +#include + +int main() +{ + /* Create the bot */ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + /* The event is fired when the bot detects a message in any server and any channel it has access to. */ + bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { + /* Check which command they ran */ + if (event.command.get_command_name() == "create-thread") { + /* Here we create a thread in the current channel. It will expire after 60 minutes of inactivity. We'll also allow other mods to join, and we won't add a slowdown timer. */ + bot.thread_create("Cool thread!", event.command.channel_id, 60, dpp::channel_type::CHANNEL_PUBLIC_THREAD, true, 0, [event](const dpp::confirmation_callback_t& callback) { + if (callback.is_error()) { + event.reply("Failed to create a thread!"); + return; + } + + event.reply("Created a thread for you!"); + }); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + /* Create and register the command */ + bot.global_command_create(dpp::slashcommand("create-thread", "Create a thread!", bot.me.id)); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/making_threads2.cpp b/docpages/example_code/making_threads2.cpp new file mode 100644 index 0000000000..21037ff233 --- /dev/null +++ b/docpages/example_code/making_threads2.cpp @@ -0,0 +1,57 @@ +#include + +int main() +{ + /* Create the bot */ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + /* The event is fired when the bot detects a message in any server and any channel it has access to. */ + bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) { + /* Check which command they ran */ + if (event.command.get_command_name() == "message-thread") { + /* Get all active threads in a guild. */ + bot.threads_get_active(event.command.guild_id, [&bot, event](const dpp::confirmation_callback_t& callback) { + if (callback.is_error()) { + event.reply("Failed to get threads!"); + return; + } + + /* Get the list of active threads in the guild. */ + auto threads = callback.get(); + + dpp::snowflake thread_id; + + /* Loop through the threads, getting each value in the map. Then we get the first value and then break off. + * The reason we're getting only the first value is because, for this example, we'll just assume you've only got a single active thread (the one created by the bot) + */ + for (const auto& _thread : threads) { + thread_id = _thread.first; + break; + } + + /* Send a message in the first thread we find. */ + bot.message_create(dpp::message(thread_id, "Hey, I'm first to message in a cool thread!"), [event](const dpp::confirmation_callback_t& callback2) { + if (callback2.is_error()) { + event.reply("Failed to send a message in a thread."); + return; + } + + event.reply("I've sent a message in the specified thread."); + }); + }); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + /* Create and register the command */ + bot.global_command_create(dpp::slashcommand("message-thread", "Message a thread!", bot.me.id)); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/modal_dialog_interactions.cpp b/docpages/example_code/modal_dialog_interactions.cpp new file mode 100644 index 0000000000..764581d468 --- /dev/null +++ b/docpages/example_code/modal_dialog_interactions.cpp @@ -0,0 +1,73 @@ +#include +#include + +int main(int argc, char const *argv[]) +{ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { + /* Check for our /dialog command */ + if (event.command.get_command_name() == "dialog") { + + /* Instantiate an interaction_modal_response object */ + dpp::interaction_modal_response modal("my_modal", "Please enter stuff"); + + /* Add a text component */ + modal.add_component( + dpp::component(). + set_label("Short type rammel"). + set_id("field_id"). + set_type(dpp::cot_text). + set_placeholder("gumd"). + set_min_length(5). + set_max_length(50). + set_text_style(dpp::text_short) + ); + + /* Add another text component in the next row, as required by Discord */ + modal.add_row(); + modal.add_component( + dpp::component(). + set_label("Type rammel"). + set_id("field_id2"). + set_type(dpp::cot_text). + set_placeholder("gumf"). + set_min_length(1). + set_max_length(2000). + set_text_style(dpp::text_paragraph) + ); + + /* Trigger the dialog box. All dialog boxes are ephemeral */ + event.dialog(modal); + } + }); + + /* This event handles form submission for the modal dialog we create above */ + bot.on_form_submit([&](const dpp::form_submit_t & event) { + + /* For this simple example we know the first element of the first row ([0][0]) is value type string. + * In the real world it may not be safe to make such assumptions! + */ + std::string v = std::get(event.components[0].components[0].value); + + dpp::message m; + m.set_content("You entered: " + v).set_flags(dpp::m_ephemeral); + + /* Emit a reply. Form submission is still an interaction and must generate some form of reply! */ + event.reply(m); + }); + + bot.on_ready([&](const dpp::ready_t & event) { + if (dpp::run_once()) { + /* Create a slash command and register it as a global command */ + bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id)); + } + }); + + /* Start bot */ + + bot.start(dpp::st_wait); + return 0; +} diff --git a/docpages/example_code/private_messaging.cpp b/docpages/example_code/private_messaging.cpp new file mode 100644 index 0000000000..3ec2a38b20 --- /dev/null +++ b/docpages/example_code/private_messaging.cpp @@ -0,0 +1,66 @@ +#include + +int main() +{ + /* Create the bot */ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "pm") { + + dpp::snowflake user; + + /* If there was no specified user, we set the "user" variable to the command author (issuing user). */ + if (event.get_parameter("user").index() == 0) { + user = event.command.get_issuing_user().id; + } else { /* Otherwise, we set it to the specified user! */ + user = std::get(event.get_parameter("user")); + } + + /* Send a message to the user set above. */ + bot.direct_message_create(user, dpp::message("Here's a private message!"), [event, user](const dpp::confirmation_callback_t& callback){ + /* If the callback errors, we want to send a message telling the author that something went wrong. */ + if (callback.is_error()) { + /* Here, we want the error message to be different if the user we're trying to send a message to is the command author. */ + if (user == event.command.get_issuing_user().id) { + event.reply(dpp::message("I couldn't send you a message.").set_flags(dpp::m_ephemeral)); + } else { + event.reply(dpp::message("I couldn't send a message to that user. Please check that is a valid user!").set_flags(dpp::m_ephemeral)); + } + + return; + } + + /* We do the same here, so the message is different if it's to the command author or if it's to a specified user. */ + if (user == event.command.get_issuing_user().id) { + event.reply(dpp::message("I've sent you a private message.").set_flags(dpp::m_ephemeral)); + } else { + event.reply(dpp::message("I've sent a message to that user.").set_flags(dpp::m_ephemeral)); + } + }); + } + }); + + bot.on_ready([&bot](const dpp::ready_t& event) { + if (dpp::run_once()) { + + /* Register the command */ + dpp::slashcommand command("pm", "Send a private message.", bot.me.id); + + /* Add the option for a user mention that isn't required */ + command.add_option(dpp::command_option(dpp::co_mentionable, "user", "The user to message", false)); + + /* Register the command */ + bot.global_command_create(command); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/slashcommands1.cpp b/docpages/example_code/slashcommands1.cpp new file mode 100644 index 0000000000..0f5f4bbf9c --- /dev/null +++ b/docpages/example_code/slashcommands1.cpp @@ -0,0 +1,47 @@ +#include + +int main() +{ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "blep") { + + /* Fetch a parameter value from the command parameters */ + std::string animal = std::get(event.get_parameter("animal")); + + /* Reply to the command. There is an overloaded version of this + * call that accepts a dpp::message so you can send embeds. + */ + event.reply(std::string("Blep! You chose") + animal); + } + }); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create a new global command on ready event */ + dpp::slashcommand newcommand("blep", "Send a random adorable animal photo", bot.me.id); + newcommand.add_option( + dpp::command_option(dpp::co_string, "animal", "The type of animal", true). + add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))). + add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))). + add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin") + ) + ) + ); + + /* Register the command */ + bot.global_command_create(newcommand); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/slashcommands2.cpp b/docpages/example_code/slashcommands2.cpp new file mode 100644 index 0000000000..1eb9c4159e --- /dev/null +++ b/docpages/example_code/slashcommands2.cpp @@ -0,0 +1,47 @@ +#include + +int main() +{ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "blep") { + + /* Fetch a parameter value from the command parameters */ + std::string animal = std::get(event.get_parameter("animal")); + + /* Reply to the command. There is an overloaded version of this + * call that accepts a dpp::message so you can send embeds. + */ + event.reply(std::string("Blep! You chose") + animal); + } + }); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create a new global command on ready event */ + dpp::slashcommand newcommand("blep", "Send a random adorable animal photo", bot.me.id); + newcommand.add_option( + dpp::command_option(dpp::co_string, "animal", "The type of animal", true). + add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))). + add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))). + add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin") + ) + ) + ); + + /* Register the command */ + bot.guild_command_create(newcommand, 857692897221033129); /* Replace this with the guild id you want */ + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/slashcommands3.cpp b/docpages/example_code/slashcommands3.cpp new file mode 100644 index 0000000000..578d3c3814 --- /dev/null +++ b/docpages/example_code/slashcommands3.cpp @@ -0,0 +1,41 @@ +#include + +int main() +{ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "ping") { + event.reply("Pong!"); + } else if (event.command.get_command_name() == "pong") { + event.reply("Ping!"); + } else if (event.command.get_command_name() == "ding") { + event.reply("Dong!"); + } else if (event.command.get_command_name() == "dong") { + event.reply("Ding!"); + } + }); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create some commands */ + dpp::slashcommand pingcommand("ping", "Pong!", bot.me.id); + dpp::slashcommand pongcommand("pong", "Ping!", bot.me.id); + dpp::slashcommand dingcommand("ding", "Dong!", bot.me.id); + dpp::slashcommand dongcommand("dong", "Ding!", bot.me.id); + + /* Register our commands in a list using bulk */ + bot.global_bulk_command_create({ pingcommand, pongcommand, dingcommand, dongcommand }); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/slashcommands4.cpp b/docpages/example_code/slashcommands4.cpp new file mode 100644 index 0000000000..3f10950d6f --- /dev/null +++ b/docpages/example_code/slashcommands4.cpp @@ -0,0 +1,42 @@ +#include + +int main() +{ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "ping") { + event.reply("Pong!"); + } else if (event.command.get_command_name() == "pong") { + event.reply("Ping!"); + } else if (event.command.get_command_name() == "ding") { + event.reply("Dong!"); + } else if (event.command.get_command_name() == "dong") { + event.reply("Ding!"); + } + }); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create some commands */ + dpp::slashcommand pingcommand("ping", "Pong!", bot.me.id); + dpp::slashcommand pongcommand("pong", "Ping!", bot.me.id); + dpp::slashcommand dingcommand("ding", "Dong!", bot.me.id); + dpp::slashcommand dongcommand("dong", "Ding!", bot.me.id); + + /* Register our commands in a list using bulk */ + bot.guild_bulk_command_create({ pingcommand, pongcommand, dingcommand, dongcommand }, 857692897221033129); + + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/subcommands.cpp b/docpages/example_code/subcommands.cpp new file mode 100644 index 0000000000..ac8c65da18 --- /dev/null +++ b/docpages/example_code/subcommands.cpp @@ -0,0 +1,72 @@ +#include +#include + +int main() { + + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + /* Executes on ready. */ + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + /* Define a slash command. */ + dpp::slashcommand image("image", "Send a specific image.", bot.me.id); + image.add_option( + /* Create a subcommand type option for "dog". */ + dpp::command_option(dpp::co_sub_command, "dog", "Send a picture of a dog."). + add_option(dpp::command_option(dpp::co_user, "user", "User to turn into a dog.", false)) + ); + image.add_option( + /* Create another subcommand type option for "cat". */ + dpp::command_option(dpp::co_sub_command, "cat", "Send a picture of a cat."). + add_option(dpp::command_option(dpp::co_user, "user", "User to turn into a cat.", false)) + ); + /* Create command */ + bot.global_command_create(image); + } + }); + + /* Use the on_slashcommand event to look for commands */ + bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) { + dpp::interaction interaction = event.command; + dpp::command_interaction cmd_data = interaction.get_command_interaction(); + /* Check if the command is the image command. */ + if (interaction.get_command_name() == "image") { + /* Get the sub command */ + auto subcommand = cmd_data.options[0]; + /* Check if the subcommand is "dog" */ + if (subcommand.name == "dog") { + /* Checks if the subcommand has any options. */ + if (!subcommand.options.empty()) { + /* Get the user from the parameter */ + dpp::user user = interaction.get_resolved_user( + subcommand.get_value(0) + ); + event.reply(user.get_mention() + " has now been turned into a dog."); + } else { + /* Reply if there were no options.. */ + event.reply("No user specified"); + } + } + /* Check if the subcommand is "cat" */ + if (subcommand.name == "cat") { + /* Checks if the subcommand has any options. */ + if (!subcommand.options.empty()) { + /* Get the user from the parameter */ + dpp::user user = interaction.get_resolved_user( + subcommand.get_value(0) + ); + event.reply(user.get_mention() + " has now been turned into a cat."); + } else { + /* Reply if there were no options.. */ + event.reply("No user specified"); + } + } + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_code/upload_parameter.cpp b/docpages/example_code/upload_parameter.cpp new file mode 100644 index 0000000000..dc78829c44 --- /dev/null +++ b/docpages/example_code/upload_parameter.cpp @@ -0,0 +1,43 @@ +#include + +int main() +{ + dpp::cluster bot("token"); + + 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) { + + /* Check which command they ran */ + if (event.command.get_command_name() == "show") { + + /* Get the file id from the parameter attachment. */ + dpp::snowflake file_id = std::get(event.get_parameter("file")); + + /* Get the attachment that the user inputted from the file id. */ + dpp::attachment att = event.command.get_resolved_attachment(file_id); + + /* Reply with the file as a URL. */ + event.reply(att.url); + } + }); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create a new command. */ + dpp::slashcommand newcommand("show", "Show an uploaded file", bot.me.id); + + /* Add a parameter option. */ + newcommand.add_option(dpp::command_option(dpp::co_attachment, "file", "Select an image")); + + /* Register the command */ + bot.global_command_create(newcommand); + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_programs/interactions_and_components/commandhandler.md b/docpages/example_programs/interactions_and_components/commandhandler.md index 140fc9aab3..69b84caf6b 100644 --- a/docpages/example_programs/interactions_and_components/commandhandler.md +++ b/docpages/example_programs/interactions_and_components/commandhandler.md @@ -12,59 +12,4 @@ and allows you to decide how and where commands should be routed, either to an o \warning As of [August 30th, 2022](https://support-dev.discord.com/hc/en-us/articles/6025578854295-Why-We-Moved-to-Slash-Commands), you are advised to only be using slash commands, not messages for commands. To prevent the command handler from handling commands with messages, you should only use the "/" prefix. If you wish to still use messages for commands, this tutorial will still cover it but, again, it is discouraged by Discord. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() -{ - /* If your bot only uses the "/" prefix, you can remove the intents here. */ - dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); - - bot.on_log(dpp::utility::cout_logger()); - - /* Create command handler, and specify prefixes */ - dpp::commandhandler command_handler(&bot); - /* Specifying a prefix of "/" tells the command handler it should also expect slash commands. Remove the .add_prefix(".") if you wish to only make it a slash command */ - command_handler.add_prefix(".").add_prefix("/"); - - bot.on_ready([&command_handler](const dpp::ready_t &event) { - - command_handler.add_command( - /* Command name */ - "ping", - - /* Parameters */ - { - {"testparameter", dpp::param_info(dpp::pt_string, true, "Optional test parameter") } - }, - - /* Command handler */ - [&command_handler](const std::string& command, const dpp::parameter_list_t& parameters, dpp::command_source src) { - std::string got_param; - if (!parameters.empty()) { - got_param = std::get(parameters[0].second); - } - command_handler.reply(dpp::message("Pong! -> " + got_param), src); - }, - - /* Command description */ - "A test ping command", - - /* Guild id (omit for a guild command) */ - 819556414099554344 - ); - - /* NOTE: We must call this to ensure slash commands are registered. - * This does a bulk register, which will replace other commands - * that are registered already! - */ - command_handler.register_commands(); - - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - +\include{cpp} commandhandler.cpp diff --git a/docpages/example_programs/interactions_and_components/components.md b/docpages/example_programs/interactions_and_components/components.md index 90eeca928a..dfadf95004 100644 --- a/docpages/example_programs/interactions_and_components/components.md +++ b/docpages/example_programs/interactions_and_components/components.md @@ -3,65 +3,7 @@ Discord's newest features support sending buttons alongside messages, which when clicked by the user trigger an interaction which is routed by D++ as an `on_button_click` event. To make use of this, use this code as in this example. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include -#include - -int main() { - - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "button") { - - /* Create a message */ - dpp::message msg(event.command.channel_id, "this text has a button"); - - /* Add an action row, and then a button within the action row. */ - msg.add_component( - dpp::component().add_component( - dpp::component(). - set_label("Click me!"). - set_type(dpp::cot_button). - set_emoji(dpp::unicode_emoji::smile). - set_style(dpp::cos_danger). - set_id("myid") - ) - ); - - /* Reply to the user with our message. */ - event.reply(msg); - } - }); - - /* When a user clicks your button, the on_button_click event will fire, - * containing the custom_id you defined in your button. - */ - bot.on_button_click([&bot](const dpp::button_click_t& event) { - /* Button clicks are still interactions, and must be replied to in some form to - * prevent the "this interaction has failed" message from Discord to the user. - */ - event.reply("You clicked: " + event.custom_id); - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - - /* Create and register a command when the bot is ready */ - bot.global_command_create(dpp::slashcommand("button", "Send a message with a button!", bot.me.id)); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} components.cpp When the feature is functioning, the code below will produce buttons on the reply message like in the image below: diff --git a/docpages/example_programs/interactions_and_components/components2.md b/docpages/example_programs/interactions_and_components/components2.md index 1bc4ae8599..5d306ac47c 100644 --- a/docpages/example_programs/interactions_and_components/components2.md +++ b/docpages/example_programs/interactions_and_components/components2.md @@ -2,70 +2,7 @@ This example demonstrates adding multiple buttons, receiving button clicks and sending response messages. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() { - - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "math") { - - /* Create a message */ - dpp::message msg(event.command.channel_id, "What is 5+5?"); - - /* Add an action row, and then 3 buttons within the action row. */ - msg.add_component( - dpp::component().add_component( - dpp::component(). - set_label("9"). - set_style(dpp::cos_primary). - set_id("9") - ).add_component( - dpp::component(). - set_label("10"). - set_style(dpp::cos_primary). - set_id("10") - ).add_component( - dpp::component(). - set_label("11"). - set_style(dpp::cos_primary). - set_id("11") - ) - ); - - /* Reply to the user with our message. */ - event.reply(msg); - } - }); - - bot.on_button_click([&bot](const dpp::button_click_t & event) { - if (event.custom_id == "10") { - event.reply(dpp::message("You got it right!").set_flags(dpp::m_ephemeral)); - } else { - event.reply(dpp::message("Wrong! Try again.").set_flags(dpp::m_ephemeral)); - } - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - - /* Create and register a command when the bot is ready */ - bot.global_command_create(dpp::slashcommand("math", "A quick maths question!", bot.me.id)); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} components2.cpp This code will send a different message for correct and incorrect answers. diff --git a/docpages/example_programs/interactions_and_components/components3.md b/docpages/example_programs/interactions_and_components/components3.md index 359a101c6b..e3ac023c23 100644 --- a/docpages/example_programs/interactions_and_components/components3.md +++ b/docpages/example_programs/interactions_and_components/components3.md @@ -2,62 +2,4 @@ This example demonstrates creating a select menu, receiving select menu clicks and sending a response message. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include -#include - -int main() { - - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "select") { - - /* Create a message */ - dpp::message msg(event.command.channel_id, "This text has a select menu!"); - - /* Add an action row, and a select menu within the action row. */ - msg.add_component( - dpp::component().add_component( - dpp::component(). - set_type(dpp::cot_selectmenu). - set_placeholder("Pick something"). - add_select_option(dpp::select_option("label1","value1","description1").set_emoji(dpp::unicode_emoji::smile)). - add_select_option(dpp::select_option("label2","value2","description2").set_emoji(dpp::unicode_emoji::slight_smile)). - set_id("myselectid") - ) - ); - - /* Reply to the user with our message. */ - event.reply(msg); - } - }); - - /* When a user clicks your select menu , the on_select_click event will fire, - * containing the custom_id you defined in your select menu. - */ - bot.on_select_click([&bot](const dpp::select_click_t & event) { - /* Select clicks are still interactions, and must be replied to in some form to - * prevent the "this interaction has failed" message from Discord to the user. - */ - event.reply("You clicked " + event.custom_id + " and chose: " + event.values[0]); - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - - /* Create and register a command when the bot is ready */ - bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id)); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} components3.cpp diff --git a/docpages/example_programs/interactions_and_components/context_menus.md b/docpages/example_programs/interactions_and_components/context_menus.md index d810fcc0a0..cd451998d6 100644 --- a/docpages/example_programs/interactions_and_components/context_menus.md +++ b/docpages/example_programs/interactions_and_components/context_menus.md @@ -8,47 +8,7 @@ Context menus are application commands that appear on the context menu (right cl The following example shows how to create and handle **user context menus** for message context menus, read the notice above. -~~~~~~~~~~{.cpp} -#include -#include - -int main() -{ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - /* Use the on_user_context_menu event to look for user context menu actions */ - bot.on_user_context_menu([&](const dpp::user_context_menu_t& event) { - - /* check if the context menu name is High Five */ - if (event.command.get_command_name() == "high five") { - dpp::user user = event.get_user(); // the user who the command has been issued on - dpp::user author = event.command.get_issuing_user(); // the user who clicked on the context menu - event.reply(author.get_mention() + " slapped " + user.get_mention()); - } - }); - - bot.on_ready([&bot](const dpp::ready_t &event) { - if (dpp::run_once()) { - - /* Create the command */ - dpp::slashcommand command; - command.set_name("High Five"); - command.set_application_id(bot.me.id); - command.set_type(dpp::ctxm_user); - - /* Register the command */ - bot.guild_command_create(command, 857692897221033129); /* Replace this with the guild id you want */ - } - }); - - /* Start bot */ - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} context_menus.cpp It registers a guild command that can be called by right-clicking a user and clicking on the created menu. diff --git a/docpages/example_programs/interactions_and_components/detecting-messages.md b/docpages/example_programs/interactions_and_components/detecting-messages.md index a3278c85a5..2964f29ea3 100644 --- a/docpages/example_programs/interactions_and_components/detecting-messages.md +++ b/docpages/example_programs/interactions_and_components/detecting-messages.md @@ -4,32 +4,7 @@ Sometimes, you may want to listen out for a message, rather than a command. This \warning As of August 30th, 2022, Discord made Message Content a privileged intent. Whilst this means you can still use prefixed messages as commands, Discord does not encourage this and heavily suggests you use \ref slashcommands "slash commands". If you wish to create commands, use \ref slashcommands "slash commands", not messages. -~~~~~~~~~~{.cpp} -#include - -int main() -{ - /* Create the bot, but with our intents so we can use 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 the bot detects a message in any server and any channel it has access to. */ - bot.on_message_create([&bot](const dpp::message_create_t& event) { - - /* See if the message contains the phrase we want to check for. - * If there's at least a single match, we reply and say it's not allowed. - */ - if (event.msg.content.find("bad word") != std::string::npos) { - event.reply("That is not allowed here. Please, mind your language!", true); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} detecting_messages.cpp If all went well, you should have something like this! diff --git a/docpages/example_programs/interactions_and_components/making_threads.md b/docpages/example_programs/interactions_and_components/making_threads.md index 25822e0ba6..ee24e614d6 100644 --- a/docpages/example_programs/interactions_and_components/making_threads.md +++ b/docpages/example_programs/interactions_and_components/making_threads.md @@ -5,110 +5,16 @@ A new feature added to Discord recently is `Threads`, these allow you to break o In this tutorial, we'll be going through how to create a thread and how to talk in a thread. First, let's go through creating a thread. -~~~~~~~~~~{.cpp} -#include -int main() -{ - /* Create the bot */ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - /* The event is fired when the bot detects a message in any server and any channel it has access to. */ - bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { - /* Check which command they ran */ - if (event.command.get_command_name() == "create-thread") { - /* Here we create a thread in the current channel. It will expire after 60 minutes of inactivity. We'll also allow other mods to join, and we won't add a slowdown timer. */ - bot.thread_create("Cool thread!", event.command.channel_id, 60, dpp::channel_type::CHANNEL_PUBLIC_THREAD, true, 0, [event](const dpp::confirmation_callback_t& callback) { - if (callback.is_error()) { - event.reply("Failed to create a thread!"); - return; - } - - event.reply("Created a thread for you!"); - }); - } - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - /* Create and register the command */ - bot.global_command_create(dpp::slashcommand("create-thread", "Create a thread!", bot.me.id)); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} making_threads1.cpp If all went well, you'll see that the bot has successfully created a thread! \image html creating_thread.png Now, let's cover talking in that thread from a channel. It's worth noting that we will be assuming that the thread you just created is the only thread in your server! -~~~~~~~~~~{.cpp} - -#include - -int main() -{ - /* Create the bot */ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - /* The event is fired when the bot detects a message in any server and any channel it has access to. */ - bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) { - /* Check which command they ran */ - if (event.command.get_command_name() == "message-thread") { - /* Get all active threads in a guild. */ - bot.threads_get_active(event.command.guild_id, [&bot, event](const dpp::confirmation_callback_t& callback) { - if (callback.is_error()) { - event.reply("Failed to get threads!"); - return; - } - - /* Get the list of active threads in the guild. */ - auto threads = callback.get(); - - dpp::snowflake thread_id; - - /* Loop through the threads, getting each value in the map. Then we get the first value and then break off. - * The reason we're getting only the first value is because, for this example, we'll just assume you've only got a single active thread (the one created by the bot) - */ - for (const auto& _thread : threads) { - thread_id = _thread.first; - break; - } - - /* Send a message in the first thread we find. */ - bot.message_create(dpp::message(thread_id, "Hey, I'm first to message in a cool thread!"), [event](const dpp::confirmation_callback_t& callback2) { - if (callback2.is_error()) { - event.reply("Failed to send a message in a thread."); - return; - } - - event.reply("I've sent a message in the specified thread."); - }); - }); - } - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - /* Create and register the command */ - bot.global_command_create(dpp::slashcommand("message-thread", "Message a thread!", bot.me.id)); - } - }); - - bot.start(dpp::st_wait); - return 0; -} -~~~~~~~~~~ +\include{cpp} making_threads2.cpp After that, you'll be able to see your bot send a message in your thread! diff --git a/docpages/example_programs/interactions_and_components/modal_dialog_interactions.md b/docpages/example_programs/interactions_and_components/modal_dialog_interactions.md index f9a99bcde6..bb63048202 100644 --- a/docpages/example_programs/interactions_and_components/modal_dialog_interactions.md +++ b/docpages/example_programs/interactions_and_components/modal_dialog_interactions.md @@ -4,81 +4,7 @@ Modal dialog interactions are a new Discord API feature that allow you to have p Each dialog box may have up to five rows of input fields. The example below demonstrates a simple setup with just one text input: -~~~~~~~~~~{.cpp} -#include -#include - -int main(int argc, char const *argv[]) -{ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { - /* Check for our /dialog command */ - if (event.command.get_command_name() == "dialog") { - - /* Instantiate an interaction_modal_response object */ - dpp::interaction_modal_response modal("my_modal", "Please enter stuff"); - - /* Add a text component */ - modal.add_component( - dpp::component(). - set_label("Short type rammel"). - set_id("field_id"). - set_type(dpp::cot_text). - set_placeholder("gumd"). - set_min_length(5). - set_max_length(50). - set_text_style(dpp::text_short) - ); - - /* Add another text component in the next row, as required by Discord */ - modal.add_row(); - modal.add_component( - dpp::component(). - set_label("Type rammel"). - set_id("field_id2"). - set_type(dpp::cot_text). - set_placeholder("gumf"). - set_min_length(1). - set_max_length(2000). - set_text_style(dpp::text_paragraph) - ); - - /* Trigger the dialog box. All dialog boxes are ephemeral */ - event.dialog(modal); - } - }); - - /* This event handles form submission for the modal dialog we create above */ - bot.on_form_submit([&](const dpp::form_submit_t & event) { - - /* For this simple example we know the first element of the first row ([0][0]) is value type string. - * In the real world it may not be safe to make such assumptions! - */ - std::string v = std::get(event.components[0].components[0].value); - - dpp::message m; - m.set_content("You entered: " + v).set_flags(dpp::m_ephemeral); - - /* Emit a reply. Form submission is still an interaction and must generate some form of reply! */ - event.reply(m); - }); - - bot.on_ready([&](const dpp::ready_t & event) { - if (dpp::run_once()) { - /* Create a slash command and register it as a global command */ - bot.global_command_create(dpp::slashcommand("dialog", "Make a modal dialog box", bot.me.id)); - } - }); - - /* Start bot */ - - bot.start(dpp::st_wait); - return 0; -} -~~~~~~~~~~ +\include{cpp} modal_dialog_interactions.cpp If you compile and run this program and wait for the global command to register, typing `/dialog` will present you with a dialog box like the one below: diff --git a/docpages/example_programs/interactions_and_components/private-messaging.md b/docpages/example_programs/interactions_and_components/private-messaging.md index 1d6c4e7037..9460449e8e 100644 --- a/docpages/example_programs/interactions_and_components/private-messaging.md +++ b/docpages/example_programs/interactions_and_components/private-messaging.md @@ -4,74 +4,7 @@ Sometimes it's simply not enough to ping someone in a server with a message, and \note This tutorial makes use of callbacks. For more information about that, visit \ref callback-functions "Using Callback Functions". -~~~~~~~~~~{.cpp} -#include - -int main() -{ - /* Create the bot */ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "pm") { - - dpp::snowflake user; - - /* If there was no specified user, we set the "user" variable to the command author (issuing user). */ - if (event.get_parameter("user").index() == 0) { - user = event.command.get_issuing_user().id; - } else { /* Otherwise, we set it to the specified user! */ - user = std::get(event.get_parameter("user")); - } - - /* Send a message to the user set above. */ - bot.direct_message_create(user, dpp::message("Here's a private message!"), [event, user](const dpp::confirmation_callback_t& callback){ - /* If the callback errors, we want to send a message telling the author that something went wrong. */ - if (callback.is_error()) { - /* Here, we want the error message to be different if the user we're trying to send a message to is the command author. */ - if (user == event.command.get_issuing_user().id) { - event.reply(dpp::message("I couldn't send you a message.").set_flags(dpp::m_ephemeral)); - } else { - event.reply(dpp::message("I couldn't send a message to that user. Please check that is a valid user!").set_flags(dpp::m_ephemeral)); - } - - return; - } - - /* We do the same here, so the message is different if it's to the command author or if it's to a specified user. */ - if (user == event.command.get_issuing_user().id) { - event.reply(dpp::message("I've sent you a private message.").set_flags(dpp::m_ephemeral)); - } else { - event.reply(dpp::message("I've sent a message to that user.").set_flags(dpp::m_ephemeral)); - } - }); - } - }); - - bot.on_ready([&bot](const dpp::ready_t& event) { - if (dpp::run_once()) { - - /* Register the command */ - dpp::slashcommand command("pm", "Send a private message.", bot.me.id); - - /* Add the option for a user mention that isn't required */ - command.add_option(dpp::command_option(dpp::co_mentionable, "user", "The user to message", false)); - - /* Register the command */ - bot.global_command_create(command); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} private_messaging.cpp That's it! Now, you should have something like this: diff --git a/docpages/example_programs/interactions_and_components/slashcommands.md b/docpages/example_programs/interactions_and_components/slashcommands.md index 57f3bac606..a6d7ea0bdc 100644 --- a/docpages/example_programs/interactions_and_components/slashcommands.md +++ b/docpages/example_programs/interactions_and_components/slashcommands.md @@ -12,199 +12,18 @@ dpp::interaction_create_t::reply has two overloaded versions of the method, one This first example goes over creating a single command globally. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() -{ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "blep") { - - /* Fetch a parameter value from the command parameters */ - std::string animal = std::get(event.get_parameter("animal")); - - /* Reply to the command. There is an overloaded version of this - * call that accepts a dpp::message so you can send embeds. - */ - event.reply(std::string("Blep! You chose") + animal); - } - }); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create a new global command on ready event */ - dpp::slashcommand newcommand("blep", "Send a random adorable animal photo", bot.me.id); - newcommand.add_option( - dpp::command_option(dpp::co_string, "animal", "The type of animal", true). - add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))). - add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))). - add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin") - ) - ) - ); - - /* Register the command */ - bot.global_command_create(newcommand); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} slashcommands1.cpp This second example goes over creating a single command but only for a guild, this means that the command can not be accessed anywhere else but the guild specified. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() -{ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "blep") { - - /* Fetch a parameter value from the command parameters */ - std::string animal = std::get(event.get_parameter("animal")); - - /* Reply to the command. There is an overloaded version of this - * call that accepts a dpp::message so you can send embeds. - */ - event.reply(std::string("Blep! You chose") + animal); - } - }); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create a new global command on ready event */ - dpp::slashcommand newcommand("blep", "Send a random adorable animal photo", bot.me.id); - newcommand.add_option( - dpp::command_option(dpp::co_string, "animal", "The type of animal", true). - add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))). - add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))). - add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin") - ) - ) - ); - - /* Register the command */ - bot.guild_command_create(newcommand, 857692897221033129); /* Replace this with the guild id you want */ - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} slashcommands2.cpp This third example goes over creating four commands globally, using the bulk create method. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() -{ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "ping") { - event.reply("Pong!"); - } else if (event.command.get_command_name() == "pong") { - event.reply("Ping!"); - } else if (event.command.get_command_name() == "ding") { - event.reply("Dong!"); - } else if (event.command.get_command_name() == "dong") { - event.reply("Ding!"); - } - }); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create some commands */ - dpp::slashcommand pingcommand("ping", "Pong!", bot.me.id); - dpp::slashcommand pongcommand("pong", "Ping!", bot.me.id); - dpp::slashcommand dingcommand("ding", "Dong!", bot.me.id); - dpp::slashcommand dongcommand("dong", "Ding!", bot.me.id); - - /* Register our commands in a list using bulk */ - bot.global_bulk_command_create({ pingcommand, pongcommand, dingcommand, dongcommand }); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} slashcommands3.cpp This fourth example goes over creating four commands but only for a guild. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() -{ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "ping") { - event.reply("Pong!"); - } else if (event.command.get_command_name() == "pong") { - event.reply("Ping!"); - } else if (event.command.get_command_name() == "ding") { - event.reply("Dong!"); - } else if (event.command.get_command_name() == "dong") { - event.reply("Ding!"); - } - }); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create some commands */ - dpp::slashcommand pingcommand("ping", "Pong!", bot.me.id); - dpp::slashcommand pongcommand("pong", "Ping!", bot.me.id); - dpp::slashcommand dingcommand("ding", "Dong!", bot.me.id); - dpp::slashcommand dongcommand("dong", "Ding!", bot.me.id); - - /* Register our commands in a list using bulk */ - bot.guild_bulk_command_create({ pingcommand, pongcommand, dingcommand, dongcommand }, 857692897221033129); - - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} slashcommands4.cpp \note For demonstration purposes, and small bots, this code is OK, but in the real world once your bot gets big, it's not recommended to create slash commands in the `on_ready` event even when it's inside dpp::run_once as, if you re-run your bot multiple times or start multiple clusters, you will quickly get rate-limited! You could, for example, add a commandline parameter to your bot (`argc`, `argv`) so that if you want the bot to register commands it must be launched with a specific command line argument. \ No newline at end of file diff --git a/docpages/example_programs/interactions_and_components/subcommands.md b/docpages/example_programs/interactions_and_components/subcommands.md index 250463332d..68755bd682 100644 --- a/docpages/example_programs/interactions_and_components/subcommands.md +++ b/docpages/example_programs/interactions_and_components/subcommands.md @@ -2,77 +2,4 @@ This demonstrates how to use sub-commands within slash commands. Also shown below is an example of how to get a "resolved" parameter without having to use the cache or an extra API call. -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} -#include -#include - -int main() { - - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - /* Executes on ready. */ - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - /* Define a slash command. */ - dpp::slashcommand image("image", "Send a specific image.", bot.me.id); - image.add_option( - /* Create a subcommand type option for "dog". */ - dpp::command_option(dpp::co_sub_command, "dog", "Send a picture of a dog."). - add_option(dpp::command_option(dpp::co_user, "user", "User to turn into a dog.", false)) - ); - image.add_option( - /* Create another subcommand type option for "cat". */ - dpp::command_option(dpp::co_sub_command, "cat", "Send a picture of a cat."). - add_option(dpp::command_option(dpp::co_user, "user", "User to turn into a cat.", false)) - ); - /* Create command */ - bot.global_command_create(image); - } - }); - - /* Use the on_slashcommand event to look for commands */ - bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) { - dpp::interaction interaction = event.command; - dpp::command_interaction cmd_data = interaction.get_command_interaction(); - /* Check if the command is the image command. */ - if (interaction.get_command_name() == "image") { - /* Get the sub command */ - auto subcommand = cmd_data.options[0]; - /* Check if the subcommand is "dog" */ - if (subcommand.name == "dog") { - /* Checks if the subcommand has any options. */ - if (!subcommand.options.empty()) { - /* Get the user from the parameter */ - dpp::user user = interaction.get_resolved_user( - subcommand.get_value(0) - ); - event.reply(user.get_mention() + " has now been turned into a dog."); - } else { - /* Reply if there were no options.. */ - event.reply("No user specified"); - } - } - /* Check if the subcommand is "cat" */ - if (subcommand.name == "cat") { - /* Checks if the subcommand has any options. */ - if (!subcommand.options.empty()) { - /* Get the user from the parameter */ - dpp::user user = interaction.get_resolved_user( - subcommand.get_value(0) - ); - event.reply(user.get_mention() + " has now been turned into a cat."); - } else { - /* Reply if there were no options.. */ - event.reply("No user specified"); - } - } - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\include{cpp} subcommands.cpp \ No newline at end of file diff --git a/docpages/example_programs/interactions_and_components/upload_parameter.md b/docpages/example_programs/interactions_and_components/upload_parameter.md index f971f20926..d73e878835 100644 --- a/docpages/example_programs/interactions_and_components/upload_parameter.md +++ b/docpages/example_programs/interactions_and_components/upload_parameter.md @@ -7,48 +7,4 @@ section, `event.command.resolved`. The file is uploaded to Discord's CDN so if you need it locally you should fetch the `.url` value, e.g. by using something like dpp::cluster::request(). -~~~~~~~~~~~~~~~~{.cpp} -#include - -int main() -{ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "show") { - - /* Get the file id from the parameter attachment. */ - dpp::snowflake file_id = std::get(event.get_parameter("file")); - - /* Get the attachment that the user inputted from the file id. */ - dpp::attachment att = event.command.get_resolved_attachment(file_id); - - /* Reply with the file as a URL. */ - event.reply(att.url); - } - }); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create a new command. */ - dpp::slashcommand newcommand("show", "Show an uploaded file", bot.me.id); - - /* Add a parameter option. */ - newcommand.add_option(dpp::command_option(dpp::co_attachment, "file", "Select an image")); - - /* Register the command */ - bot.global_command_create(newcommand); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~~~~~~~ +\include{cpp} upload_parameter.cpp diff --git a/docpages/example_programs/interactions_and_components/user-only-messages.md b/docpages/example_programs/interactions_and_components/user-only-messages.md index 36b182cfe2..7576efc0d1 100644 --- a/docpages/example_programs/interactions_and_components/user-only-messages.md +++ b/docpages/example_programs/interactions_and_components/user-only-messages.md @@ -4,40 +4,7 @@ If you've used a discord bot, there's a chance that you've encountered a message Here's how you can do exactly that! -~~~~~~~~~~{.cpp} -#include - -int main() -{ - /* Create the bot */ - dpp::cluster bot("token"); - - 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) { - - /* Check which command they ran */ - if (event.command.get_command_name() == "hello") { - - /* Reply to the user, but only let them see the response. */ - event.reply(dpp::message("Hello! How are you today?").set_flags(dpp::m_ephemeral)); - } - }); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create and Register the command */ - bot.global_command_create(dpp::slashcommand("hello", "Hello there!", bot.me.id)); - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ +\include{cpp} ephemeral.cpp That's it! If everything went well, it should look like this: