| 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 <oggz/oggz.h> |
| 8 | |
| 9 | int main() { |
| 10 | /* Load an ogg opus file into memory. |
| 11 | * The bot expects opus packets to be 2 channel stereo, 48000Hz. |
| 12 | * |
| 13 | * You may use ffmpeg to encode songs to ogg opus: |
| 14 | * ffmpeg -i /path/to/song -c:a libopus -ar 48000 -ac 2 -vn -b:a 96K /path/to/opus.ogg |
| 15 | */ |
| 16 | |
| 17 | /* Setup the bot */ |
| 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 | /* Get the guild */ |
| 28 | dpp::guild* g = dpp::find_guild(event.command.guild_id); |
| 29 | |
| 30 | /* Attempt to connect to a voice channel, returns false if we fail to connect. */ |
| 31 | if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) { |
| 32 | event.reply("You don't seem to be in a voice channel!"); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | /* Tell the user we joined their channel. */ |
| 37 | event.reply("Joined your channel!"); |
| 38 | } else if (event.command.get_command_name() == "play") { |
| 39 | /* Get the voice channel the bot is in, in this current guild. */ |
| 40 | dpp::voiceconn* v = event.from()->get_voice(event.command.guild_id); |
| 41 | |
| 42 | /* If the voice channel was invalid, or there is an issue with it, then tell the user. */ |
| 43 | if (!v || !v->voiceclient || !v->voiceclient->is_ready()) { |
| 44 | event.reply("There was an issue with getting the voice channel. Make sure I'm in a voice channel!"); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | /* load the audio file with oggz */ |
| 49 | OGGZ *track_og = oggz_open("/path/to/opus.ogg", OGGZ_READ); |
| 50 | |
| 51 | /* If there was an issue reading the file, tell the user and stop */ |
| 52 | if (!track_og) { |
| 53 | fprintf(stderr, "Error opening file\n"); |
| 54 | event.reply("There was an issue opening the file!"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | /* set read callback, this callback will be called on packets with the serialno, |
| 59 | * -1 means every packet will be handled with this callback. |
| 60 | */ |
| 61 | oggz_set_read_callback( |
| 62 | track_og, -1, |
| 63 | [](OGGZ *oggz, oggz_packet *packet, long serialno, |
| 64 | void *user_data) { |
| 65 | dpp::voiceconn *voiceconn = (dpp::voiceconn *)user_data; |
| 66 | |
| 67 | /* send the audio */ |
| 68 | voiceconn->voiceclient->send_audio_opus(packet->op.packet, |
| 69 | packet->op.bytes); |
| 70 | |
| 71 | /* make sure to always return 0 here, read more on oggz documentation */ |
| 72 | return 0; |
| 73 | }, |
| 74 | /* this will be the value of void *user_data */ |
| 75 | (void *)v |
| 76 | ); |
| 77 | |
| 78 | // read loop |
| 79 | while (v && v->voiceclient && !v->voiceclient->terminating) { |
| 80 | /* you can tweak this to whatever. Here I use BUFSIZ, defined in |
| 81 | * stdio.h as 8192. |
| 82 | */ |
| 83 | static const constexpr long CHUNK_READ = BUFSIZ * 2; |
| 84 | |
| 85 | const long read_bytes = oggz_read(track_og, CHUNK_READ); |
| 86 | |
| 87 | /* break on eof */ |
| 88 | if (!read_bytes) { |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* Don't forget to free the memory */ |
| 94 | oggz_close(track_og); |
| 95 | |
| 96 | event.reply("Finished playing the audio file!"); |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | bot.on_ready([&bot](const dpp::ready_t& event) { |
| 101 | if (dpp::run_once<struct register_bot_commands>()) { |
| 102 | /* Create a new command. */ |
| 103 | dpp::slashcommand joincommand("join", "Joins your voice channel.", bot.me.id); |
| 104 | dpp::slashcommand playcommand("play", "Plays an ogg file.", bot.me.id); |
| 105 | |
| 106 | bot.global_bulk_command_create({ joincommand, playcommand }); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | /* Start bot */ |
| 111 | bot.start(dpp::st_wait); |
| 112 | |
| 113 | return 0; |
| 114 | } |