From fb171e2d4baa1d4a1df9fb7b9a40927ca4d0474e Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Thu, 8 Aug 2024 04:41:35 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E4=B8=A4=E6=97=A5=E5=86=85?= =?UTF-8?q?=E6=9C=AA=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E7=9A=84=E7=BE=A4?= =?UTF-8?q?=E7=BB=84=E5=B0=86=E8=A2=AB=E5=85=B3=E9=97=AD=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=A2=AB=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat_history/chat_message.py | 3 +- .../builtin_plugins/scheduler/chat_check.py | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 zhenxun/builtin_plugins/scheduler/chat_check.py diff --git a/zhenxun/builtin_plugins/chat_history/chat_message.py b/zhenxun/builtin_plugins/chat_history/chat_message.py index 8200ab5e..2cfdd607 100644 --- a/zhenxun/builtin_plugins/chat_history/chat_message.py +++ b/zhenxun/builtin_plugins/chat_history/chat_message.py @@ -44,7 +44,8 @@ TEMP_LIST = [] @chat_history.handle() async def _(message: UniMsg, session: EventSession): - group_id = session.id3 or session.id2 + # group_id = session.id3 or session.id2 + group_id = session.id2 TEMP_LIST.append( ChatHistory( user_id=session.id1, diff --git a/zhenxun/builtin_plugins/scheduler/chat_check.py b/zhenxun/builtin_plugins/scheduler/chat_check.py new file mode 100644 index 00000000..599dff35 --- /dev/null +++ b/zhenxun/builtin_plugins/scheduler/chat_check.py @@ -0,0 +1,54 @@ +from datetime import datetime, timedelta + +import nonebot +import pytz +from nonebot_plugin_apscheduler import scheduler + +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 + + +@scheduler.scheduled_job( + "cron", + hour=4, + minute=40, +) +async def _(): + """检测群组发言时间并禁用全部被动""" + update_list = [] + for bot in nonebot.get_bots().values(): + group_list, _ = await PlatformUtils.get_group_list(bot) + group_list = [g for g in group_list if g.channel_id == None] + 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 modules := await TaskInfo.annotate().values_list( + "module", flat=True + ): + if now - timedelta(days=2) > last_message.create_time: + _group, _ = await GroupConsole.get_or_create( + group_id=group.group_id, channel_id__isnull=True + ) + _group.block_task = ",".join(modules) + "," # type: ignore + update_list.append(_group) + logger.info( + "群组两日内未发送任何消息,关闭该群全部被动", + "Chat检测", + target=_group.group_id, + ) + except Exception as e: + logger.error( + "检测群组发言时间失败...", "Chat检测", target=group.group_id + ) + if update_list: + await GroupConsole.bulk_update(update_list, ["block_task"], 10)