From c87f36973f1680089e910922aaeeffb678d75ebf Mon Sep 17 00:00:00 2001 From: A0000Xz <122650088+A0000Xz@users.noreply.github.com> Date: Fri, 11 Jul 2025 19:30:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96no=5Freply=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=92=8C=E5=8F=82=E6=95=B0=EF=BC=8C=E9=80=82=E9=85=8D=E7=A7=81?= =?UTF-8?q?=E8=81=8A=EF=BC=8C=E8=A7=A3=E5=86=B3=E6=8F=92=E7=A9=BA=E7=9A=84?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=97=A0=E5=8F=8D=E5=BA=94=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/built_in/core_actions/no_reply.py | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/src/plugins/built_in/core_actions/no_reply.py b/src/plugins/built_in/core_actions/no_reply.py index 99337e515..f1c2b640b 100644 --- a/src/plugins/built_in/core_actions/no_reply.py +++ b/src/plugins/built_in/core_actions/no_reply.py @@ -1,5 +1,6 @@ import random import time +import asyncio from typing import Tuple # 导入新插件系统 @@ -15,6 +16,8 @@ from src.config.config import global_config logger = get_logger("core_actions") +#设置一个全局字典,确保同一个消息流的下一个NoReplyAction实例能够获取到上一次消息的时间戳 +_CHAT_START_TIMES = {} class NoReplyAction(BaseAction): """不回复动作,根据新消息的兴趣值或数量决定何时结束等待. @@ -39,29 +42,47 @@ class NoReplyAction(BaseAction): # 新增:兴趣值退出阈值 _interest_exit_threshold = 3.0 # 新增:消息数量退出阈值 - _min_exit_message_count = 5 - _max_exit_message_count = 10 + _min_exit_message_count = 4 + _max_exit_message_count = 8 # 动作参数定义 action_parameters = {"reason": "不回复的原因"} # 动作使用场景 - action_require = ["你发送了消息,目前无人回复"] + action_require = [ + "你发送了消息,目前无人回复", + "你觉得对方还没把话说完", + "你觉得当前话题不适合插嘴", + "你觉得自己说话太多了" + ] # 关联类型 associated_types = [] async def execute(self) -> Tuple[bool, str]: """执行不回复动作""" - import asyncio - try: + + # 获取或初始化当前消息的起始时间,因为用户消息是可能在刚决定好可用动作,但还没选择动作的时候发送的。原先的start_time设计会导致这种消息被漏掉,现在采用全局字典存储 + if self.chat_id not in _CHAT_START_TIMES: + # 如果对应消息流没有存储时间,就设置为当前时间 + _CHAT_START_TIMES[self.chat_id] = time.time() + start_time = _CHAT_START_TIMES[self.chat_id] + else: + message_current_time = time.time() + if message_current_time - _CHAT_START_TIMES[self.chat_id] > 600: + # 如果上一次NoReplyAction实例记录的最后消息时间戳距离现在时间戳超过了十分钟,将会把start_time设置为当前时间戳,避免在数据库内过度搜索 + start_time = message_current_time + logger.debug("距离上一次消息时间过长,已重置等待开始时间为当前时间") + else: + # 如果距离上一次noreply没有十分钟,就沿用上一次noreply退出时记录的最新消息时间戳 + start_time = _CHAT_START_TIMES[self.chat_id] + # 增加连续计数 NoReplyAction._consecutive_count += 1 count = NoReplyAction._consecutive_count reason = self.action_data.get("reason", "") - start_time = time.time() check_interval = 1.0 # 每秒检查一次 # 随机生成本次等待需要的新消息数量阈值 @@ -69,6 +90,9 @@ class NoReplyAction(BaseAction): logger.info( f"{self.log_prefix} 本次no_reply需要 {exit_message_count_threshold} 条新消息或累计兴趣值超过 {self._interest_exit_threshold} 才能打断" ) + if not self.is_group: + exit_message_count_threshold = 1 + logger.info(f"检测到当前环境为私聊,本次no_reply已更正为需要{exit_message_count_threshold}条新消息就能打断") logger.info(f"{self.log_prefix} 选择不回复(第{count}次),开始摸鱼,原因: {reason}") @@ -77,9 +101,9 @@ class NoReplyAction(BaseAction): current_time = time.time() elapsed_time = current_time - start_time - # 1. 检查新消息 + # 1. 检查新消息,默认过滤麦麦自己的消息 recent_messages_dict = message_api.get_messages_by_time_in_chat( - chat_id=self.chat_id, start_time=start_time, end_time=current_time + chat_id=self.chat_id, start_time=start_time, end_time=current_time, filter_mai=True ) new_message_count = len(recent_messages_dict) @@ -89,11 +113,20 @@ class NoReplyAction(BaseAction): f"{self.log_prefix} 累计消息数量达到{new_message_count}条(>{exit_message_count_threshold}),结束等待" ) exit_reason = f"{global_config.bot.nickname}(你)看到了{new_message_count}条新消息,可以考虑一下是否要进行回复" + # 如果是私聊,就稍微改一下退出理由 + if not self.is_group: + exit_reason = f"{global_config.bot.nickname}(你)看到了私聊的{new_message_count}条新消息,可以考虑一下是否要进行回复" await self.store_action_info( action_build_into_prompt=False, action_prompt_display=exit_reason, action_done=True, ) + + # 获取最后一条消息 + latest_message = recent_messages_dict[-1] + # 在退出时更新全局字典时间戳(加1微秒防止重复) + _CHAT_START_TIMES[self.chat_id] = latest_message['time'] + 0.000001 # 0.000001秒 = 1微秒 + return True, f"累计消息数量达到{new_message_count}条,结束等待 (等待时间: {elapsed_time:.1f}秒)" # 3. 检查累计兴趣值 @@ -115,6 +148,12 @@ class NoReplyAction(BaseAction): action_prompt_display=exit_reason, action_done=True, ) + + # 获取最后一条消息 + latest_message = recent_messages_dict[-1] + # 在退出时更新全局字典时间戳(加1微秒防止重复) + _CHAT_START_TIMES[self.chat_id] = latest_message['time'] + 0.000001 # 0.000001秒 = 1微秒 + return ( True, f"累计兴趣值达到{accumulated_interest:.2f},结束等待 (等待时间: {elapsed_time:.1f}秒)",