fix(chat): 修复睡眠模式下无法通过艾特唤醒的问题

在之前的实现中,当机器人处于睡眠模式时,获取消息的逻辑会过滤掉所有命令消息(`filter_command=True`),这其中也包括了艾特(@)机器人的消息。这导致了用户无法通过艾特来唤醒睡眠中的机器人。

本次修复通过引入一个 `filter_command_flag` 标志来解决此问题。当机器人处于睡眠状态时(`is_sleeping` 为 `True`),该标志会设置为 `False`,从而在获取消息时不过滤命令消息,确保艾特消息能够被正确接收和处理,进而触发唤醒逻辑。

此外,还对 `schedule_manager` 中的睡眠日志记录逻辑进行了优化,增加了时间间隔来避免在睡眠期间频繁打印日志,减少不必要的日志刷屏。
This commit is contained in:
tt-P607
2025-08-23 02:49:28 +08:00
parent 32cceb0167
commit ab48324c6f
2 changed files with 16 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import asyncio
import time
import traceback
import re
from typing import Optional
from src.common.logger import get_logger
@@ -197,6 +198,9 @@ class HeartFChatting:
"""
is_sleeping = schedule_manager.is_sleeping(self.wakeup_manager)
# 核心修复:在睡眠模式下获取消息时,不过滤命令消息,以确保@消息能被接收
filter_command_flag = not is_sleeping
recent_messages = message_api.get_messages_by_time_in_chat(
chat_id=self.context.stream_id,
start_time=self.context.last_read_time,
@@ -204,7 +208,7 @@ class HeartFChatting:
limit=10,
limit_mode="latest",
filter_mai=True,
filter_command=True,
filter_command=filter_command_flag,
)
has_new_messages = bool(recent_messages)
@@ -321,19 +325,10 @@ class HeartFChatting:
# 检查群聊消息是否艾特了机器人
if not is_private_chat:
# 检查消息中是否包含艾特信息
message_content = message.get("processed_plain_text", "")
bot_name = global_config.bot.nickname
alias_names = global_config.bot.alias_names or []
# 检查是否被艾特(简单的文本匹配)
if f"@{bot_name}" in message_content:
# 最终修复:直接使用消息对象中由上游处理好的 is_mention 字段。
# 该字段在 message.py 的 MessageRecv._process_single_segment 中被设置。
if message.get("is_mentioned"):
is_mentioned = True
else:
for alias in alias_names:
if f"@{alias}" in message_content:
is_mentioned = True
break
# 累积唤醒度
woke_up = self.wakeup_manager.add_wakeup_value(is_private_chat, is_mentioned)

View File

@@ -122,6 +122,8 @@ class ScheduleManager:
self.llm = LLMRequest(model_set=model_config.model_task_config.schedule_generator, request_type="schedule")
self.max_retries = 3 # 最大重试次数
self.daily_task_started = False
self.last_sleep_log_time = 0
self.sleep_log_interval = 35 # 日志记录间隔,单位秒
async def start_daily_schedule_generation(self):
"""启动每日零点自动生成新日程的任务"""
@@ -376,7 +378,12 @@ class ScheduleManager:
logger.info(f"在休眠活动 '{activity}' 期间,但已被唤醒。")
return False
logger.info(f"当前处于休眠活动 '{activity}' 中。")
current_timestamp = datetime.now().timestamp()
if current_timestamp - self.last_sleep_log_time > self.sleep_log_interval:
logger.info(f"当前处于休眠活动 '{activity}' 中。")
self.last_sleep_log_time = current_timestamp
else:
logger.debug(f"当前处于休眠活动 '{activity}' 中。")
return True # 找到匹配的休眠活动直接返回True
except (ValueError, KeyError, AttributeError) as e: