From a9ff58724b5c0cf51b00f804ea185beaa5840da8 Mon Sep 17 00:00:00 2001 From: Imayhaveborkedit Date: Fri, 26 Jan 2024 20:14:19 -0500 Subject: [PATCH] Fix move_to related voice state bugs --- discord/gateway.py | 2 +- discord/voice_state.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/discord/gateway.py b/discord/gateway.py index dc5282813..b8936bf57 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -827,7 +827,7 @@ class DiscordVoiceWebSocket: self.loop: asyncio.AbstractEventLoop = loop self._keep_alive: Optional[VoiceKeepAliveHandler] = None self._close_code: Optional[int] = None - self.secret_key: Optional[str] = None + self.secret_key: Optional[List[int]] = None if hook: self._hook = hook diff --git a/discord/voice_state.py b/discord/voice_state.py index 6a680a106..8ea83c651 100644 --- a/discord/voice_state.py +++ b/discord/voice_state.py @@ -267,26 +267,31 @@ class VoiceConnectionState: return + channel_id = int(channel_id) self.session_id = data['session_id'] # we got the event while connecting if self.state in (ConnectionFlowState.set_guild_voice_state, ConnectionFlowState.got_voice_server_update): if self.state is ConnectionFlowState.set_guild_voice_state: self.state = ConnectionFlowState.got_voice_state_update + + # we moved ourselves + if channel_id != self.voice_client.channel.id: + self._update_voice_channel(channel_id) + else: self.state = ConnectionFlowState.got_both_voice_updates return if self.state is ConnectionFlowState.connected: - self.voice_client.channel = channel_id and self.guild.get_channel(int(channel_id)) # type: ignore + self._update_voice_channel(channel_id) elif self.state is not ConnectionFlowState.disconnected: if channel_id != self.voice_client.channel.id: # For some unfortunate reason we were moved during the connection flow _log.info('Handling channel move while connecting...') - self.voice_client.channel = channel_id and self.guild.get_channel(int(channel_id)) # type: ignore - + self._update_voice_channel(channel_id) await self.soft_disconnect(with_state=ConnectionFlowState.got_voice_state_update) await self.connect( reconnect=self.reconnect, @@ -484,6 +489,9 @@ class VoiceConnectionState: await self.disconnect() return + if self.voice_client.channel and channel.id == self.voice_client.channel.id: + return + previous_state = self.state # this is only an outgoing ws request # if it fails, nothing happens and nothing changes (besides self.state) @@ -637,3 +645,6 @@ class VoiceConnectionState: async def _move_to(self, channel: abc.Snowflake) -> None: await self.voice_client.channel.guild.change_voice_state(channel=channel) self.state = ConnectionFlowState.set_guild_voice_state + + def _update_voice_channel(self, channel_id: Optional[int]) -> None: + self.voice_client.channel = channel_id and self.guild.get_channel(channel_id) # type: ignore