Skip to content

Commit 69d187f

Browse files
Fix bot voice state connection issues (#1533)
* Fix bot voice state connection issues * ci: correct from checks. * dispatch bot voice state update event * Fixes * Fix docstring * Prevent updating user cache for bot itself * ci: correct from checks. * Added update_cache param * ci: correct from checks. * Return type annotation * ci: correct from checks. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c270804 commit 69d187f

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

interactions/api/events/processors/voice_events.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
class VoiceEvents(EventMixinTemplate):
1414
@Processor.define()
1515
async def _on_raw_voice_state_update(self, event: "RawGatewayEvent") -> None:
16-
before = copy.copy(self.cache.get_voice_state(event.data["user_id"])) or None
17-
after = await self.cache.place_voice_state_data(event.data)
18-
19-
self.dispatch(events.VoiceStateUpdate(before, after))
20-
21-
if before and before.user_id == self.user.id:
22-
if vc := self.cache.get_bot_voice_state(event.data["guild_id"]):
16+
if str(event.data["user_id"]) == str(self.user.id):
17+
# User is the bot itself
18+
before = copy.copy(self.cache.get_bot_voice_state(event.data["guild_id"])) or None
19+
after = await self.cache.place_voice_state_data(event.data, update_cache=False)
20+
if vc := before:
2321
# noinspection PyProtectedMember
2422
await vc._voice_state_update(before, after, event.data)
23+
else:
24+
# User is not the bot
25+
before = copy.copy(self.cache.get_voice_state(event.data["user_id"])) or None
26+
after = await self.cache.place_voice_state_data(event.data)
27+
28+
self.dispatch(events.VoiceStateUpdate(before, after))
2529

2630
if before and after:
2731
if (before.mute != after.mute) or (before.self_mute != after.self_mute):

interactions/client/smart_cache.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,12 +747,15 @@ def get_voice_state(self, user_id: Optional["Snowflake_Type"]) -> Optional[Voice
747747
"""
748748
return self.voice_state_cache.get(to_optional_snowflake(user_id))
749749

750-
async def place_voice_state_data(self, data: discord_typings.VoiceStateData) -> Optional[VoiceState]:
750+
async def place_voice_state_data(
751+
self, data: discord_typings.VoiceStateData, update_cache=True
752+
) -> Optional[VoiceState]:
751753
"""
752754
Take json data representing a VoiceState, process it, and cache it.
753755
754756
Args:
755757
data: json representation of the VoiceState
758+
update_cache: Bool for updating cache or not
756759
757760
Returns:
758761
The processed VoiceState object
@@ -768,7 +771,7 @@ async def place_voice_state_data(self, data: discord_typings.VoiceStateData) ->
768771
# check if the channel_id is None
769772
# if that is the case, the user disconnected, and we can delete them from the cache
770773
if not data["channel_id"]:
771-
if user_id in self.voice_state_cache:
774+
if update_cache and user_id in self.voice_state_cache:
772775
self.voice_state_cache.pop(user_id)
773776
voice_state = None
774777

@@ -780,7 +783,8 @@ async def place_voice_state_data(self, data: discord_typings.VoiceStateData) ->
780783
new_channel._voice_member_ids.append(user_id)
781784

782785
voice_state = VoiceState.from_dict(data, self._client)
783-
self.voice_state_cache[user_id] = voice_state
786+
if update_cache:
787+
self.voice_state_cache[user_id] = voice_state
784788

785789
return voice_state
786790

0 commit comments

Comments
 (0)