diff --git a/plugins/ai_auto_response/bot_ai.py b/plugins/ai_auto_response/bot_ai.py index ce7f565..0e46166 100644 --- a/plugins/ai_auto_response/bot_ai.py +++ b/plugins/ai_auto_response/bot_ai.py @@ -39,10 +39,17 @@ class InterventionBot: self.messages_per_minute_threshold = reply_threshold.get("messages_per_minute_threshold", 3) self.analysis_window_minutes = reply_threshold.get("analysis_window_minutes", 5) + # 冷却时间配置(秒) + self.cooldown_seconds = 20 + self.last_intervention_time = None + + # 最近话题记录 + self.last_topic = None + self.last_topic_time = None + self.topic_cooldown = timedelta(seconds=60) + def is_morning_window(self, timestamp): - """检查是否在早晨签到时间窗口""" try: - # 处理不同类型的时间戳 if isinstance(timestamp, float): message_datetime = datetime.fromtimestamp(timestamp) message_time = message_datetime.time() @@ -56,14 +63,12 @@ class InterventionBot: return False else: return False - return self.morning_window[0] <= message_time <= self.morning_window[1] except Exception as e: print(f"[早晨窗口检测] 错误: {e}") return False def detect_topic(self, message): - """检测消息所属话题类型""" if not isinstance(message, str): return None message_lower = message.lower() @@ -80,64 +85,47 @@ class InterventionBot: return None def rule_morning_signin(self, timestamp, messages): - """规则1:早晨签到""" return self.is_morning_window(timestamp) and any("签到" in msg or "早" in msg for msg in messages[-5:]) def rule_hot_topic(self, message, messages): - """规则2:热点话题参与""" return self.detect_topic(message) == "hot_topic" and len( [m for m in messages[-5:] if self.detect_topic(m) == "hot_topic"]) >= 3 def rule_tech_discussion(self, message, messages): - """规则3:技术性讨论""" return self.detect_topic(message) == "tech" def rule_fish_discussion(self, message, messages): - """规则4:养鱼话题""" return self.detect_topic(message) == "fish" def rule_mechanism_interaction(self, message, messages): - """规则5:群内机制互动""" return self.detect_topic(message) == "mechanism" def rule_humor_tease(self, message, messages): - """规则6:幽默与调侃""" return any(emoji in message for emoji in self.emojis) or "哈哈" in message or len( [m for m in messages[-5:] if any(e in m for e in self.emojis)]) >= 2 def rule_news_reaction(self, message, messages): - """规则7:猎奇或社会新闻反应""" return self.detect_topic(message) == "news" def rule_high_reply_rate(self, timestamp, chat_log): - """规则8:高回复频率(每分钟消息数超过阈值)""" try: - # 检查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_timestamp = msg.get("timestamp") if isinstance(msg_timestamp, float): msg_time = datetime.fromtimestamp(msg_timestamp) @@ -157,14 +145,11 @@ class InterventionBot: 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}/分钟") @@ -174,7 +159,6 @@ class InterventionBot: return False def should_intervene(self, timestamp, message, messages, chat_log): - """判断是否需要介入""" rules = [ self.rule_morning_signin, self.rule_hot_topic, @@ -186,28 +170,50 @@ class InterventionBot: self.rule_high_reply_rate ] + current_time = datetime.now() if not isinstance(timestamp, datetime) else timestamp + if isinstance(timestamp, str): + try: + current_time = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") + except: + current_time = datetime.now() + elif isinstance(timestamp, float): + current_time = datetime.fromtimestamp(timestamp) + + if self.last_intervention_time: + if (current_time - self.last_intervention_time).total_seconds() < self.cooldown_seconds: + return False + + current_topic = self.detect_topic(message) + if self.last_topic == current_topic and self.last_topic_time: + if (current_time - self.last_topic_time) < self.topic_cooldown: + return False + for rule in rules: if rule == self.rule_morning_signin: if rule(timestamp, messages): + self.last_intervention_time = current_time + self.last_topic = current_topic + self.last_topic_time = current_time return True elif rule == self.rule_high_reply_rate: if rule(timestamp, chat_log): + self.last_intervention_time = current_time + self.last_topic = current_topic + self.last_topic_time = current_time return True elif rule(message, messages): + self.last_intervention_time = current_time + self.last_topic = current_topic + self.last_topic_time = current_time return True return False def process_message(self, timestamp, message, messages, chat_log): - """处理单条消息,返回介入状态""" - if self.should_intervene(timestamp, message, messages, chat_log): - return True - return False + return self.should_intervene(timestamp, message, messages, chat_log) def process_chat_log(self, chat_log): - """处理聊天记录,返回每条消息的介入状态""" messages = [line["message"] for line in chat_log] results = [] - for i, line in enumerate(chat_log): timestamp = line["timestamp"] message = line["message"] @@ -217,13 +223,10 @@ class InterventionBot: "message": message, "intervention": intervention }) - return results -# 示例用法 if __name__ == "__main__": - # 模拟聊天记录 sample_chat_log = [ {"timestamp": "2025-03-14 08:06:38", "user_id": "Jyunere", "message": "白嫖马斯克,每个月150刀的额度,应该能玩很久了。"}, {"timestamp": "2025-03-14 08:06:54", "user_id": "Jyunere", "message": "啥情况?卷了?"}, @@ -231,8 +234,7 @@ if __name__ == "__main__": {"timestamp": "2025-03-14 09:12:28", "user_id": "Jyunere", "message": "我同事的鸿蒙确实流畅。"}, {"timestamp": "2025-03-14 09:35:21", "user_id": "Jyunere", "message": "垃圾MIUI"}, {"timestamp": "2025-05-21 14:31:57", "user_id": "wxid_4re8ddo26dxb52", "message": "年轻人随随便便就能深蹲200"}, - {"timestamp": "2025-05-21 14:32:20", "user_id": "liu79830956", - "message": "@水牛 过分了啊,报错还扣积分 赔我200"}, + {"timestamp": "2025-05-21 14:32:20", "user_id": "liu79830956", "message": "@水牛 过分了啊,报错还扣积分 赔我200"}, {"timestamp": "2025-05-21 14:32:39", "user_id": "Jyunere", "message": "哈哈,识别到指令了。"}, {"timestamp": "2025-05-21 14:32:42", "user_id": "wxid_z8uo70zywfpn12", "message": "检测到天 气了"}, {"timestamp": "2025-05-21 14:35:08", "user_id": "liu79830956", "message": "这螺蛳粉估计要明天也吃不上了[旺柴]"}