diff --git a/__init__.py b/__init__.py index e1aa7a0..e7e6f46 100644 --- a/__init__.py +++ b/__init__.py @@ -21,3 +21,8 @@ def startup(): rlib.set_ghp(pconfig.github_token) rlib.boot(str(Path.cwd()), "nonebot_plugin_sekai_update_notify", pconfig.assets_studio_path) scheduler.add_job(sync, "interval", seconds=10, id="sekai_update_sync", replace_existing=True) + + +@driver.on_shutdown +def shutdown(): + rlib.stop() diff --git a/lib.py b/lib.py index 4fcbf89..ce2cecc 100644 --- a/lib.py +++ b/lib.py @@ -39,9 +39,13 @@ class SekaiSyncLib: self.lib.ghp.argtypes = [ctypes.c_char_p] self.lib.ghp.restype = None - # boot(cwd: *const c_char, name: *const c_char, as_path: *const c_char) -> i32 + # boot(cwd: *const c_char, name: *const c_char, as_path: *const c_char) -> c_int self.lib.boot.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p] - self.lib.boot.restype = ctypes.c_int32 + self.lib.boot.restype = ctypes.c_int + + # stop() -> c_int + self.lib.stop.argtypes = [] + self.lib.stop.restype = ctypes.c_int # fetch_data() -> *mut c_char # ⚠️ 这里必须用 c_void_p,不能用 c_char_p,否则会丢失指针地址导致内存泄漏 @@ -77,6 +81,12 @@ class SekaiSyncLib: self._str_to_bytes(as_path) ) + def stop(self) -> int: + """ + 停止主循环与清理状态 + """ + return self.lib.stop() + def fetch_data(self) -> Optional[FetchData]: # 1. 获取裸指针 ptr = self.lib.fetch_data() diff --git a/sync.py b/sync.py index 15cb92c..f952468 100644 --- a/sync.py +++ b/sync.py @@ -1,9 +1,11 @@ import json from pathlib import Path -from nonebot import get_bot -from nonebot.adapters.onebot.v11 import Message, MessageSegment -from nonebot_plugin_apscheduler import scheduler +from nonebot import get_bot, logger +from nonebot.adapters.onebot.v11 import Message, MessageSegment, GroupMessageEvent +from nonebot.internal.matcher import Matcher +from nonebot.permission import SUPERUSER +from nonebot.plugin.on import on_command from .config import pconfig from .lib import rlib @@ -33,7 +35,7 @@ async def sync(): if data is None: return message = Message() - inner = [MessageSegment.text(f"活动 {data.event_id} 的卡牌更新了!\n")] + inner = [MessageSegment.text(f"活动 {data.event_id} 的卡牌更新了!")] for item in data.card_paths: p = Path(item) n = MessageSegment.image(file=item) @@ -49,5 +51,26 @@ async def sync(): for group in enabled_groups: await get_bot().send_group_msg(message=message, group_id=group) + for path in data.card_paths: + try: + Path(path).unlink() + except Exception as e: + logger.error(f"删除文件 {path} 失败: {e}") +subscribe = on_command("订阅烤leak", permission=SUPERUSER) +unsubscribe = on_command("退订烤leak", permission=SUPERUSER) + + +@subscribe.handle() +async def _(event: GroupMessageEvent, matcher: Matcher): + group_id = event.group_id + add_group(group_id) + await matcher.send(f"已订阅烤leak更新") + + +@unsubscribe.handle() +async def _(event: GroupMessageEvent, matcher: Matcher): + group_id = event.group_id + remove_group(group_id) + await matcher.send(f"已退订烤leak更新")