This example script shows how to send a sound file to a voice channel. A few shortcuts are taken here, for more advanced techniques for connecting to a voice channel see the tutorial Join or Switch to the Voice Channel of the User Issuing a Command .
#include <iomanip>
#include <sstream>
uint8_t*
robot =
nullptr;
size_t robot_size = 0;
std::ifstream input ("../testdata/Robot.pcm", std::ios::in|std::ios::binary|std::ios::ate);
if (input.is_open()) {
robot_size = input.tellg();
robot =
new uint8_t[robot_size];
input.seekg (0, std::ios::beg);
input.read ((
char*)
robot, robot_size);
input.close();
}
dpp::guild* g = dpp::find_guild(event.command.guild_id);
if (!g->connect_member_voice(event.command.get_issuing_user().id)) {
event.reply("You don't seem to be in a voice channel!");
return;
}
event.
reply(
"Joined your channel!");
dpp::voiceconn* v = event.from->get_voice(event.command.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
event.reply("There was an issue with getting the voice channel. Make sure I'm in a voice channel!");
return;
}
v->voiceclient->send_audio_raw((uint16_t*)
robot, robot_size);
event.
reply(
"Played robot.");
}
});
if (dpp::run_once<struct register_bot_commands>()) {
dpp::slashcommand robotcommand(
"robot",
"Plays a robot noise in your voice channel.", bot.me.id);
bot.global_bulk_command_create({ joincommand, robotcommand });
}
});
return 0;
}