MCPcopy Create free account
hub / github.com/brainboxdotcc/DPP / main

Function main

docpages/example_code/soundboard.cpp:5–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include <sstream>
4
5int 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([&bot, 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 }

Callers

nothing calls this directly

Calls 10

cout_loggerFunction · 0.85
get_command_nameMethod · 0.80
connect_member_voiceMethod · 0.80
get_voiceMethod · 0.80
fromMethod · 0.80
startMethod · 0.80
closeMethod · 0.45
replyMethod · 0.45
is_readyMethod · 0.45

Tested by

no test coverage detected