Skip to content

Commit 75713d1

Browse files
committed
feat: allow bot to recover from more error states
1 parent 4244cfb commit 75713d1

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

interactions/api/gateway/websocket.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from aiohttp import WSMsgType
1111

12+
from interactions.client import const
1213
from interactions.client.errors import WebSocketClosed
1314
from interactions.client.utils.input_utils import FastJson
1415
from interactions.models.internal.cooldowns import CooldownSystem
@@ -185,15 +186,16 @@ async def receive(self, force: bool = False) -> str:
185186

186187
if resp.type == WSMsgType.CLOSE:
187188
self.logger.debug(f"Disconnecting from gateway! Reason: {resp.data}::{resp.extra}")
188-
if resp.data >= 4000:
189+
code = int(resp.data)
190+
if code not in const.RECOVERABLE_WEBSOCKET_CLOSE_CODES:
189191
# This should propagate to __aexit__() which will forcefully shut down everything
190192
# and cleanup correctly.
191-
raise WebSocketClosed(resp.data)
193+
raise WebSocketClosed(code)
192194

193195
if force:
194196
raise RuntimeError("Discord unexpectedly wants to close the WebSocket during force receive!")
195197

196-
await self.reconnect(code=resp.data, resume=resp.data != 1000)
198+
await self.reconnect(code=code, resume=code not in const.NON_RESUMABLE_WEBSOCKET_CLOSE_CODES)
197199
continue
198200

199201
if resp.type is WSMsgType.CLOSED:

interactions/client/const.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
"T",
7777
"T_co",
7878
"LIB_PATH",
79+
"RECOVERABLE_WEBSOCKET_CLOSE_CODES",
80+
"NON_RESUMABLE_WEBSOCKET_CLOSE_CODES",
7981
)
8082

8183
_ver_info = sys.version_info
@@ -212,3 +214,6 @@ class MentionPrefix(Sentinel):
212214

213215
LIB_PATH = os.sep.join(__file__.split(os.sep)[:-2])
214216
"""The path to the library folder."""
217+
218+
RECOVERABLE_WEBSOCKET_CLOSE_CODES = (4000, 4001, 4002, 4003, 4005, 4007, 4008, 4009)
219+
NON_RESUMABLE_WEBSOCKET_CLOSE_CODES = (1000, 4007)

0 commit comments

Comments
 (0)