components3

# Using Select Menu Components

This tutorial will cover creating two types of select menus:

- A generic select menu with just text

- An auto-populated role select menu.

This first example demonstrates creating a select menu, receiving select menu clicks, and sending a response message.

    #include<dpp/dpp.h>
    #include<dpp/unicode_emoji.h>

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

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="select"){
    /*Createamessage*/
    dpp::messagemsg(event.command.channel_id,"Thistexthasaselectmenu!");

    /*Addanactionrow,andaselectmenuwithintheactionrow.*/
    msg.add_component(
    dpp::component().add_component(
    dpp::component()
    .set_type(dpp::cot_selectmenu)
    .set_placeholder("Picksomething")
    .add_select_option(dpp::select_option("label1","value1","description1").set_emoji(dpp::unicode_emoji::smile))
    .add_select_option(dpp::select_option("label2","value2","description2").set_emoji(dpp::unicode_emoji::slight_smile))
    .set_id("myselectid")
    )
    );

    /*Replytotheuserwithourmessage.*/
    event.reply(msg);
    }
    });

    /*Whenauserclicksyourselectmenu,theon_select_clickeventwillfire,
    *containingthecustom_idyoudefinedinyourselectmenu.
    */
    bot.on_select_click([&bot](constdpp::select_click_t&event){
    /*Selectclicksarestillinteractions,andmustberepliedtoinsomeformto
    *preventthe"thisinteractionhasfailed"messagefromDiscordtotheuser.
    */
    event.reply("Youclicked"+event.custom_id+"andchose:"+event.values[0]);
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisteracommandwhenthebotisready*/
    bot.global_command_create(dpp::slashcommand("select","Selectsomethingatrandom!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

This second example demonstrates:

- Creating a role select menu that is auto-populated by Discord

- Allowing users to select two options!

This type of select menu, along with other types (these types being: user, role, mentionable, and channel), always auto-fill. You never need to define the data in these types of select menus. All select menu types allow you to select multiple options.

    #include<dpp/dpp.h>

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

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="select"){
    /*Createamessage*/
    dpp::messagemsg(event.command.channel_id,"Thistexthasaselectmenu!");

    /*Addanactionrow,andaselectmenuwithintheactionrow.
    *
    *Bydefault,maxvaluesis1,meaningpeoplecanonlypick1option.
    *We'rechangingthistotwo,sopeoplecanselectmultipleoptions!
    *We'llalsosetthemin_valuesto2sopeoplehavetopickanothervalue!
    */
    msg.add_component(
    dpp::component().add_component(
    dpp::component()
    .set_type(dpp::cot_role_selectmenu)
    .set_min_values(2)
    .set_max_values(2)
    .set_id("myselectid")
    )
    );

    /*Replytotheuserwithourmessage.*/
    event.reply(msg);
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisteracommandwhenthebotisready*/
    bot.global_command_create(dpp::slashcommand("select","Selectsomethingatrandom!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }