| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
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() {
/* 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");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([robot, robot_size](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "join") {
/* Get the guild */
dpp::guild* g = dpp::find_guild(event.command.guild_id);
/* Attempt to connect to a voice channel, returns false if we fail to connect. */
if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) {
event.reply("You don't seem to be in a voice channel!");
return;
}
/* Tell the user we joined their channel. */
event.reply("Joined your channel!");
} else if (event.command.get_command_name() == "robot") {
/* Get the voice channel the bot is in, in this current guild. */
dpp::voiceconn* v = event.from()->get_voice(event.command.guild_id);
/* If the voice channel was invalid, or there is an issue with it, then tell the user. */
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;
}
/* Tell the bot to play the sound file 'Robot.pcm' in the current voice channel. */
v->voiceclient->send_audio_raw((uint16_t*)robot, robot_size);
event.reply("Played robot.");
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create a new command. */
dpp::slashcommand joincommand("join", "Joins your voice channel.", bot.me.id);
dpp::slashcommand robotcommand("robot", "Plays a robot noise in your voice channel.", bot.me.id);
bot.global_bulk_command_create({ joincommand, robotcommand });
}
});
/* Start bot */
bot.start(dpp::st_wait);
return 0;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::guild
Represents a guild on Discord (AKA a server).
Definition guild.h:999
dpp::guild::connect_member_voice
bool connect_member_voice(const cluster &owner, snowflake user_id, bool self_mute=false, bool self_deaf=false, bool dave=true)
Connect to a voice channel another guild member is in.
dpp::interaction::guild_id
snowflake guild_id
Optional: the guild it was sent from.
Definition appcommand.h:1051
dpp::interaction::get_issuing_user
const dpp::user & get_issuing_user() const
Get the user who issued this command.
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::managed::id
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition managed.h:39
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition appcommand.h:1449
dpp::voiceconn
Represents a connection to a voice channel. A client can only connect to one voice channel per guild ...
Definition discordclient.h:152
dpp::unicode_emoji::v
constexpr const char v[]
Definition unicode_emoji.h:1982
dpp::utility::cout_logger
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
dpp::run_once
auto run_once()
Run some code within an if() statement only once.
Definition once.h:41
dpp::find_guild
DPP_EXPORT class guild * find_guild(snowflake id)
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::event_dispatch_t::owner
dpp::cluster * owner
Cluster owning the event dispatch.
Definition dispatcher.h:102
dpp::interaction_create_t::command
interaction command
command interaction
Definition dispatcher.h:789
dpp::ready_t
Session ready.
Definition dispatcher.h:1072
dpp::slashcommand_t
User has issued a slash command.
Definition dispatcher.h:806
D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0