soundboard

# Creating a Sound Board

This example script shows how to send a sound file to a voice channel. A few shortcuts are taken here, for more advanced techniques for connecting to a voice channel see the tutorial Join or Switch to the Voice Channel of the User Issuing a Command .

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

    intmain(){
    /*LoadasoundfilecalledRobot.pcmintomemory.
    *ThebotexpectsPCMformat,whicharerawsounddata,
    *2channelstereo,16bitsigned48000Hz.
    *
    *YoucanuseaudacitytoexportthesefromWAVorMP3etc.
    *
    *Ifyouwantedtosendamorecomplicatedformat,youcould
    *useaseparatelibrarytodecodethataudiotoPCM.For
    *examplepurposes,arawPCMwillsuffice.ThisPCMfilecan
    *befoundwithinthebot'sgithubrepo.
    */
    uint8_t*robot=nullptr;
    size_trobot_size=0;
    std::ifstreaminput("../testdata/Robot.pcm",std::ios::in|std::ios::binary|std::ios::ate);
    if(input.is_open()){
    robot_size=input.tellg();
    robot=newuint8_t[robot_size];
    input.seekg(0,std::ios::beg);
    input.read((char*)robot,robot_size);
    input.close();
    }

    /*Setupthebot*/
    dpp::clusterbot("token");

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([robot,robot_size](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="join"){
    /*Gettheguild*/
    dpp::guild*g=dpp::find_guild(event.command.guild_id);

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

    /*Telltheuserwejoinedtheirchannel.*/
    event.reply("Joinedyourchannel!");
    }elseif(event.command.get_command_name()=="robot"){
    /*Getthevoicechannelthebotisin,inthiscurrentguild.*/
    dpp::voiceconn*v=event.from()->get_voice(event.command.guild_id);

    /*Ifthevoicechannelwasinvalid,orthereisanissuewithit,thentelltheuser.*/
    if(!v||!v->voiceclient||!v->voiceclient->is_ready()){
    event.reply("Therewasanissuewithgettingthevoicechannel.MakesureI'minavoicechannel!");
    return;
    }

    /*Tellthebottoplaythesoundfile'Robot.pcm'inthecurrentvoicechannel.*/
    v->voiceclient->send_audio_raw((uint16_t*)robot,robot_size);

    event.reply("Playedrobot.");
    }
    });

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

    dpp::slashcommandrobotcommand("robot","Playsarobotnoiseinyourvoicechannel.",bot.me.id);

    bot.global_bulk_command_create({joincommand,robotcommand});
    }
    });

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

    return0;
    }