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.
#include <dpp/dpp.h>
#include <iomanip>
#include <sstream>
int main() {
dpp::guild* g = dpp::find_guild(event.command.guild_id);
auto current_vc = event.from->get_voice(event.command.guild_id);
bool join_vc = true;
if (current_vc) {
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;
} else {
event.from->disconnect_voice(event.command.guild_id);
join_vc = true;
}
}
if (join_vc) {
event.reply("You don't seem to be in a voice channel!");
return;
}
event.
reply(
"Joined your channel!");
} else {
event.reply("Don't need to join your channel as i'm already there with you!");
}
}
});
if (dpp::run_once<struct register_bot_commands>()) {
bot.global_command_create(
dpp::slashcommand(
"join",
"Joins your voice channel.", bot.me.id));
}
});
return 0;
}
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition: cluster.h:80
const dpp::user & get_issuing_user() const
Get the user who issued this command.
std::string get_command_name() const
Get the command name for a command interaction.
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition: managed.h:39
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:1342
std::function< void(const dpp::log_t &)> DPP_EXPORT cout_logger()
Get a default logger that outputs to std::cout. e.g.
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition: cluster.h:63
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...
interaction command
command interaction
Definition: dispatcher.h:678
Session ready.
Definition: dispatcher.h:961
User has issued a slash command.
Definition: dispatcher.h:695