diff --git a/GenshinUID/__init__.py b/GenshinUID/__init__.py index 9f2f455e..3f9b5bf3 100644 --- a/GenshinUID/__init__.py +++ b/GenshinUID/__init__.py @@ -1,6 +1,7 @@ from typing import List, Literal, Optional from hoshino import priv +from websockets.exceptions import ConnectionClosed from hoshino.typing import CQEvent, HoshinoBot, NoticeSession from .client import GsClient @@ -15,12 +16,18 @@ async def connect(): global gsclient try: gsclient = await GsClient().async_connect() + await gsclient.start() except ConnectionRefusedError: logger.error('Core服务器连接失败...请稍后使用[启动core]命令启动...') async def get_gs_msg(ev): - if gsclient is None or not gsclient.is_alive: + if gsclient is None: + return await connect() + + try: + await gsclient.ws.ping() + except ConnectionClosed: await connect() # 通用字段获取 diff --git a/GenshinUID/client.py b/GenshinUID/client.py index 0094b834..544ca33a 100644 --- a/GenshinUID/client.py +++ b/GenshinUID/client.py @@ -33,7 +33,6 @@ class GsClient: logger.info(f'与[gsuid-core]成功连接! Bot_ID: {BOT_ID}') cls.msg_list = asyncio.queues.Queue() cls.pending = [] - await self.start() return self def __new__(cls, *args, **kwargs): @@ -118,6 +117,7 @@ class GsClient: await asyncio.sleep(5) try: await self.async_connect() + await self.start() break except: # noqa logger.debug('自动连接core服务器失败...五秒后重新连接...')