🎨 连接core

This commit is contained in:
Wuyi无疑 2023-03-11 22:52:12 +08:00
parent c686a3ac81
commit c3e9ae8f61
2 changed files with 8 additions and 4 deletions

View File

@ -13,14 +13,16 @@ from .models import Message, MessageReceive
get_message = on_message(priority=999) get_message = on_message(priority=999)
install_core = on_fullmatch('gs一键安装', permission=SUPERUSER, block=True) install_core = on_fullmatch('gs一键安装', permission=SUPERUSER, block=True)
connect_core = on_fullmatch('连接core', permission=SUPERUSER, block=True) connect_core = on_fullmatch(
('连接core', '链接core'), permission=SUPERUSER, block=True
)
driver = get_driver() driver = get_driver()
gsclient: Optional[GsClient] = None gsclient: Optional[GsClient] = None
@get_message.handle() @get_message.handle()
async def send_char_adv(bot: Bot, ev: Event): async def send_char_adv(bot: Bot, ev: Event):
if gsclient is None: if gsclient is None or not gsclient.is_alive:
return await connect() return await connect()
# 通用字段获取 # 通用字段获取
@ -105,8 +107,8 @@ async def send_install_msg(matcher: Matcher):
@connect_core.handle() @connect_core.handle()
async def send_start_msg(matcher: Matcher): async def send_start_msg(matcher: Matcher):
await start_client() await connect()
await matcher.send('链接成功!...') await matcher.send('链接成功!')
@driver.on_bot_connect @driver.on_bot_connect

View File

@ -34,6 +34,7 @@ class GsClient:
cls, IP: str = 'localhost', PORT: Union[str, int] = '8765' cls, IP: str = 'localhost', PORT: Union[str, int] = '8765'
): ):
self = GsClient() self = GsClient()
cls.is_alive = True
cls.ws_url = f'ws://{IP}:{PORT}/ws/{BOT_ID}' cls.ws_url = f'ws://{IP}:{PORT}/ws/{BOT_ID}'
logger.info(f'Bot_ID: {BOT_ID}连接至[gsuid-core]: {self.ws_url}...') logger.info(f'Bot_ID: {BOT_ID}连接至[gsuid-core]: {self.ws_url}...')
cls.ws = await websockets.client.connect(cls.ws_url, max_size=2**26) cls.ws = await websockets.client.connect(cls.ws_url, max_size=2**26)
@ -109,6 +110,7 @@ class GsClient:
logger.error(e) logger.error(e)
except ConnectionClosedError: except ConnectionClosedError:
logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}') logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}')
self.is_alive = False
async def _input(self, msg: MessageReceive): async def _input(self, msg: MessageReceive):
await self.msg_list.put(msg) await self.msg_list.put(msg)