mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 20:00:04 +08:00
feat(init): 添加自动清理无效插件和群组数据功能 (#2116)
* feat(init): 添加自动清理无效插件和群组数据功能 - 添加nonebot_plugin_apscheduler依赖用于定时任务 - 导入ChatHistory模型用于清理聊天记录 - 移除CacheException相关异常处理代码 - 重写群组同步逻辑,支持创建新群组记录和删除已退出群组 - 添加定时清理已退出群组聊天记录的功能 - 优化日志输出格式和信息内容 * refactor(manager): 添加E501忽略注释以修复代码风格问题 在Manager类的方法中添加noqa: E501注释,解决docstring行长超过限制的代码检查警告 * feat(init): 添加自动清理聊天记录配置选项 - 引入Config配置模块 - 新增auto_clean.CLEAN_CHAT_HISTORY配置项,控制是否自动清理已退出群聊的聊天记录 - 根据配置决定是否添加定时清理任务,实现可配置的聊天历史清理功能
This commit is contained in:
@@ -2,23 +2,33 @@ from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from nonebot.adapters import Bot
|
||||
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.cache import CacheException
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
from .__init_cache import register_cache_types
|
||||
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
|
||||
try:
|
||||
from .__init_cache import register_cache_types
|
||||
except CacheException as e:
|
||||
raise SystemError(f"ERROR:{e}")
|
||||
|
||||
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()
|
||||
@@ -27,29 +37,87 @@ async def _():
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _(bot: Bot):
|
||||
"""将bot已存在的群组添加群认证
|
||||
"""同步 Bot 已存在的群组到 GroupConsole,并清理已退出的群
|
||||
|
||||
参数:
|
||||
bot: Bot
|
||||
"""
|
||||
if PlatformUtils.get_platform(bot) != "qq":
|
||||
return
|
||||
logger.debug(f"更新Bot: {bot.self_id} 的群认证...")
|
||||
group_list, _ = await PlatformUtils.get_group_list(bot)
|
||||
db_group_list = await GroupConsole.all().values_list("group_id", flat=True)
|
||||
|
||||
logger.debug(f"更新Bot: {bot.self_id} 的群认证...", "群认证同步")
|
||||
|
||||
# 实际在用的群列表(当前 bot 连接可见的群)
|
||||
current_group_list, _ = await PlatformUtils.get_group_list(bot)
|
||||
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
|
||||
) # pyright: ignore[reportAssignmentType]
|
||||
db_group_ids = set(db_group_list)
|
||||
|
||||
# 需要创建的群(当前存在,但数据库中没有)
|
||||
create_list = []
|
||||
update_id = []
|
||||
for group in group_list:
|
||||
if group.group_id not in db_group_list:
|
||||
for group in current_group_list:
|
||||
if group.group_id not in db_group_ids:
|
||||
group.group_flag = 1
|
||||
create_list.append(group)
|
||||
else:
|
||||
update_id.append(group.group_id)
|
||||
|
||||
if create_list:
|
||||
await GroupConsole.bulk_create(create_list, 10)
|
||||
|
||||
if delete_ids := list(db_group_ids - current_group_ids):
|
||||
deleted_count = await GroupConsole.filter(group_id__in=delete_ids).delete()
|
||||
else:
|
||||
await GroupConsole.filter(group_id__in=update_id).update(group_flag=1)
|
||||
logger.debug(
|
||||
deleted_count = 0
|
||||
logger.info(
|
||||
f"更新Bot: {bot.self_id} 的群认证完成,共创建 {len(create_list)} 条数据,"
|
||||
f"共修改 {len(update_id)} 条数据..."
|
||||
f"删除 {deleted_count} 条已退出群组的数据...",
|
||||
"群认证同步",
|
||||
)
|
||||
|
||||
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)
|
||||
.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)} 条已退出群组的聊天记录", "定时清理群组聊天记录")
|
||||
|
||||
Reference in New Issue
Block a user