joinvc

# Join or Switch to the Voice Channel of the User Issuing a Command

When a user issues a command you may want to join their voice channel, e.g. in a music bot. If you are already on the same voice channel, the bot should do nothing (but be ready to instantly play audio) and if the user is on a different voice channel, the bot should switch to it. If the user is on no voice channel at all, this should be considered an error. This example shows how to do this.

Please be aware this example sends no audio, but indicates clearly in the comments where and how you should do so.

    #include<dpp/dpp.h>
    #include<iomanip>
    #include<sstream>

    intmain(){
    /*Setupthebot*/
    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()=="join"){

    /*Gettheguild*/
    dpp::guild*g=dpp::find_guild(event.command.guild_id);

    /*Getthevoicechannelthatthebotiscurrentlyinfromthisserver(willreturnnullptrifwe'renotinavoicechannel!)*/
    autocurrent_vc=event.from()->get_voice(event.command.guild_id);

    booljoin_vc=true;

    /*Areweinavoicechannel?Ifso,let'sseeifwe'reintherightchannel.*/
    if(current_vc){
    /*Findthechannelidthattheuseriscurrentlyin*/
    autousers_vc=g->voice_members.find(event.command.get_issuing_user().id);

    if(users_vc!=g->voice_members.end()&&current_vc->channel_id==users_vc->second.channel_id){
    join_vc=false;

    /*Weareonthisvoicechannel,atthispointwecansendanyaudioinstantlytovc:

    *current_vc->send_audio_raw(...)
    */
    }else{
    /*Weareonadifferentvoicechannel.Weshouldleaveit,thenjointhenewone
    *byfallingthroughtothejoin_vcbranchbelow.
    */
    event.from()->disconnect_voice(event.command.guild_id);

    join_vc=true;
    }
    }

    /*Ifweneedtojoinavcatall,joinithereifjoin_vc==true*/
    if(join_vc){
    /*Attempttoconnecttoavoicechannel,returnsfalseifwefailtoconnect.*/

    /*Theuserissuingthecommandisnotonanyvoicechannel,wecan'tdoanything*/
    if(!g->connect_member_voice(*event.owner,event.command.get_issuing_user().id)){
    event.reply("Youdon'tseemtobeinavoicechannel!");
    return;
    }

    /*Wearenowconnectingtoavc.Waitforon_voice_ready
    *event,andthensendtheaudiowithinthatevent:
    *
    *event.voice_client->send_audio_raw(...);
    *
    *NOTE:Wecan'tinstantlysendaudio,aswehavetowaitfor
    *theconnectiontothevoiceservertobeestablished!
    */

    /*Telltheuserwejoinedtheirchannel.*/
    event.reply("Joinedyourchannel!");
    }else{
    event.reply("Don'tneedtojoinyourchannelasi'malreadytherewithyou!");
    }
    }
    });

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

    /*Startbot*/
    bot.start(dpp::st_wait);

    return0;
    }