From 6d12dc3768b2d29465948f5ea48178c16aafaf08 Mon Sep 17 00:00:00 2001 From: SelfishPig Date: Sat, 17 Feb 2024 10:07:42 -0600 Subject: [PATCH 1/4] Added change_voice_channel to discordclient --- include/dpp/discordclient.h | 14 ++++++++++++++ src/dpp/discordclient.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/dpp/discordclient.h b/include/dpp/discordclient.h index ff2f7736f7..7d915035a0 100644 --- a/include/dpp/discordclient.h +++ b/include/dpp/discordclient.h @@ -504,6 +504,20 @@ class DPP_EXPORT discord_client : public websocket_client */ discord_client& connect_voice(snowflake guild_id, snowflake channel_id, bool self_mute = false, bool self_deaf = false); + /** + * @brief Change voice channel + * + * @param guild_id Guild where the voice channel is + * @param channel_id Channel ID of the voice channel + * @param self_mute True if the bot should mute itself + * @param self_deaf True if the bot should deafen itself + * @return reference to self + * @note This is NOT a synchronous blocking call! The bot isn't instantly ready to send or listen for audio, + * as we have to wait for the connection to the voice server to be established! + * e.g. wait for dpp::cluster::on_voice_ready event, and then send the audio within that event. + */ + discord_client& change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute = false, bool self_deaf = false); + /** * @brief Disconnect from the connected voice channel on a guild * diff --git a/src/dpp/discordclient.cpp b/src/dpp/discordclient.cpp index 0f925f6313..f0ef1401b5 100644 --- a/src/dpp/discordclient.cpp +++ b/src/dpp/discordclient.cpp @@ -634,6 +634,36 @@ discord_client& discord_client::connect_voice(snowflake guild_id, snowflake chan return *this; } +discord_client& discord_client::change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute, bool self_deaf) { +#ifdef HAVE_VOICE + std::unique_lock lock(voice_mutex); + if (connecting_voice_channels.find(guild_id) != connecting_voice_channels.end()) { + if (connecting_voice_channels[guild_id]->channel_id != channel_id) { + connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); + /* Once sent, this expects two events (in any order) on the websocket: + * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE + */ + log(ll_debug, "Sending op 4 to change VC channel, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); + queue_message(jsonobj_to_string(json({ + { "op", 4 }, + { "d", { + { "guild_id", std::to_string(guild_id) }, + { "channel_id", std::to_string(channel_id) }, + { "self_mute", self_mute }, + { "self_deaf", self_deaf }, + } + } + })), false); + } else { + log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already in this VC"); + } + } else { + log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but we aren't connected to a VC"); + } +#endif + return *this; +} + std::string discord_client::jsonobj_to_string(const nlohmann::json& json) { if (protocol == ws_json) { return json.dump(); From dc0764835e5f9a8fbbbdf153e6bb9f3ec1bf2372 Mon Sep 17 00:00:00 2001 From: SelfishPig <62257049+SelfishPig@users.noreply.github.com> Date: Sat, 17 Feb 2024 10:35:11 -0600 Subject: [PATCH 2/4] changed space-indentation to tab-indentation --- src/dpp/discordclient.cpp | 50 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/dpp/discordclient.cpp b/src/dpp/discordclient.cpp index f0ef1401b5..63c322875e 100644 --- a/src/dpp/discordclient.cpp +++ b/src/dpp/discordclient.cpp @@ -636,32 +636,32 @@ discord_client& discord_client::connect_voice(snowflake guild_id, snowflake chan discord_client& discord_client::change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute, bool self_deaf) { #ifdef HAVE_VOICE - std::unique_lock lock(voice_mutex); - if (connecting_voice_channels.find(guild_id) != connecting_voice_channels.end()) { - if (connecting_voice_channels[guild_id]->channel_id != channel_id) { - connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); - /* Once sent, this expects two events (in any order) on the websocket: - * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE - */ - log(ll_debug, "Sending op 4 to change VC channel, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); - queue_message(jsonobj_to_string(json({ - { "op", 4 }, - { "d", { - { "guild_id", std::to_string(guild_id) }, - { "channel_id", std::to_string(channel_id) }, - { "self_mute", self_mute }, - { "self_deaf", self_deaf }, - } - } - })), false); - } else { - log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already in this VC"); - } - } else { - log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but we aren't connected to a VC"); - } + std::unique_lock lock(voice_mutex); + if (connecting_voice_channels.find(guild_id) != connecting_voice_channels.end()) { + if (connecting_voice_channels[guild_id]->channel_id != channel_id) { + connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); + /* Once sent, this expects two events (in any order) on the websocket: + * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE + */ + log(ll_debug, "Sending op 4 to change VC channel, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); + queue_message(jsonobj_to_string(json({ + { "op", 4 }, + { "d", { + { "guild_id", std::to_string(guild_id) }, + { "channel_id", std::to_string(channel_id) }, + { "self_mute", self_mute }, + { "self_deaf", self_deaf }, + } + } + })), false); + } else { + log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already in this VC"); + } + } else { + log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but we aren't connected to a VC"); + } #endif - return *this; + return *this; } std::string discord_client::jsonobj_to_string(const nlohmann::json& json) { From 4b3797beda39e3df0a0d86df88574e3afa0337b2 Mon Sep 17 00:00:00 2001 From: SelfishPig Date: Sun, 18 Feb 2024 12:02:09 -0600 Subject: [PATCH 3/4] Merge change_voice_channel into connect_voice There's actually no reason for change_voice_channel to be it's own function. When would a user ever call connect_voice and not expect it to connect to the channel they specified if it's not already in there? --- include/dpp/discordclient.h | 14 -------- src/dpp/discordclient.cpp | 64 +++++++++++-------------------------- 2 files changed, 18 insertions(+), 60 deletions(-) diff --git a/include/dpp/discordclient.h b/include/dpp/discordclient.h index 7d915035a0..ff2f7736f7 100644 --- a/include/dpp/discordclient.h +++ b/include/dpp/discordclient.h @@ -504,20 +504,6 @@ class DPP_EXPORT discord_client : public websocket_client */ discord_client& connect_voice(snowflake guild_id, snowflake channel_id, bool self_mute = false, bool self_deaf = false); - /** - * @brief Change voice channel - * - * @param guild_id Guild where the voice channel is - * @param channel_id Channel ID of the voice channel - * @param self_mute True if the bot should mute itself - * @param self_deaf True if the bot should deafen itself - * @return reference to self - * @note This is NOT a synchronous blocking call! The bot isn't instantly ready to send or listen for audio, - * as we have to wait for the connection to the voice server to be established! - * e.g. wait for dpp::cluster::on_voice_ready event, and then send the audio within that event. - */ - discord_client& change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute = false, bool self_deaf = false); - /** * @brief Disconnect from the connected voice channel on a guild * diff --git a/src/dpp/discordclient.cpp b/src/dpp/discordclient.cpp index 63c322875e..c1ba3a2a9c 100644 --- a/src/dpp/discordclient.cpp +++ b/src/dpp/discordclient.cpp @@ -609,57 +609,29 @@ uint64_t discord_client::get_channel_count() { } discord_client& discord_client::connect_voice(snowflake guild_id, snowflake channel_id, bool self_mute, bool self_deaf) { -#ifdef HAVE_VOICE - std::unique_lock lock(voice_mutex); - if (connecting_voice_channels.find(guild_id) == connecting_voice_channels.end()) { - connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); - /* Once sent, this expects two events (in any order) on the websocket: - * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE - */ - log(ll_debug, "Sending op 4 to join VC, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); - queue_message(jsonobj_to_string(json({ - { "op", 4 }, - { "d", { - { "guild_id", std::to_string(guild_id) }, - { "channel_id", std::to_string(channel_id) }, - { "self_mute", self_mute }, - { "self_deaf", self_deaf }, - } - } - })), false); - } else { - log(ll_debug, "Requested the bot connect to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already on this VC"); - } -#endif - return *this; -} - -discord_client& discord_client::change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute, bool self_deaf) { #ifdef HAVE_VOICE std::unique_lock lock(voice_mutex); if (connecting_voice_channels.find(guild_id) != connecting_voice_channels.end()) { - if (connecting_voice_channels[guild_id]->channel_id != channel_id) { - connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); - /* Once sent, this expects two events (in any order) on the websocket: - * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE - */ - log(ll_debug, "Sending op 4 to change VC channel, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); - queue_message(jsonobj_to_string(json({ - { "op", 4 }, - { "d", { - { "guild_id", std::to_string(guild_id) }, - { "channel_id", std::to_string(channel_id) }, - { "self_mute", self_mute }, - { "self_deaf", self_deaf }, - } - } - })), false); - } else { - log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already in this VC"); + if (connecting_voice_channels[guild_id]->channel_id == channel_id) { + log(ll_debug, "Requested the bot connect to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already on this VC"); + return *this; } - } else { - log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but we aren't connected to a VC"); } + connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); + /* Once sent, this expects two events (in any order) on the websocket: + * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE + */ + log(ll_debug, "Sending op 4 to join VC, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); + queue_message(jsonobj_to_string(json({ + { "op", 4 }, + { "d", { + { "guild_id", std::to_string(guild_id) }, + { "channel_id", std::to_string(channel_id) }, + { "self_mute", self_mute }, + { "self_deaf", self_deaf }, + } + } + })), false); #endif return *this; } From 2a1b732cd8e05bef1e976dc2e6a9fb74e5c5e9cb Mon Sep 17 00:00:00 2001 From: SelfishPig Date: Sun, 18 Feb 2024 13:13:27 -0600 Subject: [PATCH 4/4] Remove change_voice_channel --- include/dpp/discordclient.h | 14 -------------- src/dpp/discordclient.cpp | 30 ------------------------------ 2 files changed, 44 deletions(-) diff --git a/include/dpp/discordclient.h b/include/dpp/discordclient.h index 7d915035a0..ff2f7736f7 100644 --- a/include/dpp/discordclient.h +++ b/include/dpp/discordclient.h @@ -504,20 +504,6 @@ class DPP_EXPORT discord_client : public websocket_client */ discord_client& connect_voice(snowflake guild_id, snowflake channel_id, bool self_mute = false, bool self_deaf = false); - /** - * @brief Change voice channel - * - * @param guild_id Guild where the voice channel is - * @param channel_id Channel ID of the voice channel - * @param self_mute True if the bot should mute itself - * @param self_deaf True if the bot should deafen itself - * @return reference to self - * @note This is NOT a synchronous blocking call! The bot isn't instantly ready to send or listen for audio, - * as we have to wait for the connection to the voice server to be established! - * e.g. wait for dpp::cluster::on_voice_ready event, and then send the audio within that event. - */ - discord_client& change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute = false, bool self_deaf = false); - /** * @brief Disconnect from the connected voice channel on a guild * diff --git a/src/dpp/discordclient.cpp b/src/dpp/discordclient.cpp index f555fc366d..c1ba3a2a9c 100644 --- a/src/dpp/discordclient.cpp +++ b/src/dpp/discordclient.cpp @@ -636,36 +636,6 @@ discord_client& discord_client::connect_voice(snowflake guild_id, snowflake chan return *this; } -discord_client& discord_client::change_voice_channel(snowflake guild_id, snowflake channel_id, bool self_mute, bool self_deaf) { -#ifdef HAVE_VOICE - std::unique_lock lock(voice_mutex); - if (connecting_voice_channels.find(guild_id) != connecting_voice_channels.end()) { - if (connecting_voice_channels[guild_id]->channel_id != channel_id) { - connecting_voice_channels[guild_id] = std::make_unique(this, channel_id); - /* Once sent, this expects two events (in any order) on the websocket: - * VOICE_SERVER_UPDATE and VOICE_STATUS_UPDATE - */ - log(ll_debug, "Sending op 4 to change VC channel, guild " + std::to_string(guild_id) + " channel " + std::to_string(channel_id)); - queue_message(jsonobj_to_string(json({ - { "op", 4 }, - { "d", { - { "guild_id", std::to_string(guild_id) }, - { "channel_id", std::to_string(channel_id) }, - { "self_mute", self_mute }, - { "self_deaf", self_deaf }, - } - } - })), false); - } else { - log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but it seems we are already in this VC"); - } - } else { - log(ll_debug, "Requested the bot switch to voice channel " + std::to_string(channel_id) + " on guild " + std::to_string(guild_id) + ", but we aren't connected to a VC"); - } -#endif - return *this; -} - std::string discord_client::jsonobj_to_string(const nlohmann::json& json) { if (protocol == ws_json) { return json.dump();