| 1 | #include <dpp/dpp.h> |
| 2 | #include <iomanip> |
| 3 | #include <sstream> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <ogg/ogg.h> |
| 8 | #include <opus/opusfile.h> |
| 9 | |
| 10 | int main() { |
| 11 | /* Load an ogg opus file into memory. |
| 12 | * The bot expects opus packets to be 2 channel stereo, 48000Hz. |
| 13 | * |
| 14 | * You may use ffmpeg to encode songs to ogg opus: |
| 15 | * ffmpeg -i /path/to/song -c:a libopus -ar 48000 -ac 2 -vn -b:a 96K /path/to/opus.ogg |
| 16 | */ |
| 17 | |
| 18 | dpp::cluster bot("token"); |
| 19 | |
| 20 | bot.on_log(dpp::utility::cout_logger()); |
| 21 | |
| 22 | /* The event is fired when someone issues your commands */ |
| 23 | bot.on_slashcommand([](const dpp::slashcommand_t& event) { |
| 24 | |
| 25 | /* Check which command they ran */ |
| 26 | if (event.command.get_command_name() == "join") { |
| 27 | |
| 28 | /* Get the guild */ |
| 29 | dpp::guild* g = dpp::find_guild(event.command.guild_id); |
| 30 | |
| 31 | /* Attempt to connect to a voice channel, returns false if we fail to connect. */ |
| 32 | if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) { |
| 33 | event.reply("You don't seem to be in a voice channel!"); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | /* Tell the user we joined their channel. */ |
| 38 | event.reply("Joined your channel!"); |
| 39 | } else if (event.command.get_command_name() == "play") { |
| 40 | |
| 41 | /* Get the voice channel the bot is in, in this current guild. */ |
| 42 | dpp::voiceconn* v = event.from()->get_voice(event.command.guild_id); |
| 43 | |
| 44 | /* If the voice channel was invalid, or there is an issue with it, then tell the user. */ |
| 45 | if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { |
| 46 | event.reply("There was an issue with getting the voice channel. Make sure I'm in a voice channel!"); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | ogg_sync_state oy; |
| 51 | ogg_stream_state os; |
| 52 | ogg_page og; |
| 53 | ogg_packet op; |
| 54 | OpusHead header; |
| 55 | char *buffer; |
| 56 | |
| 57 | FILE *fd; |
| 58 | |
| 59 | fd = fopen("/path/to/opus.ogg", "rb"); |
| 60 | |
| 61 | fseek(fd, 0L, SEEK_END); |
| 62 | size_t sz = ftell(fd); |
| 63 | rewind(fd); |
| 64 | |
| 65 | ogg_sync_init(&oy); |
| 66 | |
| 67 | buffer = ogg_sync_buffer(&oy, sz); |
| 68 | fread(buffer, 1, sz, fd); |
| 69 | |
| 70 | ogg_sync_wrote(&oy, sz); |
| 71 | |
| 72 | /** |
| 73 | * We must first verify that the stream is indeed ogg opus |
| 74 | * by reading the header and parsing it |
| 75 | */ |
| 76 | if (ogg_sync_pageout(&oy, &og) != 1) { |
| 77 | fprintf(stderr,"Does not appear to be ogg stream.\n"); |
| 78 | exit(1); |
| 79 | } |
| 80 | |
| 81 | ogg_stream_init(&os, ogg_page_serialno(&og)); |
| 82 | |
| 83 | if (ogg_stream_pagein(&os,&og) < 0) { |
| 84 | fprintf(stderr,"Error reading initial page of ogg stream.\n"); |
| 85 | exit(1); |
| 86 | } |
| 87 | |
| 88 | if (ogg_stream_packetout(&os,&op) != 1) { |
| 89 | fprintf(stderr,"Error reading header packet of ogg stream.\n"); |
| 90 | exit(1); |
| 91 | } |
| 92 | |
| 93 | /* We must ensure that the ogg stream actually contains opus data */ |
| 94 | if (!(op.bytes > 8 && !memcmp("OpusHead", op.packet, 8))) { |
| 95 | fprintf(stderr,"Not an ogg opus stream.\n"); |
| 96 | exit(1); |
| 97 | } |
| 98 | |
| 99 | /* Parse the header to get stream info */ |
| 100 | int err = opus_head_parse(&header, op.packet, op.bytes); |
| 101 | if (err) { |
| 102 | fprintf(stderr,"Not a ogg opus stream\n"); |
| 103 | exit(1); |
| 104 | } |
| 105 | |
| 106 | /* Now we ensure the encoding is correct for Discord */ |
| 107 | if (header.channel_count != 2 && header.input_sample_rate != 48000) { |
| 108 | fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n"); |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | /* Now loop though all the pages and send the packets to the vc */ |
| 113 | while (ogg_sync_pageout(&oy, &og) == 1) { |
| 114 | ogg_stream_init(&os, ogg_page_serialno(&og)); |
| 115 | |
| 116 | if(ogg_stream_pagein(&os,&og)<0) { |
| 117 | fprintf(stderr,"Error reading page of Ogg bitstream data.\n"); |
| 118 | exit(1); |
| 119 | } |
| 120 | |
| 121 | while (ogg_stream_packetout(&os,&op) != 0) { |
| 122 | |
| 123 | /* Read remaining headers */ |
| 124 | if (op.bytes > 8 && !memcmp("OpusHead", op.packet, 8)) { |
| 125 | int err = opus_head_parse(&header, op.packet, op.bytes); |
| 126 | if (err) { |
| 127 | fprintf(stderr,"Not a ogg opus stream\n"); |
| 128 | exit(1); |
| 129 | } |
| 130 | |
| 131 | if (header.channel_count != 2 && header.input_sample_rate != 48000) { |
| 132 | fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n"); |
| 133 | exit(1); |
| 134 | } |
| 135 | |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | /* Skip the opus tags */ |
| 140 | if (op.bytes > 8 && !memcmp("OpusTags", op.packet, 8)) |
| 141 | continue; |
| 142 | |
| 143 | /* Send the audio */ |
| 144 | int samples = opus_packet_get_samples_per_frame(op.packet, 48000); |
| 145 | |
| 146 | v->voiceclient->send_audio_opus(op.packet, op.bytes, samples / 48); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* Cleanup */ |
| 151 | ogg_stream_clear(&os); |
| 152 | ogg_sync_clear(&oy); |
| 153 | |
| 154 | event.reply("Finished playing the audio file!"); |
| 155 | } |
| 156 | }); |
| 157 | |
| 158 | bot.on_ready([&bot](const dpp::ready_t& event) { |
| 159 | if (dpp::run_once<struct register_bot_commands>()) { |
| 160 | /* Create a new command. */ |
| 161 | dpp::slashcommand joincommand("join", "Joins your voice channel.", bot.me.id); |
| 162 | dpp::slashcommand playcommand("play", "Plays an ogg file.", bot.me.id); |
| 163 | |
| 164 | bot.global_bulk_command_create({ joincommand, playcommand }); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | /* Start bot */ |
| 169 | bot.start(dpp::st_wait); |
| 170 | |
| 171 | return 0; |
| 172 | } |