| 1 | #include <dpp/dpp.h> |
| 2 | #include <iomanip> |
| 3 | #include <sstream> |
| 4 | |
| 5 | int main() { |
| 6 | /* Load a sound file called Robot.pcm into memory. |
| 7 | * The bot expects PCM format, which are raw sound data, |
| 8 | * 2 channel stereo, 16 bit signed 48000Hz. |
| 9 | * |
| 10 | * You can use audacity to export these from WAV or MP3 etc. |
| 11 | * |
| 12 | * If you wanted to send a more complicated format, you could |
| 13 | * use a separate library to decode that audio to PCM. For |
| 14 | * example purposes, a raw PCM will suffice. This PCM file can |
| 15 | * be found within the bot's github repo. |
| 16 | */ |
| 17 | uint8_t* robot = nullptr; |
| 18 | size_t robot_size = 0; |
| 19 | std::ifstream input ("../testdata/Robot.pcm", std::ios::in | std::ios::binary | std::ios::ate); |
| 20 | if (input.is_open()) { |
| 21 | robot_size = input.tellg(); |
| 22 | robot = new uint8_t[robot_size]; |
| 23 | input.seekg (0, std::ios::beg); |
| 24 | input.read ((char*)robot, robot_size); |
| 25 | input.close(); |
| 26 | } |
| 27 | |
| 28 | /* Setup the bot */ |
| 29 | dpp::cluster bot("token"); |
| 30 | |
| 31 | bot.on_log(dpp::utility::cout_logger()); |
| 32 | |
| 33 | /* The event is fired when someone issues your commands */ |
| 34 | bot.on_slashcommand([robot, robot_size](const dpp::slashcommand_t& event) { |
| 35 | /* Check which command they ran */ |
| 36 | if (event.command.get_command_name() == "join") { |
| 37 | /* Get the guild */ |
| 38 | dpp::guild* g = dpp::find_guild(event.command.guild_id); |
| 39 | |
| 40 | /* Attempt to connect to a voice channel, returns false if we fail to connect. */ |
| 41 | if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) { |
| 42 | event.reply("You don't seem to be in a voice channel!"); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | /* Tell the user we joined their channel. */ |
| 47 | event.reply("Joined your channel!"); |
| 48 | } else if (event.command.get_command_name() == "robot") { |
| 49 | /* Get the voice channel the bot is in, in this current guild. */ |
| 50 | dpp::voiceconn* v = event.from()->get_voice(event.command.guild_id); |
| 51 | |
| 52 | /* If the voice channel was invalid, or there is an issue with it, then tell the user. */ |
| 53 | if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { |
| 54 | event.reply("There was an issue with getting the voice channel. Make sure I'm in a voice channel!"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | /* Tell the bot to play the sound file 'Robot.pcm' in the current voice channel. */ |
| 59 | v->voiceclient->send_audio_raw((uint16_t*)robot, robot_size); |
| 60 | |
| 61 | event.reply("Played robot."); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | bot.on_ready([&bot](const dpp::ready_t& event) { |
| 66 | if (dpp::run_once<struct register_bot_commands>()) { |
| 67 | /* Create a new command. */ |
| 68 | dpp::slashcommand joincommand("join", "Joins your voice channel.", bot.me.id); |
| 69 | |
| 70 | dpp::slashcommand robotcommand("robot", "Plays a robot noise in your voice channel.", bot.me.id); |
| 71 | |
| 72 | bot.global_bulk_command_create({ joincommand, robotcommand }); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | /* Start bot */ |
| 77 | bot.start(dpp::st_wait); |
| 78 | |
| 79 | return 0; |
| 80 | } |