MCPcopy Create free account
hub / github.com/brainboxdotcc/DPP / main

Function main

docpages/example_code/autocomplete.cpp:3–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include <dpp/dpp.h>
2
3int main()
4{
5 dpp::cluster bot("token");
6
7 bot.on_log(dpp::utility::cout_logger());
8
9 bot.on_ready([&bot](const dpp::ready_t & event) {
10 if (dpp::run_once<struct register_bot_commands>()) {
11
12 /* Create a new global command once on ready event */
13 bot.global_command_create(dpp::slashcommand("blep", "Send a random adorable animal photo", bot.me.id)
14 .add_option(
15 /* If you set the auto complete setting on a command option, it will trigger the on_autocomplete
16 * event whenever discord needs to fill information for the choices. You cannot set any choices
17 * here if you set the auto complete value to true.
18 */
19 dpp::command_option(dpp::co_string, "animal", "The type of animal").set_auto_complete(true)
20 )
21 );
22 }
23 });
24
25 /* The interaction create event is fired when someone issues your commands */
26 bot.on_slashcommand([&bot](const dpp::slashcommand_t & event) {
27
28 /* Check which command they ran */
29 if (event.command.get_command_name() == "blep") {
30 /* Fetch a parameter value from the command parameters */
31 std::string animal = std::get<std::string>(event.get_parameter("animal"));
32 /* Reply to the command. There is an overloaded version of this
33 * call that accepts a dpp::message so you can send embeds.
34 */
35 event.reply("Blep! You chose " + animal);
36 }
37 });
38
39 /* The on_autocomplete event is fired whenever discord needs information to fill in a command options's choices.
40 * You must reply with a REST event within 500ms, so make it snappy!
41 */
42 bot.on_autocomplete([&bot](const dpp::autocomplete_t & event) {
43 for (auto & opt : event.options) {
44 /* The option which has focused set to true is the one the user is typing in */
45 if (opt.focused) {
46 /* In a real world usage of this function you should return values that loosely match
47 * opt.value, which contains what the user has typed so far. The opt.value is a variant
48 * and will contain the type identical to that of the slash command parameter.
49 * Here we can safely know it is string.
50 */
51 std::string uservalue = std::get<std::string>(opt.value);
52 bot.interaction_response_create(event.command.id, event.command.token, dpp::interaction_response(dpp::ir_autocomplete_reply)
53 .add_autocomplete_choice(dpp::command_option_choice("squids", std::string("lots of squids")))
54 .add_autocomplete_choice(dpp::command_option_choice("cats", std::string("a few cats")))
55 .add_autocomplete_choice(dpp::command_option_choice("dogs", std::string("bucket of dogs")))
56 .add_autocomplete_choice(dpp::command_option_choice("elephants", std::string("bottle of elephants")))
57 );
58 bot.log(dpp::ll_debug, "Autocomplete " + opt.name + " with value '" + uservalue + "' in field " + event.name);
59 break;
60 }

Callers

nothing calls this directly

Calls 9

cout_loggerFunction · 0.85
stringFunction · 0.85
global_command_createMethod · 0.80
get_command_nameMethod · 0.80
get_parameterMethod · 0.80
startMethod · 0.80
replyMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected