mirror of
https://github.com/MelisaDev/melisa.git
synced 2024-11-11 19:07:28 +03:00
shard disconnect, reconnect
This commit is contained in:
parent
81f0c56079
commit
0499235fac
3 changed files with 27 additions and 8 deletions
|
@ -47,6 +47,7 @@ class Gateway:
|
||||||
self.client = client
|
self.client = client
|
||||||
self.shard_id = shard_id
|
self.shard_id = shard_id
|
||||||
self.latency = float('inf')
|
self.latency = float('inf')
|
||||||
|
self.connected = False
|
||||||
|
|
||||||
self.listeners = listeners
|
self.listeners = listeners
|
||||||
|
|
||||||
|
@ -87,6 +88,7 @@ class Gateway:
|
||||||
await self.hello()
|
await self.hello()
|
||||||
if self.interval is None:
|
if self.interval is None:
|
||||||
return
|
return
|
||||||
|
self.connected = True
|
||||||
await asyncio.gather(self.heartbeat(), self.receive())
|
await asyncio.gather(self.heartbeat(), self.receive())
|
||||||
|
|
||||||
async def close(self, code: int = 1000):
|
async def close(self, code: int = 1000):
|
||||||
|
@ -136,6 +138,7 @@ class Gateway:
|
||||||
async def heartbeat(self):
|
async def heartbeat(self):
|
||||||
while self.interval is not None:
|
while self.interval is not None:
|
||||||
await self.send(self.HEARTBEAT, self.sequence)
|
await self.send(self.HEARTBEAT, self.sequence)
|
||||||
|
self.connected = True
|
||||||
await asyncio.sleep(self.interval)
|
await asyncio.sleep(self.interval)
|
||||||
|
|
||||||
async def hello(self):
|
async def hello(self):
|
||||||
|
|
|
@ -143,5 +143,3 @@ class HTTPClient:
|
||||||
route,
|
route,
|
||||||
headers=headers
|
headers=headers
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from asyncio import create_task
|
from asyncio import create_task, Task, sleep
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from ...core.gateway import Gateway
|
from ...core.gateway import Gateway
|
||||||
from ..user import BotActivity
|
from ..user import BotActivity
|
||||||
|
@ -24,8 +25,11 @@ class Shard:
|
||||||
"""Id of Shard"""
|
"""Id of Shard"""
|
||||||
return self._gateway.shard_id
|
return self._gateway.shard_id
|
||||||
|
|
||||||
|
# @property
|
||||||
|
# def
|
||||||
|
|
||||||
async def launch(self, **kwargs) -> Shard:
|
async def launch(self, **kwargs) -> Shard:
|
||||||
"""Launch new shard"""
|
"""Launches new shard"""
|
||||||
self._gateway = Gateway(self._client,
|
self._gateway = Gateway(self._client,
|
||||||
self._shard_id,
|
self._shard_id,
|
||||||
self._num_shards,
|
self._num_shards,
|
||||||
|
@ -40,6 +44,11 @@ class Shard:
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
async def _try_close(self) -> None:
|
||||||
|
if self._gateway.connected:
|
||||||
|
self._gateway.connected = False
|
||||||
|
await self._gateway.close(code=1000)
|
||||||
|
|
||||||
async def update_presence(self, activity: BotActivity = None, status: str = None) -> Shard:
|
async def update_presence(self, activity: BotActivity = None, status: str = None) -> Shard:
|
||||||
"""
|
"""
|
||||||
Update Presence for the shard
|
Update Presence for the shard
|
||||||
|
@ -56,9 +65,18 @@ class Shard:
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def disconnect(self) -> Shard:
|
async def disconnect(self) -> None:
|
||||||
await self._gateway.close()
|
"""Disconnect current shard"""
|
||||||
|
await self._try_close()
|
||||||
|
|
||||||
self.disconnected = True
|
async def reconnect(self, wait_time: int = 3) -> None:
|
||||||
|
"""Reconnect current shard
|
||||||
|
|
||||||
return self
|
Parameters
|
||||||
|
----------
|
||||||
|
wait_time: :class:`int`
|
||||||
|
Reconnect after
|
||||||
|
"""
|
||||||
|
await self._try_close()
|
||||||
|
await sleep(wait_time)
|
||||||
|
await self.launch()
|
||||||
|
|
Loading…
Reference in a new issue