From 3239c9ec3f3b4ab0164b174a76f16afec1a210e4 Mon Sep 17 00:00:00 2001 From: Junyan Qin Date: Fri, 15 Nov 2024 20:03:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BD=BB=E5=BA=95=E7=A7=BB=E9=99=A4=20?= =?UTF-8?q?yirimirai?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/core/app.py | 7 ++ pkg/core/boot.py | 4 + pkg/core/taskmgr.py | 3 + pkg/platform/manager.py | 6 +- pkg/platform/sources/nakuru.py | 2 +- pkg/platform/sources/qqbotpy.py | 2 +- pkg/platform/sources/yirimirai.py | 121 ------------------------------ templates/platform.json | 8 -- templates/schema/platform.json | 37 --------- 9 files changed, 21 insertions(+), 169 deletions(-) delete mode 100644 pkg/platform/sources/yirimirai.py diff --git a/pkg/core/app.py b/pkg/core/app.py index c96808a3..2d8afeb6 100644 --- a/pkg/core/app.py +++ b/pkg/core/app.py @@ -125,3 +125,10 @@ async def never_ending(): except Exception as e: self.logger.error(f"应用运行致命异常: {e}") self.logger.debug(f"Traceback: {traceback.format_exc()}") + + async def scoped_shutdown(self, scopes: list[str]): + pass + + async def shutdown(self): + for task in self.task_mgr.tasks: + task.cancel() diff --git a/pkg/core/boot.py b/pkg/core/boot.py index dff772d9..e6a0e3eb 100644 --- a/pkg/core/boot.py +++ b/pkg/core/boot.py @@ -53,13 +53,17 @@ async def main(loop: asyncio.AbstractEventLoop): # 挂系统信号处理 import signal + ap: app.Application + def signal_handler(sig, frame): print("[Signal] 程序退出.") + # ap.shutdown() os._exit(0) signal.signal(signal.SIGINT, signal_handler) app_inst = await make_app(loop) + ap = app_inst await app_inst.run() except Exception as e: traceback.print_exc() diff --git a/pkg/core/taskmgr.py b/pkg/core/taskmgr.py index d7436b06..210b2ab6 100644 --- a/pkg/core/taskmgr.py +++ b/pkg/core/taskmgr.py @@ -154,6 +154,9 @@ def to_dict(self) -> dict: "result": self.assume_result().__str__() if self.assume_result() is not None else None, }, } + + def cancel(self): + self.task.cancel() class AsyncTaskManager: diff --git a/pkg/platform/manager.py b/pkg/platform/manager.py index 394771e5..2e60a0cd 100644 --- a/pkg/platform/manager.py +++ b/pkg/platform/manager.py @@ -37,7 +37,7 @@ def __init__(self, ap: app.Application = None): async def initialize(self): - from .sources import yirimirai, nakuru, aiocqhttp, qqbotpy + from .sources import nakuru, aiocqhttp, qqbotpy async def on_friend_message(event: platform_events.FriendMessage, adapter: msadapter.MessageSourceAdapter): @@ -195,3 +195,7 @@ async def exception_wrapper(adapter): except Exception as e: self.ap.logger.error('平台适配器运行出错: ' + str(e)) self.ap.logger.debug(f"Traceback: {traceback.format_exc()}") + + async def shutdown(self): + for adapter in self.adapters: + await adapter.kill() diff --git a/pkg/platform/sources/nakuru.py b/pkg/platform/sources/nakuru.py index 2fbe8be4..94993dc7 100644 --- a/pkg/platform/sources/nakuru.py +++ b/pkg/platform/sources/nakuru.py @@ -328,5 +328,5 @@ async def run_async(self): while True: await asyncio.sleep(1) - def kill(self) -> bool: + async def kill(self) -> bool: return False \ No newline at end of file diff --git a/pkg/platform/sources/qqbotpy.py b/pkg/platform/sources/qqbotpy.py index cbc86f44..b91377a1 100644 --- a/pkg/platform/sources/qqbotpy.py +++ b/pkg/platform/sources/qqbotpy.py @@ -591,5 +591,5 @@ async def run_async(self): self.ap.logger.info("运行 QQ 官方适配器") await self.bot.start(**self.cfg) - def kill(self) -> bool: + async def kill(self) -> bool: return False diff --git a/pkg/platform/sources/yirimirai.py b/pkg/platform/sources/yirimirai.py deleted file mode 100644 index aa0823fd..00000000 --- a/pkg/platform/sources/yirimirai.py +++ /dev/null @@ -1,121 +0,0 @@ -# import asyncio -# import typing - - -# from .. import adapter as adapter_model -# from ...core import app - - -# @adapter_model.adapter_class("yiri-mirai") -# class YiriMiraiAdapter(adapter_model.MessageSourceAdapter): -# """YiriMirai适配器""" -# bot: mirai.Mirai - -# def __init__(self, config: dict, ap: app.Application): -# """初始化YiriMirai的对象""" -# self.ap = ap -# self.config = config -# if 'adapter' not in config or \ -# config['adapter'] == 'WebSocketAdapter': -# self.bot = mirai.Mirai( -# qq=config['qq'], -# adapter=mirai.WebSocketAdapter( -# host=config['host'], -# port=config['port'], -# verify_key=config['verifyKey'] -# ) -# ) -# elif config['adapter'] == 'HTTPAdapter': -# self.bot = mirai.Mirai( -# qq=config['qq'], -# adapter=mirai.HTTPAdapter( -# host=config['host'], -# port=config['port'], -# verify_key=config['verifyKey'] -# ) -# ) -# else: -# raise Exception('Unknown adapter for YiriMirai: ' + config['adapter']) - -# async def send_message( -# self, -# target_type: str, -# target_id: str, -# message: mirai.MessageChain -# ): -# """发送消息 - -# Args: -# target_type (str): 目标类型,`person`或`group` -# target_id (str): 目标ID -# message (mirai.MessageChain): YiriMirai库的消息链 -# """ -# task = None -# if target_type == 'person': -# task = self.bot.send_friend_message(int(target_id), message) -# elif target_type == 'group': -# task = self.bot.send_group_message(int(target_id), message) -# else: -# raise Exception('Unknown target type: ' + target_type) - -# await task - -# async def reply_message( -# self, -# message_source: mirai.MessageEvent, -# message: mirai.MessageChain, -# quote_origin: bool = False -# ): -# """回复消息 - -# Args: -# message_source (mirai.MessageEvent): YiriMirai消息源事件 -# message (mirai.MessageChain): YiriMirai库的消息链 -# quote_origin (bool, optional): 是否引用原消息. Defaults to False. -# """ -# await self.bot.send(message_source, message, quote_origin) - -# async def is_muted(self, group_id: int) -> bool: -# result = await self.bot.member_info(target=group_id, member_id=self.bot.qq).get() -# if result.mute_time_remaining > 0: -# return True -# return False - -# def register_listener( -# self, -# event_type: typing.Type[mirai.Event], -# callback: typing.Callable[[mirai.Event, adapter_model.MessageSourceAdapter], None] -# ): -# """注册事件监听器 - -# Args: -# event_type (typing.Type[mirai.Event]): YiriMirai事件类型 -# callback (typing.Callable[[mirai.Event], None]): 回调函数,接收一个参数,为YiriMirai事件 -# """ -# async def wrapper(event: mirai.Event): -# await callback(event, self) -# self.bot.on(event_type)(wrapper) - -# def unregister_listener( -# self, -# event_type: typing.Type[mirai.Event], -# callback: typing.Callable[[mirai.Event, adapter_model.MessageSourceAdapter], None] -# ): -# """注销事件监听器 - -# Args: -# event_type (typing.Type[mirai.Event]): YiriMirai事件类型 -# callback (typing.Callable[[mirai.Event], None]): 回调函数,接收一个参数,为YiriMirai事件 -# """ -# assert isinstance(self.bot, mirai.Mirai) -# bus = self.bot.bus -# assert isinstance(bus, mirai.models.bus.ModelEventBus) - -# bus.unsubscribe(event_type, callback) - -# async def run_async(self): -# self.bot_account_id = self.bot.qq -# return await MiraiRunner(self.bot)._run() - -# async def kill(self) -> bool: -# return False diff --git a/templates/platform.json b/templates/platform.json index f0a13fd6..74adf833 100644 --- a/templates/platform.json +++ b/templates/platform.json @@ -1,13 +1,5 @@ { "platform-adapters": [ - { - "adapter": "yiri-mirai", - "enable": false, - "host": "127.0.0.1", - "port": 8080, - "verifyKey": "yirimirai", - "qq": 123456789 - }, { "adapter": "nakuru", "enable": false, diff --git a/templates/schema/platform.json b/templates/schema/platform.json index 4c2bab31..cb42b798 100644 --- a/templates/schema/platform.json +++ b/templates/schema/platform.json @@ -9,43 +9,6 @@ "items": { "type": "object", "oneOf": [ - { - "title": "YiriMirai 适配器", - "description": "用于接入 Mirai", - "properties": { - "adapter": { - "type": "string", - "const": "yiri-mirai" - }, - "enable": { - "type": "boolean", - "default": false, - "description": "是否启用此适配器", - "layout": { - "comp": "switch", - "props": { - "color": "primary" - } - } - }, - "host": { - "type": "string", - "default": "127.0.0.1" - }, - "port": { - "type": "integer", - "default": 8080 - }, - "verifyKey": { - "type": "string", - "default": "yirimirai" - }, - "qq": { - "type": "integer", - "default": 123456789 - } - } - }, { "title": "Nakuru 适配器", "description": "用于接入 go-cqhttp",