From 9cb40f0432b70d6e8f6d3f3d4c14d4855761bc60 Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Mon, 27 Apr 2026 16:09:33 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E7=A7=BB=E9=99=A4=E7=BE=A4=E7=BB=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=92=8C=E8=81=8A=E5=A4=A9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BB=A3=E7=A0=81=20(#2128)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhenxun/builtin_plugins/init/__init__.py | 91 +----------------------- zhenxun/models/group_console.py | 72 +++++++++---------- 2 files changed, 34 insertions(+), 129 deletions(-) diff --git a/zhenxun/builtin_plugins/init/__init__.py b/zhenxun/builtin_plugins/init/__init__.py index 4e050502..b5574233 100644 --- a/zhenxun/builtin_plugins/init/__init__.py +++ b/zhenxun/builtin_plugins/init/__init__.py @@ -3,10 +3,7 @@ from pathlib import Path import nonebot from nonebot.adapters import Bot from nonebot.adapters.onebot.v11.exception import NetworkError -from nonebot_plugin_apscheduler import scheduler -from zhenxun.configs.config import Config -from zhenxun.models.chat_history import ChatHistory from zhenxun.models.group_console import GroupConsole from zhenxun.services.log import logger from zhenxun.utils.manager.priority_manager import PriorityLifecycle @@ -20,16 +17,6 @@ nonebot.load_plugins(str(Path(__file__).parent.resolve())) driver = nonebot.get_driver() -Config.add_plugin_config( - "auto_clean", - "CLEAN_CHAT_HISTORY", - True, - help="是否自动清理已退出群聊的聊天记录", - default_value=True, - type=bool, -) - - @PriorityLifecycle.on_startup(priority=5) async def _(): register_cache_types() @@ -56,7 +43,6 @@ async def _(bot: Bot): "群认证同步", ) return - current_group_ids = {g.group_id for g in current_group_list} db_group_list: list[str] = await GroupConsole.all().values_list( "group_id", flat=True @@ -83,82 +69,7 @@ async def _(bot: Bot): for group in fresh: await GroupMemoryCache.upsert_from_model(group) - all_bots = nonebot.get_bots() - all_visible: set[str] = set(current_group_ids) - for other_bot in all_bots.values(): - if other_bot is bot: - continue - if PlatformUtils.get_platform(other_bot) != "qq": - continue - try: - other_groups, _ = await PlatformUtils.get_group_list(other_bot) - all_visible.update(g.group_id for g in other_groups) - except NetworkError as e: - reason = ( - f"Bot: {other_bot.self_id} 群列表同步被连接关闭打断," - f"回退到数据库集合: {e}" - ) - logger.debug( - reason, - "群认证同步", - ) - all_visible.update(db_group_ids) - break - except Exception: - all_visible.update(db_group_ids) - break - - if delete_ids := list(db_group_ids - all_visible): - deleted_count = await GroupConsole.filter(group_id__in=delete_ids).delete() - else: - deleted_count = 0 logger.info( - f"更新Bot: {bot.self_id} 的群认证完成,共创建 {len(create_list)} 条数据," - f"删除 {deleted_count} 条已退出群组的数据...", + f"更新Bot: {bot.self_id} 的群认证完成,共创建 {len(create_list)} 条数据,", "群认证同步", ) - - if Config.get_config("auto_clean", "CLEAN_CHAT_HISTORY"): - scheduler.add_job( - clean_chat_history, - "cron", - hour=1, - minute=0, - args=(current_group_list,), - id="clean_chat_history", - replace_existing=True, - ) - - -async def clean_chat_history( - group_list: list[GroupConsole], - max_delete: int = 2000, -): - """清理已退出群组的聊天记录 - - 为避免一次调用删除过多数据,单次调用最多删除 max_delete 条。 - """ - # 将传入的对象统一转成 group_id 字符串列表 - group_ids: list[str] = [g.group_id for g in group_list] - - if not group_ids: - logger.warning("传入群组列表为空,跳过清理", "定时清理群组聊天记录") - return - - # 只取最多 max_delete 条记录的 id,然后删除这些记录,避免一次删太多 - ids = ( - await ChatHistory.filter(group_id__not_in=group_ids) - .order_by("create_time") - .limit(max_delete) - .values_list("id", flat=True) - ) - ids = list(ids) - if not ids: - logger.info( - f"群组数 {len(group_ids)},无聊天记录可删除", "定时清理群组聊天记录" - ) - return - - await ChatHistory.filter(id__in=ids).delete() - - logger.success(f"已清理 {len(ids)} 条已退出群组的聊天记录", "定时清理群组聊天记录") diff --git a/zhenxun/models/group_console.py b/zhenxun/models/group_console.py index cd8c402f..c5c5644d 100644 --- a/zhenxun/models/group_console.py +++ b/zhenxun/models/group_console.py @@ -297,27 +297,27 @@ class GroupConsole(Model): @classmethod async def is_superuser_block_plugin(cls, group_id: str, module: str) -> bool: - group = GroupMemoryCache.get_if_ready(group_id, None) - if not group: + if group := GroupMemoryCache.get_if_ready(group_id, None): + return bool( + group.superuser_block_plugin_set + and module in group.superuser_block_plugin_set + ) + else: return False - return bool( - group.superuser_block_plugin_set - and module in group.superuser_block_plugin_set - ) @classmethod async def is_block_plugin(cls, group_id: str, module: str) -> bool: - group = GroupMemoryCache.get_if_ready(group_id, None) - if not group: + if group := GroupMemoryCache.get_if_ready(group_id, None): + return ( + True + if group.block_plugin_set and module in group.block_plugin_set + else bool( + group.superuser_block_plugin_set + and module in group.superuser_block_plugin_set + ) + ) + else: return False - if group.block_plugin_set and module in group.block_plugin_set: - return True - if ( - group.superuser_block_plugin_set - and module in group.superuser_block_plugin_set - ): - return True - return False @classmethod async def set_block_plugin( @@ -401,19 +401,20 @@ class GroupConsole(Model): async def is_normal_block_plugin( cls, group_id: str, module: str, channel_id: str | None = None ) -> bool: - group = GroupMemoryCache.get_if_ready(group_id, channel_id) - if not group: + if group := GroupMemoryCache.get_if_ready(group_id, channel_id): + return bool(group.block_plugin_set and module in group.block_plugin_set) + else: return False - return bool(group.block_plugin_set and module in group.block_plugin_set) @classmethod async def is_superuser_block_task(cls, group_id: str, task: str) -> bool: - group = GroupMemoryCache.get_if_ready(group_id, None) - if not group: + if group := GroupMemoryCache.get_if_ready(group_id, None): + return bool( + group.superuser_block_task_set + and task in group.superuser_block_task_set + ) + else: return False - return bool( - group.superuser_block_task_set and task in group.superuser_block_task_set - ) @classmethod async def is_block_task( @@ -425,23 +426,19 @@ class GroupConsole(Model): return False if group.block_task_set and task in group.block_task_set: return True - if ( + return bool( group.superuser_block_task_set and task in group.superuser_block_task_set - ): - return True - return False + ) group = GroupMemoryCache.get_if_ready(group_id, channel_id) if group and group.block_task_set and task in group.block_task_set: return True super_group = GroupMemoryCache.get_if_ready(group_id, None) - if ( + return bool( super_group and super_group.superuser_block_task_set and task in super_group.superuser_block_task_set - ): - return True - return False + ) @classmethod async def set_block_task( @@ -536,15 +533,12 @@ class GroupConsole(Model): if "postgres" in db_type: scripts.extend( [ - ( - "ALTER TABLE group_console ALTER COLUMN " - "block_plugin TYPE TEXT;" - ), + ("ALTER TABLE group_console ALTER COLUMN block_plugin TYPE TEXT;"), ( "ALTER TABLE group_console ALTER COLUMN " "superuser_block_plugin TYPE TEXT;" ), - ("ALTER TABLE group_console ALTER COLUMN " "block_task TYPE TEXT;"), + ("ALTER TABLE group_console ALTER COLUMN block_task TYPE TEXT;"), ( "ALTER TABLE group_console ALTER COLUMN " "superuser_block_task TYPE TEXT;" @@ -554,12 +548,12 @@ class GroupConsole(Model): elif "mysql" in db_type: scripts.extend( [ - ("ALTER TABLE group_console MODIFY COLUMN " "block_plugin TEXT;"), + ("ALTER TABLE group_console MODIFY COLUMN block_plugin TEXT;"), ( "ALTER TABLE group_console MODIFY COLUMN " "superuser_block_plugin TEXT;" ), - ("ALTER TABLE group_console MODIFY COLUMN " "block_task TEXT;"), + ("ALTER TABLE group_console MODIFY COLUMN block_task TEXT;"), ( "ALTER TABLE group_console MODIFY COLUMN " "superuser_block_task TEXT;"