D++ (DPP)
C++ Discord API Bot Library
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 <fmt/format.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->ConnectMemberVoice(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->GetVoice(event.msg->guild_id);
if (v && v->voiceclient && v->voiceclient->IsReady()) {
v->voiceclient->SendAudio((uint16_t*)robot, robot_size);
}
}
});
/* Start bot */
bot.start(false);
return 0;
}
bool IsReady()
voice client is ready to stream audio. The voice client is considered ready if it has a secret key.
void SendAudio(uint16_t *audio_data, const size_t length, bool use_opus=true)
Send audio to the voice channel.
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:154
Represents a guild on Discord (AKA a server)
Definition: guild.h:85
bool ConnectMemberVoice(snowflake user_id)
Connect to a voice channel another guild member is in.
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:49
class DiscordVoiceClient * voiceclient
voice websocket client
Definition: discordclient.h:78
guild * find_guild(snowflake id)
Create message.
Definition: dispatcher.h:593
message * msg
Definition: dispatcher.h:599
Represents messages sent and received on Discord.
Definition: message.h:254
user * author
Definition: message.h:262
snowflake guild_id
Definition: message.h:260
std::string content
Definition: message.h:266
D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0