D++ (DPP)
A Lightweight C++ library for Discord
Creating a Sound Board

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 <dpp/dpp.h>
#include <iomanip>
#include <sstream>
int main(int argc, char const *argv[])
{
/* Load a sound file called Robot.pcm into memory.
* The bot expects PCM format, which are raw sound data,
* 2 channel stereo, 16 bit signed 48000Hz.
*
* You can use audacity to export these from WAV or MP3 etc.
*
* If you wanted to send a more complicated format, you could
* use a separate library to decode that audio to PCM. For
* example purposes, a raw PCM will suffice. This PCM file can
* be found within the bot's github repo.
*/
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();
}
/* Setup the bot */
dpp::cluster bot("token");
/* Use the on_message_create event to look for commands */
bot.on_message_create([&bot, robot, robot_size](const dpp::message_create_t & event) {
std::stringstream ss(event.msg->content);
std::string command;
ss >> command;
/* Tell the bot to join the discord voice channel the user is on. Syntax: .join */
if (command == ".join") {
if (!g->connect_member_voice(event.msg->author->id)) {
bot.message_create(dpp::message(channel_id, "You don't seem to be on a voice channel! :("));
}
}
/* Tell the bot to play the sound file 'Robot.pcm'. Syntax: .robot */
if (command == ".robot") {
dpp::voiceconn* v = event.from->get_voice(event.msg->guild_id);
if (v && v->voiceclient && v->voiceclient->is_ready()) {
v->voiceclient->send_audio_raw((uint16_t*)robot, robot_size);
}
}
});
/* Start bot */
bot.start(false);
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:223
discord_voice_client & send_audio_raw(uint16_t *audio_data, const size_t length)
Send raw audio to the voice channel.
Definition: discordvoiceclient.cpp:782
bool is_ready()
voice client is ready to stream audio. The voice client is considered ready if it has a secret key.
Definition: discordvoiceclient.cpp:148
Represents a guild on Discord (AKA a server)
Definition: guild.h:165
bool connect_member_voice(snowflake user_id, bool self_mute=false, bool self_deaf=false)
Connect to a voice channel another guild member is in.
Definition: guild.cpp:456
snowflake id
Definition: discord.h:44
Represents a connection to a voice channel. A client can only connect to one voice channel per guild ...
Definition: discordclient.h:59
class discord_voice_client * voiceclient
voice websocket client
Definition: discordclient.h:88
guild * find_guild(snowflake id)
Definition: cache.cpp:158
Create message.
Definition: dispatcher.h:658
message * msg
Definition: dispatcher.h:664
Represents messages sent and received on Discord.
Definition: message.h:842
user * author
Definition: message.h:850
snowflake guild_id
Definition: message.h:848
std::string content
Definition: message.h:854
D++ Library version 9.0.0D++ Library version v1.0.0D++ Library version v1.0.1D++ Library version v1.0.2D++ Library version v9.0.1D++ Library version v9.0.2D++ Library version v9.0.3D++ Library version v9.0.4D++ Library version v9.0.5D++ Library version v9.0.6D++ Library version v9.0.7D++ Library version v9.0.8D++ Library version v9.0.9