mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
🐛 移除群组数据和聊天数据删除代码 (#2128)
This commit is contained in:
@@ -3,10 +3,7 @@ from pathlib import Path
|
|||||||
import nonebot
|
import nonebot
|
||||||
from nonebot.adapters import Bot
|
from nonebot.adapters import Bot
|
||||||
from nonebot.adapters.onebot.v11.exception import NetworkError
|
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.models.group_console import GroupConsole
|
||||||
from zhenxun.services.log import logger
|
from zhenxun.services.log import logger
|
||||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||||
@@ -20,16 +17,6 @@ nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
|||||||
driver = nonebot.get_driver()
|
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)
|
@PriorityLifecycle.on_startup(priority=5)
|
||||||
async def _():
|
async def _():
|
||||||
register_cache_types()
|
register_cache_types()
|
||||||
@@ -56,7 +43,6 @@ async def _(bot: Bot):
|
|||||||
"群认证同步",
|
"群认证同步",
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
current_group_ids = {g.group_id for g in current_group_list}
|
|
||||||
|
|
||||||
db_group_list: list[str] = await GroupConsole.all().values_list(
|
db_group_list: list[str] = await GroupConsole.all().values_list(
|
||||||
"group_id", flat=True
|
"group_id", flat=True
|
||||||
@@ -83,82 +69,7 @@ async def _(bot: Bot):
|
|||||||
for group in fresh:
|
for group in fresh:
|
||||||
await GroupMemoryCache.upsert_from_model(group)
|
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(
|
logger.info(
|
||||||
f"更新Bot: {bot.self_id} 的群认证完成,共创建 {len(create_list)} 条数据,"
|
f"更新Bot: {bot.self_id} 的群认证完成,共创建 {len(create_list)} 条数据,",
|
||||||
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)
|
|
||||||
.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)} 条已退出群组的聊天记录", "定时清理群组聊天记录")
|
|
||||||
|
|||||||
@@ -297,27 +297,27 @@ class GroupConsole(Model):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def is_superuser_block_plugin(cls, group_id: str, module: str) -> bool:
|
async def is_superuser_block_plugin(cls, group_id: str, module: str) -> bool:
|
||||||
group = GroupMemoryCache.get_if_ready(group_id, None)
|
if group := GroupMemoryCache.get_if_ready(group_id, None):
|
||||||
if not group:
|
return bool(
|
||||||
|
group.superuser_block_plugin_set
|
||||||
|
and module in group.superuser_block_plugin_set
|
||||||
|
)
|
||||||
|
else:
|
||||||
return False
|
return False
|
||||||
return bool(
|
|
||||||
group.superuser_block_plugin_set
|
|
||||||
and module in group.superuser_block_plugin_set
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def is_block_plugin(cls, group_id: str, module: str) -> bool:
|
async def is_block_plugin(cls, group_id: str, module: str) -> bool:
|
||||||
group = GroupMemoryCache.get_if_ready(group_id, None)
|
if group := GroupMemoryCache.get_if_ready(group_id, None):
|
||||||
if not group:
|
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
|
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
|
@classmethod
|
||||||
async def set_block_plugin(
|
async def set_block_plugin(
|
||||||
@@ -401,19 +401,20 @@ class GroupConsole(Model):
|
|||||||
async def is_normal_block_plugin(
|
async def is_normal_block_plugin(
|
||||||
cls, group_id: str, module: str, channel_id: str | None = None
|
cls, group_id: str, module: str, channel_id: str | None = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
group = GroupMemoryCache.get_if_ready(group_id, channel_id)
|
if group := GroupMemoryCache.get_if_ready(group_id, channel_id):
|
||||||
if not group:
|
return bool(group.block_plugin_set and module in group.block_plugin_set)
|
||||||
|
else:
|
||||||
return False
|
return False
|
||||||
return bool(group.block_plugin_set and module in group.block_plugin_set)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def is_superuser_block_task(cls, group_id: str, task: str) -> bool:
|
async def is_superuser_block_task(cls, group_id: str, task: str) -> bool:
|
||||||
group = GroupMemoryCache.get_if_ready(group_id, None)
|
if group := GroupMemoryCache.get_if_ready(group_id, None):
|
||||||
if not group:
|
return bool(
|
||||||
|
group.superuser_block_task_set
|
||||||
|
and task in group.superuser_block_task_set
|
||||||
|
)
|
||||||
|
else:
|
||||||
return False
|
return False
|
||||||
return bool(
|
|
||||||
group.superuser_block_task_set and task in group.superuser_block_task_set
|
|
||||||
)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def is_block_task(
|
async def is_block_task(
|
||||||
@@ -425,23 +426,19 @@ class GroupConsole(Model):
|
|||||||
return False
|
return False
|
||||||
if group.block_task_set and task in group.block_task_set:
|
if group.block_task_set and task in group.block_task_set:
|
||||||
return True
|
return True
|
||||||
if (
|
return bool(
|
||||||
group.superuser_block_task_set
|
group.superuser_block_task_set
|
||||||
and task in 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)
|
group = GroupMemoryCache.get_if_ready(group_id, channel_id)
|
||||||
if group and group.block_task_set and task in group.block_task_set:
|
if group and group.block_task_set and task in group.block_task_set:
|
||||||
return True
|
return True
|
||||||
super_group = GroupMemoryCache.get_if_ready(group_id, None)
|
super_group = GroupMemoryCache.get_if_ready(group_id, None)
|
||||||
if (
|
return bool(
|
||||||
super_group
|
super_group
|
||||||
and super_group.superuser_block_task_set
|
and super_group.superuser_block_task_set
|
||||||
and task in super_group.superuser_block_task_set
|
and task in super_group.superuser_block_task_set
|
||||||
):
|
)
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def set_block_task(
|
async def set_block_task(
|
||||||
@@ -536,15 +533,12 @@ class GroupConsole(Model):
|
|||||||
if "postgres" in db_type:
|
if "postgres" in db_type:
|
||||||
scripts.extend(
|
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 "
|
"ALTER TABLE group_console ALTER COLUMN "
|
||||||
"superuser_block_plugin TYPE TEXT;"
|
"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 "
|
"ALTER TABLE group_console ALTER COLUMN "
|
||||||
"superuser_block_task TYPE TEXT;"
|
"superuser_block_task TYPE TEXT;"
|
||||||
@@ -554,12 +548,12 @@ class GroupConsole(Model):
|
|||||||
elif "mysql" in db_type:
|
elif "mysql" in db_type:
|
||||||
scripts.extend(
|
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 "
|
"ALTER TABLE group_console MODIFY COLUMN "
|
||||||
"superuser_block_plugin TEXT;"
|
"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 "
|
"ALTER TABLE group_console MODIFY COLUMN "
|
||||||
"superuser_block_task TEXT;"
|
"superuser_block_task TEXT;"
|
||||||
|
|||||||
Reference in New Issue
Block a user