|
|
@ -59,6 +59,7 @@ __all__ = ( |
|
|
|
|
|
|
|
log: logging.Logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
class EventType: |
|
|
|
close = 0 |
|
|
|
reconnect = 1 |
|
|
@ -67,6 +68,7 @@ class EventType: |
|
|
|
terminate = 4 |
|
|
|
clean_close = 5 |
|
|
|
|
|
|
|
|
|
|
|
class EventItem: |
|
|
|
__slots__ = ('type', 'shard', 'error') |
|
|
|
|
|
|
@ -88,6 +90,7 @@ class EventItem: |
|
|
|
def __hash__(self) -> int: |
|
|
|
return hash(self.type) |
|
|
|
|
|
|
|
|
|
|
|
class Shard: |
|
|
|
def __init__(self, ws: DiscordWebSocket, client: AutoShardedClient, queue_put: Callable[[EventItem], None]) -> None: |
|
|
|
self.ws: DiscordWebSocket = ws |
|
|
@ -180,8 +183,13 @@ class Shard: |
|
|
|
self._dispatch('shard_disconnect', self.id) |
|
|
|
log.info('Got a request to %s the websocket at Shard ID %s.', exc.op, self.id) |
|
|
|
try: |
|
|
|
coro = DiscordWebSocket.from_client(self._client, resume=exc.resume, shard_id=self.id, |
|
|
|
session=self.ws.session_id, sequence=self.ws.sequence) |
|
|
|
coro = DiscordWebSocket.from_client( |
|
|
|
self._client, |
|
|
|
resume=exc.resume, |
|
|
|
shard_id=self.id, |
|
|
|
session=self.ws.session_id, |
|
|
|
sequence=self.ws.sequence, |
|
|
|
) |
|
|
|
self.ws = await asyncio.wait_for(coro, timeout=60.0) |
|
|
|
except self._handled_exceptions as e: |
|
|
|
await self._handle_disconnect(e) |
|
|
@ -206,6 +214,7 @@ class Shard: |
|
|
|
else: |
|
|
|
self.launch() |
|
|
|
|
|
|
|
|
|
|
|
class ShardInfo: |
|
|
|
"""A class that gives information and control over a specific shard. |
|
|
|
|
|
|
@ -280,6 +289,7 @@ class ShardInfo: |
|
|
|
""" |
|
|
|
return self._parent.ws.is_ratelimited() |
|
|
|
|
|
|
|
|
|
|
|
class AutoShardedClient(Client): |
|
|
|
"""A client similar to :class:`Client` except it handles the complications |
|
|
|
of sharding for the user into a more manageable and transparent single |
|
|
@ -306,6 +316,7 @@ class AutoShardedClient(Client): |
|
|
|
shard_ids: Optional[List[:class:`int`]] |
|
|
|
An optional list of shard_ids to launch the shards with. |
|
|
|
""" |
|
|
|
|
|
|
|
if TYPE_CHECKING: |
|
|
|
_connection: AutoShardedConnectionState |
|
|
|
|
|
|
@ -334,9 +345,14 @@ class AutoShardedClient(Client): |
|
|
|
return self.__shards[shard_id].ws |
|
|
|
|
|
|
|
def _get_state(self, **options: Any) -> AutoShardedConnectionState: |
|
|
|
return AutoShardedConnectionState(dispatch=self.dispatch, |
|
|
|
return AutoShardedConnectionState( |
|
|
|
dispatch=self.dispatch, |
|
|
|
handlers=self._handlers, |
|
|
|
hooks=self._hooks, http=self.http, loop=self.loop, **options) |
|
|
|
hooks=self._hooks, |
|
|
|
http=self.http, |
|
|
|
loop=self.loop, |
|
|
|
**options, |
|
|
|
) |
|
|
|
|
|
|
|
@property |
|
|
|
def latency(self) -> float: |
|
|
@ -449,7 +465,13 @@ class AutoShardedClient(Client): |
|
|
|
await self.http.close() |
|
|
|
self.__queue.put_nowait(EventItem(EventType.clean_close, None, None)) |
|
|
|
|
|
|
|
async def change_presence(self, *, activity: Optional[BaseActivity] = None, status: Optional[Status] = None, shard_id: int = None) -> None: |
|
|
|
async def change_presence( |
|
|
|
self, |
|
|
|
*, |
|
|
|
activity: Optional[BaseActivity] = None, |
|
|
|
status: Optional[Status] = None, |
|
|
|
shard_id: int = None, |
|
|
|
) -> None: |
|
|
|
"""|coro| |
|
|
|
|
|
|
|
Changes the client's presence. |
|
|
|