zhenxun_bot/zhenxun/builtin_plugins/scheduler/chat_check.py
HibiKier 35014e4048
重构webui适配 (#1801)
* ♻️ 使用Uninfo重构PlatformUtils基础方法

* 🩹 优化插件加载与模块格式转换逻辑

* 🚑 修复商店道具无法使用

* 🚑 修复道具无法正常使用

* 🔧 增加Bot状态管理及模块禁用功能

* 🎨  优化Web UI代码结构,修改target方法

* 🚨 auto fix by pre-commit hooks

* 🎨 添加菜单API及优化异常处理

* 🐛 优化菜单API及模型结构,修复WebUi插件列表Api

* 📝 更新仓库readme

* 🚨 add mdlint file

* 📝 Add help chapter.

* 🐛 修复优化AuthChecker逻辑

* 🐛 优化数据库API,移除冗余导入及修正SQL_DICT引用

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: BalconyJH <balconyjh@gmail.com>
2024-12-25 12:03:49 +08:00

69 lines
2.7 KiB
Python

from datetime import datetime, timedelta
import nonebot
from nonebot_plugin_apscheduler import scheduler
import pytz
from zhenxun.configs.config import Config
from zhenxun.models.chat_history import ChatHistory
from zhenxun.models.group_console import GroupConsole
from zhenxun.models.task_info import TaskInfo
from zhenxun.services.log import logger
from zhenxun.utils.platform import PlatformUtils
Config.add_plugin_config(
"chat_check",
"STATUS",
True,
help="是否开启群组两日内未发送任何消息,关闭该群全部被动",
default_value=True,
type=bool,
)
@scheduler.scheduled_job(
"cron",
hour=4,
minute=40,
)
async def _():
if not Config.get_config("chat_history", "FLAG"):
logger.debug("未开启历史发言记录,过滤群组发言检测...")
return
if not Config.get_config("chat_check", "STATUS"):
logger.debug("未开启群组聊天时间检查,过滤群组发言检测...")
return
"""检测群组发言时间并禁用全部被动"""
update_list = []
if modules := await TaskInfo.annotate().values_list("module", flat=True):
for bot in nonebot.get_bots().values():
group_list, _ = await PlatformUtils.get_group_list(bot, True)
for group in group_list:
try:
last_message = (
await ChatHistory.filter(group_id=group.group_id)
.annotate()
.order_by("-create_time")
.first()
)
if last_message:
now = datetime.now(pytz.timezone("Asia/Shanghai"))
if now - timedelta(days=2) > last_message.create_time:
_group, _ = await GroupConsole.get_or_create(
group_id=group.group_id, channel_id__isnull=True
)
modules = [f"<{module}" for module in modules]
_group.block_task = ",".join(modules) + "," # type: ignore
update_list.append(_group)
logger.info(
"群组两日内未发送任何消息,关闭该群全部被动",
"Chat检测",
target=_group.group_id,
)
except Exception:
logger.error(
"检测群组发言时间失败...", "Chat检测", target=group.group_id
)
if update_list:
await GroupConsole.bulk_update(update_list, ["block_task"], 10)