diff --git a/interactions/api/events/processors/voice_events.py b/interactions/api/events/processors/voice_events.py index e68501764..ea243a414 100644 --- a/interactions/api/events/processors/voice_events.py +++ b/interactions/api/events/processors/voice_events.py @@ -13,15 +13,19 @@ class VoiceEvents(EventMixinTemplate): @Processor.define() async def _on_raw_voice_state_update(self, event: "RawGatewayEvent") -> None: - before = copy.copy(self.cache.get_voice_state(event.data["user_id"])) or None - after = await self.cache.place_voice_state_data(event.data) - - self.dispatch(events.VoiceStateUpdate(before, after)) - - if before and before.user_id == self.user.id: - if vc := self.cache.get_bot_voice_state(event.data["guild_id"]): + if str(event.data["user_id"]) == str(self.user.id): + # User is the bot itself + before = copy.copy(self.cache.get_bot_voice_state(event.data["guild_id"])) or None + after = await self.cache.place_voice_state_data(event.data, update_cache=False) + if vc := before: # noinspection PyProtectedMember await vc._voice_state_update(before, after, event.data) + else: + # User is not the bot + before = copy.copy(self.cache.get_voice_state(event.data["user_id"])) or None + after = await self.cache.place_voice_state_data(event.data) + + self.dispatch(events.VoiceStateUpdate(before, after)) if before and after: if (before.mute != after.mute) or (before.self_mute != after.self_mute): diff --git a/interactions/client/smart_cache.py b/interactions/client/smart_cache.py index 568079b80..575f69741 100644 --- a/interactions/client/smart_cache.py +++ b/interactions/client/smart_cache.py @@ -747,12 +747,15 @@ def get_voice_state(self, user_id: Optional["Snowflake_Type"]) -> Optional[Voice """ return self.voice_state_cache.get(to_optional_snowflake(user_id)) - async def place_voice_state_data(self, data: discord_typings.VoiceStateData) -> Optional[VoiceState]: + async def place_voice_state_data( + self, data: discord_typings.VoiceStateData, update_cache=True + ) -> Optional[VoiceState]: """ Take json data representing a VoiceState, process it, and cache it. Args: data: json representation of the VoiceState + update_cache: Bool for updating cache or not Returns: The processed VoiceState object @@ -768,7 +771,7 @@ async def place_voice_state_data(self, data: discord_typings.VoiceStateData) -> # check if the channel_id is None # if that is the case, the user disconnected, and we can delete them from the cache if not data["channel_id"]: - if user_id in self.voice_state_cache: + if update_cache and user_id in self.voice_state_cache: self.voice_state_cache.pop(user_id) voice_state = None @@ -780,7 +783,8 @@ async def place_voice_state_data(self, data: discord_typings.VoiceStateData) -> new_channel._voice_member_ids.append(user_id) voice_state = VoiceState.from_dict(data, self._client) - self.voice_state_cache[user_id] = voice_state + if update_cache: + self.voice_state_cache[user_id] = voice_state return voice_state