Debian Codegraph @

docpages/example_code/join_voice.cpp

raw symbols SCIP
1 #include <dpp/dpp.h>
2 #include <iomanip>
3 #include <sstream>
4
5 int main() {
6 /* Setup the bot */
7 dpp::cluster bot("token");
8
9 bot.on_log(dpp::utility::cout_logger());
10
11 /* The event is fired when someone issues your commands */
12 bot.on_slashcommand([](const dpp::slashcommand_t& event) {
13
14 /* Check which command they ran */
15 if (event.command.get_command_name() == "join") {
16
17 /* Get the guild */
18 dpp::guild* g = dpp::find_guild(event.command.guild_id);
19
20 /* Get the voice channel that the bot is currently in from this server (will return nullptr if we're not in a voice channel!) */
21 auto current_vc = event.from()->get_voice(event.command.guild_id);
22
23 bool join_vc = true;
24
25 /* Are we in a voice channel? If so, let's see if we're in the right channel. */
26 if (current_vc) {
27 /* Find the channel id that the user is currently in */
28 auto users_vc = g->voice_members.find(event.command.get_issuing_user().id);
29
30 if (users_vc != g->voice_members.end() && current_vc->channel_id == users_vc->second.channel_id) {
31 join_vc = false;
32
33 /* We are on this voice channel, at this point we can send any audio instantly to vc:
34
35 * current_vc->send_audio_raw(...)
36 */
37 } else {
38 /* We are on a different voice channel. We should leave it, then join the new one
39 * by falling through to the join_vc branch below.
40 */
41 event.from()->disconnect_voice(event.command.guild_id);
42
43 join_vc = true;
44 }
45 }
46
47 /* If we need to join a vc at all, join it here if join_vc == true */
48 if (join_vc) {
49 /* Attempt to connect to a voice channel, returns false if we fail to connect. */
50
51 /* The user issuing the command is not on any voice channel, we can't do anything */
52 if (!g->connect_member_voice(*event.owner, event.command.get_issuing_user().id)) {
53 event.reply("You don't seem to be in a voice channel!");
54 return;
55 }
56
57 /* We are now connecting to a vc. Wait for on_voice_ready
58 * event, and then send the audio within that event:
59 *
60 * event.voice_client->send_audio_raw(...);
61 *
62 * NOTE: We can't instantly send audio, as we have to wait for
63 * the connection to the voice server to be established!
64 */
65
66 /* Tell the user we joined their channel. */
67 event.reply("Joined your channel!");
68 } else {
69 event.reply("Don't need to join your channel as i'm already there with you!");
70 }
71 }
72 });
73
74 bot.on_ready([&bot](const dpp::ready_t& event) {
75 if (dpp::run_once<struct register_bot_commands>()) {
76 /* Create a new command. */
77 bot.global_command_create(dpp::slashcommand("join", "Joins your voice channel.", bot.me.id));
78 }
79 });
80
81 /* Start bot */
82 bot.start(dpp::st_wait);
83
84 return 0;
85 }