D++ (DPP)
C++ Discord API Bot Library
Join or Switch to the Voice Channel of the User Issuing a Command

When a user issues a command you may want to join their voice channel, e.g. in a music bot. If you are already on the same voice channel, the bot should do nothing (but be ready to instantly play audio) and if the user is on a different voice channel, the bot should switch to it. If the user is on no voice channel at all, this should be considered an error. This example shows how to do this.

Note
Please be aware this example sends no audio, but indicates clearly in the comments where and how you should do so.
#include <dpp/dpp.h>
#include <iomanip>
#include <sstream>
int main() {
/* Setup the bot */
dpp::cluster bot("token");
/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](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);
/* Get the voice channel that the bot is currently in from this server (will return nullptr if we're not in a voice channel!) */
auto current_vc = event.from->get_voice(event.command.guild_id);
bool join_vc = true;
/* Are we in a voice channel? If so, let's see if we're in the right channel. */
if (current_vc) {
/* Find the channel id that the user is currently in */
auto users_vc = g->voice_members.find(event.command.get_issuing_user().id);
if (users_vc != g->voice_members.end() && current_vc->channel_id == users_vc->second.channel_id) {
join_vc = false;
/* We are on this voice channel, at this point we can send any audio instantly to vc:
* current_vc->send_audio_raw(...)
*/
} else {
/* We are on a different voice channel. We should leave it, then join the new one
* by falling through to the join_vc branch below.
*/
event.from->disconnect_voice(event.command.guild_id);
join_vc = true;
}
}
/* If we need to join a vc at all, join it here if join_vc == true */
if (join_vc) {
/* Attempt to connect to a voice channel, returns false if we fail to connect. */
/* The user issuing the command is not on any voice channel, we can't do anything */
if (!g->connect_member_voice(event.command.get_issuing_user().id)) {
event.reply("You don't seem to be in a voice channel!");
return;
}
/* We are now connecting to a vc. Wait for on_voice_ready
* event, and then send the audio within that event:
*
* event.voice_client->send_audio_raw(...);
*
* NOTE: We can't instantly send audio, as we have to wait for
* the connection to the voice server to be established!
*/
/* Tell the user we joined their channel. */
event.reply("Joined your channel!");
} else {
event.reply("Don't need to join your channel as i'm already there with you!");
}
}
});
bot.on_ready([&bot](const dpp::ready_t & event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create a new command. */
bot.global_command_create(dpp::slashcommand("join", "Joins your voice channel.", bot.me.id));
}
});
/* Start bot */
bot.start(dpp::st_wait);
return 0;
}
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:79
dpp::slashcommand_t
User has issued a slash command.
Definition: dispatcher.h:715
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:101
dpp::interaction::get_issuing_user
const dpp::user & get_issuing_user() const
Get the user who issued this command.
dpp::interaction_create_t::command
interaction command
command interaction
Definition: dispatcher.h:698
dpp::slashcommand
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:1358
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.
Definition: dispatcher.h:228
dpp::interaction::get_command_name
std::string get_command_name() const
Get the command name for a command interaction.
dpp::interaction_create_t::reply
void reply(command_completion_event_t callback=utility::log_error()) const
Acknowledge interaction without displaying a message to the user, for use with button and select menu...
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:99
dpp::ready_t
Session ready.
Definition: dispatcher.h:981
D++ 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