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

Search
Loading...
Searching...
No Matches
Record Yourself in a VC

DPP supports receiving audio. This example shows how to use it to record some user in a VC.

Note
  Voice receiving by bots is not officially supported by the Discord API. We cannot guarantee that this feature will work in the future.

#include <dpp/dpp.h>
#include <iomanip>
#include <sstream>
int main() {
/* Example to record a user in a VC
*
* Recording is output as './me.pcm' and you can play it via the soundboard example
* or use ffmpeg 'ffplay -f s16le -ar 48000 -ac 2 -i ./me.pcm'
*/
/* Replace with the user's id you wish to record */
dpp::snowflake user_id = 407877550216314882;
/* Setup the bot */
dpp::cluster bot("token");
FILE *fd;
fd = fopen("./me.pcm", "wb");
bot.on_log(dpp::utility::cout_logger());
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&fd](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "record") {
/* 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, now recording!");
} else if (event.command.get_command_name() == "stop") {
event.from()->disconnect_voice(event.command.guild_id);
fclose(fd);
event.reply("Stopped recording.");
}
});
bot.on_voice_receive([&fd, &user_id](const dpp::voice_receive_t& event) {
if (event.user_id == user_id) {
fwrite((char *)event.audio, 1, event.audio_size, fd);
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create a new command. */
dpp::slashcommand recordcommand("record", "Joins your voice channel and records you.", bot.me.id);
dpp::slashcommand stopcommand("stop", "Stops recording you.", bot.me.id);
bot.global_bulk_command_create({ recordcommand, stopcommand });
}
});
/* 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::snowflake
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition snowflake.h:54
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
dpp::voice_receive_t
voice receive packet
Definition dispatcher.h:2159
dpp::voice_receive_t::audio_size
size_t audio_size
Size of audio buffer.
Definition dispatcher.h:2206
dpp::voice_receive_t::audio
uint8_t * audio
Audio data, encoded as 48kHz stereo PCM or Opus,.
Definition dispatcher.h:2200
dpp::voice_receive_t::user_id
snowflake user_id
User ID of speaker (zero if unknown).
Definition dispatcher.h:2216
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