diff --git a/plugins/ai_auto_response/bot_ai.py b/plugins/ai_auto_response/bot_ai.py index 016367f..b6fd46e 100644 --- a/plugins/ai_auto_response/bot_ai.py +++ b/plugins/ai_auto_response/bot_ai.py @@ -47,9 +47,24 @@ class InterventionBot: def is_morning_window(self, timestamp): """检查是否在早晨签到时间窗口""" try: - message_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S").time() + # 处理不同类型的时间戳 + if isinstance(timestamp, float): + message_datetime = datetime.fromtimestamp(timestamp) + message_time = message_datetime.time() + elif isinstance(timestamp, str): + try: + message_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S").time() + except ValueError: + try: + message_time = datetime.fromtimestamp(float(timestamp)).time() + except: + return False + else: + return False + return self.morning_window[0] <= message_time <= self.morning_window[1] - except ValueError: + except Exception as e: + print(f"[早晨窗口检测] 错误: {e}") return False def detect_topic(self, message): @@ -102,31 +117,62 @@ class InterventionBot: def rule_high_reply_rate(self, timestamp, chat_log): """规则8:高回复频率(每分钟消息数超过阈值)""" try: - # 解析当前时间 - current_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") + # 检查timestamp类型并转换 + if isinstance(timestamp, float): + # 如果是浮点数(Unix时间戳),转换为datetime对象 + current_time = datetime.fromtimestamp(timestamp) + elif isinstance(timestamp, str): + # 如果是字符串,尝试解析 + try: + current_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") + except ValueError: + # 如果字符串格式不匹配,尝试将其转换为浮点数再处理 + try: + current_time = datetime.fromtimestamp(float(timestamp)) + except: + # 如果转换失败,使用当前时间 + current_time = datetime.now() + else: + # 其他类型,使用当前时间 + current_time = datetime.now() + window_start = current_time - timedelta(minutes=self.analysis_window_minutes) - + # 计算时间窗口内的消息数 recent_messages = [] for msg in chat_log: try: - msg_time = datetime.strptime(msg["timestamp"], "%Y-%m-%d %H:%M:%S") + # 同样处理消息时间戳 + msg_timestamp = msg.get("timestamp") + if isinstance(msg_timestamp, float): + msg_time = datetime.fromtimestamp(msg_timestamp) + elif isinstance(msg_timestamp, str): + try: + msg_time = datetime.strptime(msg_timestamp, "%Y-%m-%d %H:%M:%S") + except ValueError: + try: + msg_time = datetime.fromtimestamp(float(msg_timestamp)) + except: + continue + else: + continue + if window_start <= msg_time <= current_time: recent_messages.append(msg) except (ValueError, KeyError, TypeError): continue - + # 如果消息太少,不触发 if len(recent_messages) < self.messages_per_minute_threshold: return False - + # 计算消息频率 messages_per_minute = len(recent_messages) / self.analysis_window_minutes - + # 记录日志,便于调试 if messages_per_minute >= self.messages_per_minute_threshold: print(f"[高频率检测] 当前消息频率: {messages_per_minute:.2f}/分钟,阈值: {self.messages_per_minute_threshold}/分钟") - + return messages_per_minute >= self.messages_per_minute_threshold except Exception as e: print(f"[高频率检测] 错误: {e}")