making_threads

# Creating and Interacting with Threads

A new feature added to Discord recently is `Threads`, these allow you to break off a message into a different "channel", without creating a whole new channel. There are also other types of "thread channels", one example being a `forums channel`. This type of channel only contains threads, meaning you can't send messages in it so if you want to make one of them, be careful about trying to send a message in it!

In this tutorial, we'll be going through:

- How to create a thread.

- How to loop through all the active threads in a server (and sending a message in one).

- How to lock a thread (editing threads).

First, let's go through creating a thread.

    #include<dpp/dpp.h>

    intmain(){
    /*Createthebot*/
    dpp::clusterbot("token");

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

    /*Theeventisfiredwhenthebotdetectsamessageinanyserverandanychannelithasaccessto.*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="create-thread"){
    /*Herewecreateathreadinthecurrentchannel.Itwillexpireafter60minutesofinactivity.We'llalsoallowothermodstojoin,andwewon'taddaslowdowntimer.*/
    bot.thread_create("Coolthread!",event.command.channel_id,60,dpp::channel_type::CHANNEL_PUBLIC_THREAD,true,0,[event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){
    event.reply("Failedtocreateathread!");
    return;
    }

    event.reply("Createdathreadforyou!");
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisterthecommand*/
    bot.global_command_create(dpp::slashcommand("create-thread","Createathread!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

If all went well, you'll see that the bot has successfully created a thread!

Now, let's cover looping through all the threads in a server. For this demonstration, we'll be picking the first thread we find in the list and sending a message in it.

    #include<dpp/dpp.h>

    intmain(){
    /*Createthebot*/
    dpp::clusterbot("token");

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

    /*Theeventisfiredwhenthebotdetectsamessageinanyserverandanychannelithasaccessto.*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="message-thread"){
    /*Getallactivethreadsinaguild.*/
    bot.threads_get_active(event.command.guild_id,[&bot,event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){
    event.reply("Failedtogetthreads!");
    return;
    }

    /*Getthelistofactivethreadsintheguild.*/
    autothreads=callback.get<dpp::active_threads>();

    dpp::snowflakethread_id;

    /*Loopthroughthethreads,gettingeachvalueinthemap.Thenwegetthefirstvalueandthenbreakoff.
    *Thereasonwe'regettingonlythefirstvalueisbecause,forthisexample,we'lljustassumeyou'veonlygotasingleactivethread(theonecreatedbythebot)
    */
    for(constauto&_thread:threads){
    thread_id=_thread.first;
    break;
    }

    /*Sendamessageinthefirstthreadwefind.*/
    bot.message_create(dpp::message(thread_id,"Hey,I'mfirsttomessageinacoolthread!"),[event](constdpp::confirmation_callback_t&callback2){
    if(callback2.is_error()){
    event.reply("Failedtosendamessageinathread.");
    return;
    }

    event.reply("I'vesentamessageinthespecifiedthread.");
    });
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisterthecommand*/
    bot.global_command_create(dpp::slashcommand("message-thread","Messageathread!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

After that, you'll be able to see your bot send a message in your thread!

Those of you who are familar with sending messages in regular channels may have also noticed that sending messages to threads is the same as sending a general message. This is because threads are basically channels with a couple more features!

Now, we're going to cover how to lock a thread! With this, you'll also learn how to edit threads in general, meaning you can go forward and learn how to change even more stuff about threads, as much as your heart desires!

    #include<dpp/dpp.h>

    intmain(){
    /*Createthebot*/
    dpp::clusterbot("token");

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

    /*Theeventisfiredwhenthebotdetectsamessageinanyserverandanychannelithasaccessto.*/
    bot.on_slashcommand([&bot](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="lock-thread"){
    /*Getthischannelasathread.*/
    bot.thread_get(event.command.channel_id,[&bot,event](constdpp::confirmation_callback_t&callback){
    if(callback.is_error()){
    event.reply("Ifailedtogetthethread!");
    return;
    }

    /*Getthethreadfromthecallback.*/
    autothread=callback.get<dpp::thread>();

    /*Setthethreadtolocked.*/
    thread.metadata.locked=true;

    /*Nowwetelldiscordaboutourupdates,meaningthethreadwilllock!*/
    bot.thread_edit(thread,[event](constdpp::confirmation_callback_t&callback2){
    if(callback2.is_error()){
    event.reply("Ifailedtolockthethread!");
    return;
    }

    event.reply("Ihavelockedthethread!");
    });
    });
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createandregisterthecommand*/
    bot.global_command_create(dpp::slashcommand("lock-thread","Lockthethreadthatyourunthiscommandin!",bot.me.id));
    }
    });

    bot.start(dpp::st_wait);

    return0;
    }

Once you've ran that, you'll see that you were successfully able to lock a thread!