D++ (DPP)
C++ Discord API Bot Library
Slash command auto completion

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_ready([&bot](const dpp::ready_t & event) {
/* Create a new global command on ready event */
bot.global_command_create(dpp::slashcommand().set_name("blep")
.set_description("Send a random adorable animal photo")
.set_application_id(bot.me.id)
/* If you set the auto complete setting on a command option, it will trigger the on_auticomplete
* 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_interaction_create([&bot](const dpp::interaction_create_t & event) {
dpp::command_interaction cmd_data = std::get<dpp::command_interaction>(event.command.data);
/* Check which command they ran */
if (cmd_data.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(dpp::ir_channel_message_with_source, "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", "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;
}
}
});
/* Simple log event */
bot.on_log([&bot](const dpp::log_t & event) {
std::cout << dpp::utility::loglevel(event.severity) << ": " << event.message << "\n";
});
bot.start(false);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:233
uint8_t type
the type of interaction
Definition: slashcommand.h:426
Represents an application command, created by your bot either globally, or on a guild.
Definition: slashcommand.h:525
slashcommand & add_option(const command_option &o)
Add an option (parameter)
std::string DPP_EXPORT loglevel(dpp::loglevel in)
Convert a dpp::loglevel enum value to a string.
@ it_application_command
application command (slash command)
Definition: slashcommand.h:353
@ co_string
Definition: slashcommand.h:48
Discord requests that we fill a list of auto completion choices for a command option.
Definition: dispatcher.h:289
std::vector< dpp::command_option > options
auto completion options
Definition: dispatcher.h:318
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: slashcommand.h:122
command_option & set_auto_complete(bool autocomp)
Set the auto complete state.
Create interaction.
Definition: dispatcher.h:173
interaction command
command interaction
Definition: dispatcher.h:255
Log messages.
Definition: dispatcher.h:104
loglevel severity
Definition: dispatcher.h:112
Session ready.
Definition: dispatcher.h:432
D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0