record-user

# Record Yourself in a VC

DPP supports receiving audio. This example shows how to use it to record some user in a VC.

Voice receiving by bots is not officially supported by the Discord API. We cannot guarantee that this feature will work in the future.

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

    intmain(){
    /*ExampletorecordauserinaVC
    *
    *Recordingisoutputas'./me.pcm'andyoucanplayitviathesoundboardexample
    *oruseffmpeg'ffplay-fs16le-ar48000-ac2-i./me.pcm'
    */

    /*Replacewiththeuser'sidyouwishtorecord*/
    dpp::snowflakeuser_id=407877550216314882;

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

    FILE*fd;
    fd=fopen("./me.pcm","wb");

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

    /*Theeventisfiredwhensomeoneissuesyourcommands*/
    bot.on_slashcommand([&bot,&fd](constdpp::slashcommand_t&event){
    /*Checkwhichcommandtheyran*/
    if(event.command.get_command_name()=="record"){
    /*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,nowrecording!");
    }elseif(event.command.get_command_name()=="stop"){
    event.from()->disconnect_voice(event.command.guild_id);
    fclose(fd);

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

    bot.on_voice_receive([&bot,&fd,&user_id](constdpp::voice_receive_t&event){
    if(event.user_id==user_id){
    fwrite((char*)event.audio,1,event.audio_size,fd);
    }
    });

    bot.on_ready([&bot](constdpp::ready_t&event){
    if(dpp::run_once<struct register_bot_commands>()){
    /*Createanewcommand.*/
    dpp::slashcommandrecordcommand("record","Joinsyourvoicechannelandrecordsyou.",bot.me.id);
    dpp::slashcommandstopcommand("stop","Stopsrecordingyou.",bot.me.id);

    bot.global_bulk_command_create({recordcommand,stopcommand});
    }
    });

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

    return0;
    }