From 1e136f16a5720522fb9d502f0c3b04485b684628 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 13 Sep 2023 22:44:32 +0000 Subject: [PATCH] docs: split out autocomplete --- docpages/example_code/autocomplete.cpp | 67 +++++++++++++++++ .../autocomplete.md | 71 +------------------ 2 files changed, 68 insertions(+), 70 deletions(-) create mode 100644 docpages/example_code/autocomplete.cpp diff --git a/docpages/example_code/autocomplete.cpp b/docpages/example_code/autocomplete.cpp new file mode 100644 index 0000000000..d0204abdb2 --- /dev/null +++ b/docpages/example_code/autocomplete.cpp @@ -0,0 +1,67 @@ +#include + +int main() +{ + dpp::cluster bot("token"); + + bot.on_log(dpp::utility::cout_logger()); + + bot.on_ready([&bot](const dpp::ready_t & event) { + if (dpp::run_once()) { + + /* Create a new global command once on ready event */ + bot.global_command_create(dpp::slashcommand("blep", "Send a random adorable animal photo", bot.me.id) + .add_option( + /* If you set the auto complete setting on a command option, it will trigger the on_autocomplete + * event whenever discord needs to fill information for the choices. You cannot set any choices + * here if you set the auto complete value to true. + */ + dpp::command_option(dpp::co_string, "animal", "The type of animal").set_auto_complete(true) + ) + ); + } + }); + + /* The interaction create 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("Blep! You chose " + animal); + } + }); + + /* The on_autocomplete event is fired whenever discord needs information to fill in a command options's choices. + * You must reply with a REST event within 500ms, so make it snappy! + */ + bot.on_autocomplete([&bot](const dpp::autocomplete_t & event) { + for (auto & opt : event.options) { + /* The option which has focused set to true is the one the user is typing in */ + if (opt.focused) { + /* In a real world usage of this function you should return values that loosely match + * opt.value, which contains what the user has typed so far. The opt.value is a variant + * and will contain the type identical to that of the slash command parameter. + * Here we can safely know it is string. + */ + std::string uservalue = std::get(opt.value); + bot.interaction_response_create(event.command.id, event.command.token, dpp::interaction_response(dpp::ir_autocomplete_reply) + .add_autocomplete_choice(dpp::command_option_choice("squids", "lots of squids")) + .add_autocomplete_choice(dpp::command_option_choice("cats", "a few cats")) + .add_autocomplete_choice(dpp::command_option_choice("dogs", "bucket of dogs")) + .add_autocomplete_choice(dpp::command_option_choice("elephants", "bottle of elephants")) + ); + bot.log(dpp::ll_debug, "Autocomplete " + opt.name + " with value '" + uservalue + "' in field " + event.name); + break; + } + } + }); + + bot.start(dpp::st_wait); + + return 0; +} diff --git a/docpages/example_programs/interactions_and_components/autocomplete.md b/docpages/example_programs/interactions_and_components/autocomplete.md index 87bee8b4fe..6ee9230ce6 100644 --- a/docpages/example_programs/interactions_and_components/autocomplete.md +++ b/docpages/example_programs/interactions_and_components/autocomplete.md @@ -2,73 +2,4 @@ Discord now supports sending auto completion lists for slash command choices. To use this feature you can use code such as the example below: -~~~~~~~~~~{.cpp} -#include - -int main() -{ - dpp::cluster bot("token"); - - bot.on_log(dpp::utility::cout_logger()); - - bot.on_ready([&bot](const dpp::ready_t & event) { - if (dpp::run_once()) { - - /* Create a new global command once on ready event */ - bot.global_command_create(dpp::slashcommand("blep", "Send a random adorable animal photo", bot.me.id) - .add_option( - /* If you set the auto complete setting on a command option, it will trigger the on_autocomplete - * event whenever discord needs to fill information for the choices. You cannot set any choices - * here if you set the auto complete value to true. - */ - dpp::command_option(dpp::co_string, "animal", "The type of animal").set_auto_complete(true) - ) - ); - } - }); - - /* The interaction create 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("Blep! You chose " + animal); - } - }); - - /* The on_autocomplete event is fired whenever discord needs information to fill in a command options's choices. - * You must reply with a REST event within 500ms, so make it snappy! - */ - bot.on_autocomplete([&bot](const dpp::autocomplete_t & event) { - for (auto & opt : event.options) { - /* The option which has focused set to true is the one the user is typing in */ - if (opt.focused) { - /* In a real world usage of this function you should return values that loosely match - * opt.value, which contains what the user has typed so far. The opt.value is a variant - * and will contain the type identical to that of the slash command parameter. - * Here we can safely know it is string. - */ - std::string uservalue = std::get(opt.value); - bot.interaction_response_create(event.command.id, event.command.token, dpp::interaction_response(dpp::ir_autocomplete_reply) - .add_autocomplete_choice(dpp::command_option_choice("squids", "lots of squids")) - .add_autocomplete_choice(dpp::command_option_choice("cats", "a few cats")) - .add_autocomplete_choice(dpp::command_option_choice("dogs", "bucket of dogs")) - .add_autocomplete_choice(dpp::command_option_choice("elephants", "bottle of elephants")) - ); - bot.log(dpp::ll_debug, "Autocomplete " + opt.name + " with value '" + uservalue + "' in field " + event.name); - break; - } - } - }); - - bot.start(dpp::st_wait); - - return 0; -} -~~~~~~~~~~ - +\include{cpp} autocomplete.cpp