59
59
60
60
log : logging .Logger = logging .getLogger (__name__ )
61
61
62
+
62
63
class EventType :
63
64
close = 0
64
65
reconnect = 1
@@ -67,6 +68,7 @@ class EventType:
67
68
terminate = 4
68
69
clean_close = 5
69
70
71
+
70
72
class EventItem :
71
73
__slots__ = ('type' , 'shard' , 'error' )
72
74
@@ -88,6 +90,7 @@ def __eq__(self: EI, other: EI) -> bool:
88
90
def __hash__ (self ) -> int :
89
91
return hash (self .type )
90
92
93
+
91
94
class Shard :
92
95
def __init__ (self , ws : DiscordWebSocket , client : AutoShardedClient , queue_put : Callable [[EventItem ], None ]) -> None :
93
96
self .ws : DiscordWebSocket = ws
@@ -111,7 +114,7 @@ def __init__(self, ws: DiscordWebSocket, client: AutoShardedClient, queue_put: C
111
114
@property
112
115
def id (self ) -> int :
113
116
# DiscordWebSocket.shard_id is set in the from_client classmethod
114
- return self .ws .shard_id # type: ignore
117
+ return self .ws .shard_id # type: ignore
115
118
116
119
def launch (self ) -> None :
117
120
self ._task = self .loop .create_task (self .worker ())
@@ -180,8 +183,13 @@ async def reidentify(self, exc: ReconnectWebSocket) -> None:
180
183
self ._dispatch ('shard_disconnect' , self .id )
181
184
log .info ('Got a request to %s the websocket at Shard ID %s.' , exc .op , self .id )
182
185
try :
183
- coro = DiscordWebSocket .from_client (self ._client , resume = exc .resume , shard_id = self .id ,
184
- session = self .ws .session_id , sequence = self .ws .sequence )
186
+ coro = DiscordWebSocket .from_client (
187
+ self ._client ,
188
+ resume = exc .resume ,
189
+ shard_id = self .id ,
190
+ session = self .ws .session_id ,
191
+ sequence = self .ws .sequence ,
192
+ )
185
193
self .ws = await asyncio .wait_for (coro , timeout = 60.0 )
186
194
except self ._handled_exceptions as e :
187
195
await self ._handle_disconnect (e )
@@ -206,6 +214,7 @@ async def reconnect(self) -> None:
206
214
else :
207
215
self .launch ()
208
216
217
+
209
218
class ShardInfo :
210
219
"""A class that gives information and control over a specific shard.
211
220
@@ -280,6 +289,7 @@ def is_ws_ratelimited(self) -> bool:
280
289
"""
281
290
return self ._parent .ws .is_ratelimited ()
282
291
292
+
283
293
class AutoShardedClient (Client ):
284
294
"""A client similar to :class:`Client` except it handles the complications
285
295
of sharding for the user into a more manageable and transparent single
@@ -306,6 +316,7 @@ class AutoShardedClient(Client):
306
316
shard_ids: Optional[List[:class:`int`]]
307
317
An optional list of shard_ids to launch the shards with.
308
318
"""
319
+
309
320
if TYPE_CHECKING :
310
321
_connection : AutoShardedConnectionState
311
322
@@ -330,13 +341,18 @@ def __init__(self, *args: Any, loop: Optional[asyncio.AbstractEventLoop] = None,
330
341
def _get_websocket (self , guild_id : Optional [int ] = None , * , shard_id : Optional [int ] = None ) -> DiscordWebSocket :
331
342
if shard_id is None :
332
343
# guild_id won't be None if shard_id is None and shard_count won't be None here
333
- shard_id = (guild_id >> 22 ) % self .shard_count # type: ignore
344
+ shard_id = (guild_id >> 22 ) % self .shard_count # type: ignore
334
345
return self .__shards [shard_id ].ws
335
346
336
347
def _get_state (self , ** options : Any ) -> AutoShardedConnectionState :
337
- return AutoShardedConnectionState (dispatch = self .dispatch ,
338
- handlers = self ._handlers ,
339
- hooks = self ._hooks , http = self .http , loop = self .loop , ** options )
348
+ return AutoShardedConnectionState (
349
+ dispatch = self .dispatch ,
350
+ handlers = self ._handlers ,
351
+ hooks = self ._hooks ,
352
+ http = self .http ,
353
+ loop = self .loop ,
354
+ ** options ,
355
+ )
340
356
341
357
@property
342
358
def latency (self ) -> float :
@@ -370,7 +386,7 @@ def get_shard(self, shard_id: int) -> Optional[ShardInfo]:
370
386
@property
371
387
def shards (self ) -> Dict [int , ShardInfo ]:
372
388
"""Mapping[int, :class:`ShardInfo`]: Returns a mapping of shard IDs to their respective info object."""
373
- return { shard_id : ShardInfo (parent , self .shard_count ) for shard_id , parent in self .__shards .items () }
389
+ return {shard_id : ShardInfo (parent , self .shard_count ) for shard_id , parent in self .__shards .items ()}
374
390
375
391
async def launch_shard (self , gateway : str , shard_id : int , * , initial : bool = False ) -> None :
376
392
try :
@@ -449,7 +465,13 @@ async def close(self) -> None:
449
465
await self .http .close ()
450
466
self .__queue .put_nowait (EventItem (EventType .clean_close , None , None ))
451
467
452
- async def change_presence (self , * , activity : Optional [BaseActivity ] = None , status : Optional [Status ] = None , shard_id : int = None ) -> None :
468
+ async def change_presence (
469
+ self ,
470
+ * ,
471
+ activity : Optional [BaseActivity ] = None ,
472
+ status : Optional [Status ] = None ,
473
+ shard_id : int = None ,
474
+ ) -> None :
453
475
"""|coro|
454
476
455
477
Changes the client's presence.
@@ -507,7 +529,7 @@ async def change_presence(self, *, activity: Optional[BaseActivity] = None, stat
507
529
continue
508
530
509
531
# Member.activities is typehinted as Tuple[ActivityType, ...], we may be setting it as Tuple[BaseActivity, ...]
510
- me .activities = activities # type: ignore
532
+ me .activities = activities # type: ignore
511
533
me .status = status_enum
512
534
513
535
def is_ws_ratelimited (self ) -> bool :
0 commit comments