@@ -281,6 +281,7 @@ def __init__(self, token: str, presence: Presence = None):
281
281
TOKEN = token
282
282
AUTH_HEADER = {"Authorization" : f"Bot { token } " }
283
283
284
+
284
285
self .auth = {
285
286
"token" : self .TOKEN ,
286
287
"properties" : {
@@ -309,19 +310,32 @@ def __init__(self, token: str, presence: Presence = None):
309
310
"afk" : False
310
311
}
311
312
312
- asyncio .run (self .main ())
313
+ try :
314
+ asyncio .run (self .main (False ))
315
+ except websockets .exceptions .ConnectionClosedError :
316
+ while True :
317
+ try :
318
+ asyncio .run (self .main (True ))
319
+ except websockets .exceptions .ConnectionClosedError :
320
+ pass
313
321
# asyncio.get_event_loop().run_until_complete(self.hello())
314
322
# print(self.opcode(1, self.sequence))
315
323
316
- async def main (self ):
324
+ async def main (self , resume = False ):
317
325
async with websockets .connect (
318
326
'wss://gateway.discord.gg/?v=6&encoding=json' ) \
319
327
as self .websocket :
320
- await self .hello ()
321
- if self .interval is None :
322
- print ("Hello failed, exiting" )
323
- return
324
- await asyncio .gather (self .heartbeat (), self .receive ())
328
+ if resume is False :
329
+ await self .hello ()
330
+ if self .interval is None :
331
+ print ("Hello failed, exiting" )
332
+ return
333
+ await asyncio .gather (self .heartbeat (), self .receive ())
334
+ if resume is True :
335
+ await self .resume ()
336
+ print ('RESUMING------------------------------------------------------------------------------------------------------------------------------------------------' )
337
+ await asyncio .gather (self .heartbeat (), self .receive ())
338
+
325
339
# while self.interval is not None:
326
340
# pass
327
341
@@ -383,8 +397,19 @@ def opcode(self, opcode: int, payload) -> str:
383
397
"d" : payload
384
398
}
385
399
return json .dumps (data )
400
+
386
401
async def resume (self ):
387
- pass
402
+ resume_pkt = await self .create_resume_packet ()
403
+ await self .send (op .RESUME , resume_pkt )
404
+
405
+ async def create_resume_packet (self ):
406
+ resume_blk = {
407
+ "token" : self .TOKEN ,
408
+ "session_id" : self .session_id ,
409
+ "seq" : self .sequence
410
+ }
411
+ return resume_blk
412
+
388
413
389
414
390
415
def command (name : str ):
0 commit comments