callback-functions

# Using Callback Functions

When you create or get an object from Discord, you send the request to its API and in return you get either an error or the object you requested/created. You can pass a function to API calls as the callback function. This means that when the request completes, and you get a response from the API, your callback function executes. You must be careful with lambda captures! Good practice would be not capturing variables by reference unless you have to, since when the request completes and the function executes, the variables can already be destructed. Advanced reference can be found here. Now, let's see callback functions in action:

    #include<dpp/dpp.h>

    intmain(){
    dpp::clusterbot("TokenWasHere",dpp::i_default_intents|dpp::i_message_content);
    /*thesecondargumentisabitmaskofintents-i_message_contentisneededtogetmessages*/

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    if(event.command.get_command_name()=="msgs-get"){
    int64_tlimit=std::get<int64_t>(event.get_parameter("quantity"));

    /*getmessagesusingIDofthechannelthecommandwasissuedin*/
    bot.messages_get(event.command.channel_id,0,0,0,limit,[event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){/*catchinganerrortologit*/
    std::cout<<callback.get_error().message<<std::endl;
    return;
    }

    automessages=callback.get<dpp::message_map>();
    /*std::get<dpp::message_map>(callback.value)wouldgivethesameresult*/

    std::stringcontents;
    for(constauto&x:messages){/*hereweiteratethroughthedpp::message_mapwegotfromcallback...*/
    contents+=x.second.content+'\n';/*...wherex.firstisIDofthecurrentmessageandx.secondisthemessageitself.*/
    }

    event.reply(contents);/*wewillseeallthosemessageswegot,unitedasone!*/
    });
    }elseif(event.command.get_command_name()=="channel-create"){
    /*createatextchannel*/
    dpp::channelchannel=dpp::channel()
    .set_name("test")
    .set_guild_id(event.command.guild_id);

    bot.channel_create(channel,[&bot,event](constdpp::confirmation_callback_t&callback)->void{
    if(callback.is_error()){/*catchinganerrortologit*/
    bot.log(dpp::loglevel::ll_error,callback.get_error().message);
    return;
    }

    autochannel=callback.get<dpp::channel>();
    /*std::get<dpp::channel>(callback.value)wouldgivethesameresult*/

    /*replywiththecreatedchannelinformation*/
    dpp::messagemessage=dpp::message("Thechannel'snameis`"+channel.name+"`,IDis`"+std::to_string(channel.id)+"andtypeis`"+std::to_string(channel.get_type())+"`.");
    /*notethatchanneltypesarerepresentedasnumbers*/
    event.reply(message);
    });
    }elseif(event.command.get_command_name()=="msg-error"){
    bot.message_get(0,0,[event](constdpp::confirmation_callback_t&callback)->void{
    /*theerrorwilloccursincethereisnomessagewithID'0'thatisinachannelwithID'0'(I'mnotexplainingwhy)*/
    if(callback.is_error()){
    event.reply(callback.get_error().message);
    return;
    }

    /*wewon'tbeabletogetherebecauseofthereturn;statement*/
    automessage=callback.get<dpp::message>();
    event.reply(message);
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once <struct register_global_commands>()){
    dpp::slashcommandmsgs_get("msgs-get","Getmessages",bot.me.id);

    constexprint64_tmin_val{1};
    constexprint64_tmax_val{100};

    msgs_get.add_option(
    dpp::command_option(dpp::co_integer,"quantity","Quantityofmessagestoget.Max-100.")
    .set_min_value(min_val)
    .set_max_value(max_val)
    );

    dpp::slashcommandchannel_create("channel-create","Createachannel",bot.me.id);
    dpp::slashcommandmsg_error("msg-error","Getanerrorinsteadofmessage:)",bot.me.id);

    bot.global_bulk_command_create({msgs_get,channel_create,msg_error});
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

This is the result: