From 533157ece22c020dbf09d991b23d1cf4e5281e7a Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Thu, 14 Mar 2024 01:08:13 -0400 Subject: preliminary speaker checks --- src/discord/discord.cpp | 9 +++++++-- src/discord/discord.hpp | 7 ++++--- src/discord/objects.cpp | 1 + src/discord/objects.hpp | 1 + src/discord/voicestate.cpp | 5 +++++ src/discord/voicestate.hpp | 29 +++++++++++++++++++++++++++++ src/discord/voicestateflags.hpp | 19 ------------------- 7 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 src/discord/voicestate.cpp create mode 100644 src/discord/voicestate.hpp delete mode 100644 src/discord/voicestateflags.hpp (limited to 'src/discord') diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp index ab0374d..d997bc2 100644 --- a/src/discord/discord.cpp +++ b/src/discord/discord.cpp @@ -1290,13 +1290,18 @@ std::optional DiscordClient::GetSSRCOfUser(Snowflake id) const { return m_voice.GetSSRCOfUser(id); } -std::optional> DiscordClient::GetVoiceState(Snowflake user_id) const { +std::optional> DiscordClient::GetVoiceState(Snowflake user_id) const { if (const auto it = m_voice_states.find(user_id); it != m_voice_states.end()) { return it->second; } return std::nullopt; } +bool DiscordClient::IsUserSpeaker(Snowflake user_id) const { + const auto state = GetVoiceState(user_id); + return state.has_value() && state->second.IsSpeaker(); +} + DiscordVoiceClient &DiscordClient::GetVoiceClient() { return m_voice; } @@ -2962,7 +2967,7 @@ void DiscordClient::SetVoiceState(Snowflake user_id, const VoiceState &state) { if (state.IsSelfVideo) flags |= VoiceStateFlags::SelfVideo; if (state.IsSuppressed) flags |= VoiceStateFlags::Suppressed; - m_voice_states[user_id] = std::make_pair(*state.ChannelID, flags); + m_voice_states[user_id] = std::make_pair(*state.ChannelID, PackedVoiceState { flags, state.RequestToSpeakTimestamp }); m_voice_state_channel_users[*state.ChannelID].insert(user_id); m_signal_voice_state_set.emit(user_id, *state.ChannelID, flags); diff --git a/src/discord/discord.hpp b/src/discord/discord.hpp index a74b5f6..155079f 100644 --- a/src/discord/discord.hpp +++ b/src/discord/discord.hpp @@ -5,7 +5,7 @@ #include "objects.hpp" #include "store.hpp" #include "voiceclient.hpp" -#include "voicestateflags.hpp" +#include "voicestate.hpp" #include "websocket.hpp" #include #include @@ -202,7 +202,8 @@ public: [[nodiscard]] Snowflake GetVoiceChannelID() const noexcept; [[nodiscard]] std::unordered_set GetUsersInVoiceChannel(Snowflake channel_id); [[nodiscard]] std::optional GetSSRCOfUser(Snowflake id) const; - [[nodiscard]] std::optional> GetVoiceState(Snowflake user_id) const; + [[nodiscard]] std::optional> GetVoiceState(Snowflake user_id) const; + [[nodiscard]] bool IsUserSpeaker(Snowflake user_id) const; DiscordVoiceClient &GetVoiceClient(); @@ -380,7 +381,7 @@ private: Snowflake m_voice_channel_id; // todo sql i guess - std::unordered_map> m_voice_states; + std::unordered_map> m_voice_states; std::unordered_map> m_voice_state_channel_users; void SendVoiceStateUpdate(); diff --git a/src/discord/objects.cpp b/src/discord/objects.cpp index 804f10d..1c5dd39 100644 --- a/src/discord/objects.cpp +++ b/src/discord/objects.cpp @@ -714,4 +714,5 @@ void from_json(const nlohmann::json &j, VoiceState &m) { JS_D("user_id", m.UserID); JS_ON("member", m.Member); JS_D("session_id", m.SessionID); + JS_ON("request_to_speak_timestamp", m.RequestToSpeakTimestamp); } diff --git a/src/discord/objects.hpp b/src/discord/objects.hpp index 67474a3..e026311 100644 --- a/src/discord/objects.hpp +++ b/src/discord/objects.hpp @@ -921,6 +921,7 @@ struct VoiceState { std::string SessionID; bool IsSuppressed; Snowflake UserID; + std::optional RequestToSpeakTimestamp; friend void from_json(const nlohmann::json &j, VoiceState &m); }; diff --git a/src/discord/voicestate.cpp b/src/discord/voicestate.cpp new file mode 100644 index 0000000..05c050d --- /dev/null +++ b/src/discord/voicestate.cpp @@ -0,0 +1,5 @@ +#include "voicestate.hpp" + +bool PackedVoiceState::IsSpeaker() const noexcept { + return ((Flags & VoiceStateFlags::Suppressed) != VoiceStateFlags::Suppressed) && !RequestToSpeakTimestamp.has_value(); +} diff --git a/src/discord/voicestate.hpp b/src/discord/voicestate.hpp new file mode 100644 index 0000000..cc75b0c --- /dev/null +++ b/src/discord/voicestate.hpp @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include +#include "misc/bitwise.hpp" + +// this is packed into a enum cuz it makes implementing tree models easier +enum class VoiceStateFlags : uint8_t { + Clear = 0, + Deaf = 1 << 0, + Mute = 1 << 1, + SelfDeaf = 1 << 2, + SelfMute = 1 << 3, + SelfStream = 1 << 4, + SelfVideo = 1 << 5, + Suppressed = 1 << 6, +}; + +struct PackedVoiceState { + VoiceStateFlags Flags; + std::optional RequestToSpeakTimestamp; + + [[nodiscard]] bool IsSpeaker() const noexcept; +}; + +template<> +struct Bitwise { + static const bool enable = true; +}; diff --git a/src/discord/voicestateflags.hpp b/src/discord/voicestateflags.hpp deleted file mode 100644 index e725d9a..0000000 --- a/src/discord/voicestateflags.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include -#include "misc/bitwise.hpp" - -enum class VoiceStateFlags : uint8_t { - Clear = 0, - Deaf = 1 << 0, - Mute = 1 << 1, - SelfDeaf = 1 << 2, - SelfMute = 1 << 3, - SelfStream = 1 << 4, - SelfVideo = 1 << 5, - Suppressed = 1 << 6, -}; - -template<> -struct Bitwise { - static const bool enable = true; -}; -- cgit v1.2.3