Skip to content

Fix bot voice state connection issues #1533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions interactions/api/events/processors/voice_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions interactions/client/smart_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down