From 08cdbd9b95266628100066961ea78b97909740fd Mon Sep 17 00:00:00 2001 From: Hanserprpr <2041283903@qq.com> Date: Thu, 24 Jul 2025 17:54:37 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BF=9B=E7=BE=A4?= =?UTF-8?q?=E5=90=8E=E6=9C=AA=E8=83=BD=E6=88=90=E5=8A=9F=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E9=85=8D=E7=BD=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_eventmonitor/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nonebot_plugin_eventmonitor/config.py b/nonebot_plugin_eventmonitor/config.py index 934b358..9391e3b 100644 --- a/nonebot_plugin_eventmonitor/config.py +++ b/nonebot_plugin_eventmonitor/config.py @@ -226,6 +226,7 @@ async def json_upload(self, updated_config: dict) -> None: """将 JSON 数据上传到指定路径""" with self.data_address.open('w', encoding='utf-8') as f: json.dump(updated_config, f, ensure_ascii=False, indent=4) + self.g_temp = updated_config @staticmethod async def poke(event: Event) -> bool: From fd1a6d2bc609b7c35b5883ca8d3c8499e74cb317 Mon Sep 17 00:00:00 2001 From: Reversedeer Date: Fri, 25 Jul 2025 18:23:34 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=E7=BE=A4?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=80=E9=94=AE=E5=BC=80=E5=85=B3=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_eventmonitor/handle.py | 92 ++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/nonebot_plugin_eventmonitor/handle.py b/nonebot_plugin_eventmonitor/handle.py index a439a63..2d1416f 100644 --- a/nonebot_plugin_eventmonitor/handle.py +++ b/nonebot_plugin_eventmonitor/handle.py @@ -156,22 +156,56 @@ async def switch( self, matcher: Matcher, event: GroupMessageEvent, - ) -> None: + ) -> bool: """获取开关指令的参数,即用户输入的指令内容""" command = str(event.get_message()).strip() - # 获取群组ID gid = str(event.group_id) - # 判断指令是否包含"开启"或"关闭"关键字 + if 'event全部功能' in command or '全部event功能' in command: + await self._handle_all_switch(command, gid, matcher) + else: + await self._handle_single_switch(command, gid, matcher) + return False + + async def _handle_all_switch( + self, + command: str, + gid: str, + matcher: Matcher, + ) -> bool: + """处理一键开启/关闭所有功能""" if '开启' in command or '开始' in command: - if key := utils.get_command_type(command): + for key in utils.path: utils.g_temp[gid][key] = True - await utils.write_group_data(utils.g_temp) - name = utils.get_function_name(key) - if not (await utils.check_txt_to_img(config_data.event_check_txt_img)): - await matcher.finish(f'{name}功能已开启喵') - else: - await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(f'{name}功能已开启喵'))) - elif ('禁止' in command or '关闭' in command) and (key := utils.get_command_type(command)): + await utils.write_group_data(utils.g_temp) + msg = '所有功能已一键开启喵' + await self._send_switch_msg(msg, matcher) + return True + if '禁止' in command or '关闭' in command: + for key in utils.path: + utils.g_temp[gid][key] = False + await utils.write_group_data(utils.g_temp) + msg = '所有功能已一键关闭喵' + await self._send_switch_msg(msg, matcher) + return True + return False + + async def _handle_single_switch( + self, + command: str, + gid: str, + matcher: Matcher, + ) -> bool: + """处理单项功能开关""" + if ('开启' in command or '开始' in command) and (key := utils.get_command_type(command)): + utils.g_temp[gid][key] = True + await utils.write_group_data(utils.g_temp) + name = utils.get_function_name(key) + if not (await utils.check_txt_to_img(config_data.event_check_txt_img)): + await matcher.finish(f'{name}功能已开启喵') + else: + await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(f'{name}功能已开启喵'))) + return True + if ('禁止' in command or '关闭' in command) and (key := utils.get_command_type(command)): utils.g_temp[gid][key] = False await utils.write_group_data(utils.g_temp) name = utils.get_function_name(key) @@ -179,6 +213,15 @@ async def switch( await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(f'{name}功能已关闭喵'))) else: await matcher.finish(f'{name}功能已关闭喵') + return True + return False + + async def _send_switch_msg(self, msg: str, matcher: Matcher) -> None: + """根据配置发送图片或文本消息""" + if await utils.check_txt_to_img(config_data.event_check_txt_img): + await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(msg))) + else: + await matcher.finish(msg) async def usage(self, matcher: Matcher) -> None: """获取指令帮助""" @@ -187,7 +230,7 @@ async def usage(self, matcher: Matcher) -> None: else: await matcher.finish(utils.usage) - async def state(self, matcher: Matcher, event: GroupMessageEvent) -> None: + async def state(self, matcher: Matcher, event: GroupMessageEvent) -> bool: """指令开关""" gid = str(event.group_id) group_status: dict = await utils.load_current_config() @@ -201,12 +244,19 @@ async def state(self, matcher: Matcher, event: GroupMessageEvent) -> None: await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(rely_msg))) else: await matcher.finish(rely_msg) + return True async def check_plugin(self) -> str | None: """检测插件更新""" try: await self.check_plugin_update() - except (HTTPStatusError, ConnectError, RequestError, TimeoutError, json.JSONDecodeError) as e: + except ( + HTTPStatusError, + ConnectError, + RequestError, + TimeoutError, + json.JSONDecodeError, + ) as e: logger.exception('检查更新时发生未处理的异常') return f'检查更新失败: {e}' @@ -242,7 +292,13 @@ async def check_plugin_update(self) -> bool: if not data or 'tag_name' not in data: logger.error('获取版本数据无效') return False - except (HTTPStatusError, ConnectError, RequestError, TimeoutError, json.JSONDecodeError) as e: + except ( + HTTPStatusError, + ConnectError, + RequestError, + TimeoutError, + json.JSONDecodeError, + ) as e: logger.error(f'获取最新版本数据失败: {e}') raise current = str(version.parse(utils.current_version.lstrip('v'))) @@ -267,7 +323,13 @@ async def job_plugin_update(self) -> bool: if not data or 'tag_name' not in data: logger.error('获取版本数据无效') return False - except (HTTPStatusError, ConnectError, RequestError, TimeoutError, json.JSONDecodeError) as e: + except ( + HTTPStatusError, + ConnectError, + RequestError, + TimeoutError, + json.JSONDecodeError, + ) as e: logger.error(f'获取最新版本数据失败: {e}') raise current = str(version.parse(utils.current_version.lstrip('v'))) From e0155fc031d59e2446599f3bf9faf29c6dca49e9 Mon Sep 17 00:00:00 2001 From: Reversedeer Date: Fri, 25 Jul 2025 20:53:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8Dbot?= =?UTF-8?q?=E9=80=80=E7=BE=A4=E5=90=8E=E4=BA=A7=E7=94=9F=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_eventmonitor/__init__.py | 2 +- nonebot_plugin_eventmonitor/config.py | 8 ++++++- nonebot_plugin_eventmonitor/handle.py | 28 ++++++++++++++++++------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/nonebot_plugin_eventmonitor/__init__.py b/nonebot_plugin_eventmonitor/__init__.py index ea3d4a8..15ff4ad 100644 --- a/nonebot_plugin_eventmonitor/__init__.py +++ b/nonebot_plugin_eventmonitor/__init__.py @@ -125,7 +125,7 @@ async def _() -> None: supported_adapters={'~onebot.v11'}, extra={ 'author': 'Reversedeer', - 'version': '0.4.8', + 'version': '0.4.9', 'priority': 50, 'email': 'ysjvillmark@gmail.com', }, diff --git a/nonebot_plugin_eventmonitor/config.py b/nonebot_plugin_eventmonitor/config.py index 9391e3b..e0c6bdf 100644 --- a/nonebot_plugin_eventmonitor/config.py +++ b/nonebot_plugin_eventmonitor/config.py @@ -57,7 +57,7 @@ def __init__(self) -> None: } self.g_temp = {} self.chuo_CD_dir = {} - self.current_version = 'v0.4.8' + self.current_version = 'v0.4.9' self.config_path: Path = store.get_plugin_config_dir() self.data_address: Path = self.config_path / 'config.json' self.release_url = 'https://api.github.com/repos/Reversedeer/nonebot_plugin_eventmonitor/releases/latest' @@ -150,6 +150,12 @@ async def update_missing_config(self, config: dict) -> dict: del current_group[key] return config + async def remove_group_config(self, gid: str) -> None: + """删除指定群的配置""" + if gid in self.g_temp: + del self.g_temp[gid] + await self.write_group_data(self.g_temp) + @staticmethod async def check_chuo(g_temp: dict, gid: str) -> bool: """检查戳一戳是否允许""" diff --git a/nonebot_plugin_eventmonitor/handle.py b/nonebot_plugin_eventmonitor/handle.py index 2d1416f..36fe885 100644 --- a/nonebot_plugin_eventmonitor/handle.py +++ b/nonebot_plugin_eventmonitor/handle.py @@ -86,10 +86,18 @@ async def del_user( matcher: Matcher, event: GroupDecreaseNoticeEvent, bot: Bot, - ) -> None: + ) -> bool: """退群事件""" + gid = str(event.group_id) + # 检查 bot 是否还在群,不在则删除配置 + group_list = await bot.get_group_list() + group_ids = {str(g['group_id']) for g in group_list} + if gid not in group_ids: + await utils.remove_group_config(gid) + return False + if not (await utils.check_del_user(utils.g_temp, str(event.group_id))): - return + return False user_id: int = event.user_id member_info: dict = await bot.get_stranger_info(user_id=user_id) nickname: str = member_info.get('nickname', '未知昵称') @@ -98,17 +106,18 @@ async def del_user( await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(str(rely_msg))), at_sender=True) else: await matcher.finish(rely_msg) + return True async def add_user( self, matcher: Matcher, event: GroupIncreaseNoticeEvent, bot: Bot, - ) -> None: + ) -> bool: """入群事件""" await utils.config_check() if not (await utils.check_add_user(utils.g_temp, str(event.group_id))): - return + return False bot_qq = int(bot.self_id) user_id: int = event.user_id group_id: int = event.group_id @@ -119,38 +128,41 @@ async def add_user( await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(str(rely_msg))), at_sender=True) else: await matcher.finish(rely_msg) + return True async def admin_chance( self, matcher: Matcher, event: GroupAdminNoticeEvent, bot: Bot, - ) -> None: + ) -> bool: """管理员变动事件""" if not (await utils.check_admin(utils.g_temp, str(event.group_id))): - return + return False bot_qq = int(bot.self_id) rely_msg: str = await message.admin_changer(event.sub_type, event.user_id, bot_qq) if await utils.check_txt_to_img(config_data.event_check_txt_img): await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(rely_msg)), at_sender=True) else: await matcher.finish(rely_msg) + return True async def hongbao( self, matcher: Matcher, event: LuckyKingNotifyEvent, bot: Bot, - ) -> None: + ) -> bool: """红包运气王事件""" if not (await utils.check_red_package(utils.g_temp, str(event.group_id))): - return + return False bot_qq = int(bot.self_id) rely_msg = await message.rad_package_change(event.target_id, bot_qq) if await utils.check_txt_to_img(config_data.event_check_txt_img): await matcher.send(MessageSegment.image(await txt_to_img.txt_to_img(rely_msg)), at_sender=True) else: await matcher.finish(rely_msg) + return True async def switch( self, From 474c5af40c2f10d59deccf2ebcee14085e5c92a1 Mon Sep 17 00:00:00 2001 From: Reversedeer Date: Fri, 25 Jul 2025 21:31:58 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=92=A5=20Release=20v0.4.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++++- pyproject.toml | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc78268..e93d6af 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,12 @@ json结构(默认值):

更新日志

-- v0.4.8 +- v0.4.9 + + - ✨ 新增群配置一键开关功能 + - 🐛 修复bot退群后产生的异常报错 +- v0.4.8 - ✨ 使用UTC-8显示时间 - v0.4.6 diff --git a/pyproject.toml b/pyproject.toml index 0285b1d..6dc58e1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-plugin-eventmonitor" -version = "0.4.8" +version = "0.4.9" description = "监控群事件的插件 For Nonebot2" authors = [{ name = "Reversedeer", email = "ysjvillmark@gmail.com" }] readme = "README.md"