diff --git a/src/chat/focus_chat/expressors/exprssion_learner.py b/src/chat/focus_chat/expressors/exprssion_learner.py index fbe44eb34..8dd54b8a0 100644 --- a/src/chat/focus_chat/expressors/exprssion_learner.py +++ b/src/chat/focus_chat/expressors/exprssion_learner.py @@ -204,7 +204,8 @@ class ExpressionLearner: random_msg: Optional[List[Dict[str, Any]]] = get_raw_msg_by_timestamp_random( current_time - 3600 * 24, current_time, limit=num ) - if not random_msg: + # print(random_msg) + if not random_msg or random_msg == []: return None # 转化成str chat_id: str = random_msg[0]["chat_id"] @@ -216,7 +217,7 @@ class ExpressionLearner: chat_str=random_msg_str, ) - # logger.info(f"学习{type_str}的prompt: {prompt}") + logger.info(f"学习{type_str}的prompt: {prompt}") try: response, _ = await self.express_learn_model.generate_response_async(prompt) diff --git a/src/chat/utils/chat_message_builder.py b/src/chat/utils/chat_message_builder.py index 66b58e257..27eeb5648 100644 --- a/src/chat/utils/chat_message_builder.py +++ b/src/chat/utils/chat_message_builder.py @@ -441,6 +441,7 @@ async def build_anonymous_messages(messages: List[Dict[str, Any]]) -> str: 处理 回复 和 @ 字段,将bbb映射为匿名占位符。 """ if not messages: + print("111111111111没有消息,无法构建匿名消息") return "" person_map = {} @@ -450,7 +451,12 @@ async def build_anonymous_messages(messages: List[Dict[str, Any]]) -> str: def get_anon_name(platform, user_id): if user_id == global_config.bot.qq_account: return "SELF" - person_id = person_info_manager.get_person_id(platform, user_id) + try: + person_id = person_info_manager.get_person_id(platform, user_id) + except Exception as e: + person_id = None + if not person_id: + return "?" if person_id not in person_map: nonlocal current_char person_map[person_id] = chr(current_char) @@ -458,56 +464,74 @@ async def build_anonymous_messages(messages: List[Dict[str, Any]]) -> str: return person_map[person_id] for msg in messages: - user_info = msg.get("user_info", {}) - platform = user_info.get("platform") - user_id = user_info.get("user_id") - timestamp = msg.get("time") - if msg.get("display_message"): - content = msg.get("display_message") - else: - content = msg.get("processed_plain_text", "") + try: + # user_info = msg.get("user_info", {}) + platform = msg.get("chat_info_platform") + user_id = msg.get("chat_info_user_id") + timestamp = msg.get("time") + # print(f"msg:{msg}") + # print(f"platform:{platform}") + # print(f"user_id:{user_id}") + # print(f"timestamp:{timestamp}") + if msg.get("display_message"): + content = msg.get("display_message") + else: + content = msg.get("processed_plain_text", "") - if "ᶠ" in content: - content = content.replace("ᶠ", "") - if "ⁿ" in content: - content = content.replace("ⁿ", "") + if "ᶠ" in content: + content = content.replace("ᶠ", "") + if "ⁿ" in content: + content = content.replace("ⁿ", "") - if not all([platform, user_id, timestamp is not None]): + # if not all([platform, user_id, timestamp is not None]): + # continue + + anon_name = get_anon_name(platform, user_id) + # print(f"anon_name:{anon_name}") + + # 处理 回复 + reply_pattern = r"回复<([^:<>]+):([^:<>]+)>" + match = re.search(reply_pattern, content) + if match: + # print(f"发现回复match:{match}") + bbb = match.group(2) + try: + anon_reply = get_anon_name(platform, bbb) + except Exception: + anon_reply = "?" + content = re.sub(reply_pattern, f"回复 {anon_reply}", content, count=1) + + # 处理 @,无嵌套def + at_pattern = r"@<([^:<>]+):([^:<>]+)>" + at_matches = list(re.finditer(at_pattern, content)) + if at_matches: + # print(f"发现@match:{at_matches}") + new_content = "" + last_end = 0 + for m in at_matches: + new_content += content[last_end:m.start()] + bbb = m.group(2) + try: + anon_at = get_anon_name(platform, bbb) + except Exception: + anon_at = "?" + new_content += f"@{anon_at}" + last_end = m.end() + new_content += content[last_end:] + content = new_content + + header = f"{anon_name}说 " + output_lines.append(header) + stripped_line = content.strip() + if stripped_line: + if stripped_line.endswith("。"): + stripped_line = stripped_line[:-1] + output_lines.append(f"{stripped_line}") + # print(f"output_lines:{output_lines}") + output_lines.append("\n") + except Exception: continue - anon_name = get_anon_name(platform, user_id) - - # 处理 回复 - reply_pattern = r"回复<([^:<>]+):([^:<>]+)>" - - def reply_replacer(match, platform=platform): - # aaa = match.group(1) - bbb = match.group(2) - anon_reply = get_anon_name(platform, bbb) # noqa - return f"回复 {anon_reply}" - - content = re.sub(reply_pattern, reply_replacer, content, count=1) - - # 处理 @ - at_pattern = r"@<([^:<>]+):([^:<>]+)>" - - def at_replacer(match, platform=platform): - # aaa = match.group(1) - bbb = match.group(2) - anon_at = get_anon_name(platform, bbb) # noqa - return f"@{anon_at}" - - content = re.sub(at_pattern, at_replacer, content) - - header = f"{anon_name}说 " - output_lines.append(header) - stripped_line = content.strip() - if stripped_line: - if stripped_line.endswith("。"): - stripped_line = stripped_line[:-1] - output_lines.append(f"{stripped_line}") - output_lines.append("\n") - formatted_string = "".join(output_lines).strip() return formatted_string diff --git a/src/main.py b/src/main.py index f723fb991..4846b9134 100644 --- a/src/main.py +++ b/src/main.py @@ -162,9 +162,10 @@ class MainSystem: """学习并存储表达方式任务""" while True: await asyncio.sleep(global_config.expression.learning_interval) - logger.info("[表达方式学习] 开始学习表达方式...") - await expression_learner.learn_and_store_expression() - logger.info("[表达方式学习] 表达方式学习完成") + if global_config.expression.enable_expression_learning: + logger.info("[表达方式学习] 开始学习表达方式...") + await expression_learner.learn_and_store_expression() + logger.info("[表达方式学习] 表达方式学习完成") # async def print_mood_task(self): # """打印情绪状态""" diff --git a/src/plugins/test_plugin/actions/mute_action.py b/src/plugins/test_plugin/actions/mute_action.py index df112a16d..21a8e057c 100644 --- a/src/plugins/test_plugin/actions/mute_action.py +++ b/src/plugins/test_plugin/actions/mute_action.py @@ -12,8 +12,8 @@ class MuteAction(PluginAction): action_name = "mute_action" action_description = "如果某人违反了公序良俗,或者别人戳你太多,或者某人刷屏,一定要禁言某人,如果你很生气,可以禁言某人,可以自选禁言时长,视严重程度而定。" action_parameters = { - "target": "禁言对象,输入你要禁言的对象的名字,必填", - "duration": "禁言时长,输入你要禁言的时长(秒),单位为秒,必填,必须为数字", + "target": "禁言对象,必填,输入你要禁言的对象的名字", + "duration": "禁言时长,必填,输入你要禁言的时长(秒),单位为秒,必须为数字", "reason": "禁言理由,可选", } action_require = [ @@ -24,8 +24,8 @@ class MuteAction(PluginAction): "当你想回避某个话题时使用", ] default = True # 默认动作,是否手动添加到使用集 - # associated_types = ["command", "text"] - associated_types = ["text"] + associated_types = ["command", "text"] + # associated_types = ["text"] async def process(self) -> Tuple[bool, str]: """处理群聊禁言动作"""