| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
Slash Command Autocompletion

Discord now supports sending auto completion lists for slash command choices. To use this feature you can use code such as the example below:

#include <dpp/dpp.h>
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<struct register_bot_commands>()) {
/* 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<std::string>(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<std::string>(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", std::string("lots of squids")))
.add_autocomplete_choice(dpp::command_option_choice("cats", std::string("a few cats")))
.add_autocomplete_choice(dpp::command_option_choice("dogs", std::string("bucket of dogs")))
.add_autocomplete_choice(dpp::command_option_choice("elephants", std::string("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;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::interaction::token
std::string token
A continuation token for responding to the interaction.
Definition appcommand.h:1078
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::managed::id
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition managed.h:39
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1436
dpp::slashcommand::add_option
slashcommand & add_option(const command_option &o)
Add an option (parameter).
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::ll_debug
@ ll_debug
Debug.
Definition misc-enum.h:66
dpp::ir_autocomplete_reply
@ ir_autocomplete_reply
Reply to autocomplete interaction.
Definition appcommand.h:436
dpp::co_string
@ co_string
A string value.
Definition appcommand.h:69
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::autocomplete_t
Discord requests that we fill a list of auto completion choices for a command option.
Definition dispatcher.h:856
dpp::autocomplete_t::options
std::vector< dpp::command_option > options
auto completion options
Definition dispatcher.h:876
dpp::autocomplete_t::name
std::string name
Command name.
Definition dispatcher.h:871
dpp::command_option_choice
This struct represents choices in a multiple choice option for a command parameter....
Definition appcommand.h:131
dpp::command_option
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition appcommand.h:208
dpp::command_option::set_auto_complete
command_option & set_auto_complete(bool autocomp)
Set the auto complete state.
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::interaction_create_t::get_parameter
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
dpp::interaction_response
A response to an interaction, used to reply to a command and initiate a message, which can be hidden ...
Definition appcommand.h:468
dpp::interaction_response::add_autocomplete_choice
interaction_response & add_autocomplete_choice(const command_option_choice &achoice)
Add a command option choice.
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806
D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0