From 7b8fc3aef05272fbe7ed777de0edfc30de9a0c47 Mon Sep 17 00:00:00 2001 From: DA-344 <108473820+DA-344@users.noreply.github.com> Date: Sat, 28 Jun 2025 10:41:45 +0200 Subject: [PATCH] Handle 4021 & 4022 --- discord/voice_state.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/discord/voice_state.py b/discord/voice_state.py index 33247cd10..d2cc0ebc1 100644 --- a/discord/voice_state.py +++ b/discord/voice_state.py @@ -607,6 +607,8 @@ class VoiceConnectionState: # 1000 - normal closure (obviously) # 4014 - we were externally disconnected (voice channel deleted, we were moved, etc) # 4015 - voice server has crashed, we should resume + # 4021 - rate limited, we should not reconnect + # 4022 - call terminated, similar to 4014 if exc.code == 1000: # Don't call disconnect a second time if the websocket closed from a disconnect call if not self._expecting_disconnect: @@ -614,7 +616,7 @@ class VoiceConnectionState: await self.disconnect() break - if exc.code == 4014: + if exc.code in (4014, 4022): # We were disconnected by discord # This condition is a race between the main ws event and the voice ws closing if self._disconnected.is_set(): @@ -634,6 +636,12 @@ class VoiceConnectionState: else: continue + if exc.code == 4021: + _log.warning('We are being ratelimited while trying to connect to voice. Disconnecting...') + if self.state is not ConnectionFlowState.disconnected: + await self.disconnect() + break + if exc.code == 4015: _log.info('Disconnected from voice, attempting a resume...') try: