application-command-autocomplete

# 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>

    intmain()
    {
    dpp::clusterbot("token");

    bot.on_log(dpp::utility::cout_logger());

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){

    /*Createanewglobalcommandonceonreadyevent*/
    bot.global_command_create(dpp::slashcommand("blep","Sendarandomadorableanimalphoto",bot.me.id)
    .add_option(
    /*Ifyousettheautocompletesettingonacommandoption,itwilltriggertheon_autocomplete
    *eventwheneverdiscordneedstofillinformationforthechoices.Youcannotsetanychoices
    *hereifyousettheautocompletevaluetotrue.
    */
    dpp::command_option(dpp::co_string,"animal","Thetypeofanimal").set_auto_complete(true)
    )
    );
    }
    });

    /*Theinteractioncreateeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){

    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="blep"){
    /*Fetchaparametervaluefromthecommandparameters*/
    std::stringanimal=std::get<std::string>(event.get_parameter("animal"));
    /*Replytothecommand.Thereisanoverloadedversionofthis
    *callthatacceptsadpp::messagesoyoucansendembeds.
    */
    event.reply("Blep!Youchose"+animal);
    }
    });

    /*Theon_autocompleteeventisfiredwheneverdiscordneedsinformationtofillinacommandoptions'schoices.
    *YoumustreplywithaRESTeventwithin500ms,somakeitsnappy!
    */
    bot.on_autocomplete([&bot](constdpp::autocomplete_t&event){
    for(auto&opt:event.options){
    /*Theoptionwhichhasfocusedsettotrueistheonetheuseristypingin*/
    if(opt.focused){
    /*Inarealworldusageofthisfunctionyoushouldreturnvaluesthatlooselymatch
    *opt.value,whichcontainswhattheuserhastypedsofar.Theopt.valueisavariant
    *andwillcontainthetypeidenticaltothatoftheslashcommandparameter.
    *Herewecansafelyknowitisstring.
    */
    std::stringuservalue=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("lotsofsquids")))
    .add_autocomplete_choice(dpp::command_option_choice("cats",std::string("afewcats")))
    .add_autocomplete_choice(dpp::command_option_choice("dogs",std::string("bucketofdogs")))
    .add_autocomplete_choice(dpp::command_option_choice("elephants",std::string("bottleofelephants")))
    );
    bot.log(dpp::ll_debug,"Autocomplete"+opt.name+"withvalue'"+uservalue+"'infield"+event.name);
    break;
    }
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }