From d3420bd1b34fcb7adb39833601d3f14447013717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E7=8C=AB?= Date: Sun, 15 Mar 2026 08:05:36 +0900 Subject: [PATCH] fix: address second-round review feedback on bot identity PR --- pytests/utils_test/test_bot_identity_utils.py | 11 +++++++++++ src/bw_learner/expression_learner.py | 2 +- src/chat/brain_chat/PFC/message_sender.py | 6 ++++-- src/chat/replyer/group_generator.py | 6 ++++-- src/chat/replyer/private_generator.py | 6 ++++-- src/chat/utils/utils.py | 2 +- src/common/message_repository.py | 4 ++-- src/services/send_service.py | 6 ++++-- 8 files changed, 31 insertions(+), 12 deletions(-) diff --git a/pytests/utils_test/test_bot_identity_utils.py b/pytests/utils_test/test_bot_identity_utils.py index 2ba8f35a..c345174b 100644 --- a/pytests/utils_test/test_bot_identity_utils.py +++ b/pytests/utils_test/test_bot_identity_utils.py @@ -149,6 +149,17 @@ def test_get_all_bot_accounts_includes_runtime_aliases(monkeypatch): } +def test_get_all_bot_accounts_keeps_canonical_qq_identity(monkeypatch): + utils_module, _logger = load_utils_module( + monkeypatch, + qq_account=123456, + platforms=["qq:999999", "webui:888888", "TG:tg_bot"], + ) + + assert utils_module.get_all_bot_accounts()["qq"] == "123456" + assert utils_module.get_all_bot_accounts()["webui"] == "123456" + + def test_unknown_platform_no_longer_falls_back_to_qq(monkeypatch): utils_module, logger = load_utils_module(monkeypatch, qq_account=123456, platforms=[]) diff --git a/src/bw_learner/expression_learner.py b/src/bw_learner/expression_learner.py index 1641c0f0..43e4ee7d 100644 --- a/src/bw_learner/expression_learner.py +++ b/src/bw_learner/expression_learner.py @@ -14,8 +14,8 @@ from src.common.logger import get_logger from src.common.database.database_model import Expression from src.common.database.database import get_db_session from src.common.data_models.expression_data_model import MaiExpression +from src.chat.utils.utils import is_bot_self from src.common.utils.utils_message import MessageUtils -from src.common.utils.system_utils import is_bot_self from .expression_utils import check_expression_suitability, parse_expression_response diff --git a/src/chat/brain_chat/PFC/message_sender.py b/src/chat/brain_chat/PFC/message_sender.py index a927c413..7cb57d96 100644 --- a/src/chat/brain_chat/PFC/message_sender.py +++ b/src/chat/brain_chat/PFC/message_sender.py @@ -44,8 +44,10 @@ class DirectMessageSender: # 获取麦麦的信息 bot_user_id = get_bot_account(chat_stream.platform) if not bot_user_id: - logger.warning(f"[私聊][{self.private_name}]平台 {chat_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号") - bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip() + bot_user_id = get_bot_account("qq") + if not bot_user_id: + logger.error(f"[私聊][{self.private_name}]平台 {chat_stream.platform} 无可用机器人账号,无法发送消息") + raise RuntimeError("机器人账号未配置") bot_user_info = UserInfo( user_id=bot_user_id, user_nickname=global_config.bot.nickname, diff --git a/src/chat/replyer/group_generator.py b/src/chat/replyer/group_generator.py index 3c41ae2f..a2187be7 100644 --- a/src/chat/replyer/group_generator.py +++ b/src/chat/replyer/group_generator.py @@ -1117,8 +1117,10 @@ class DefaultReplyer: """构建单个发送消息""" bot_user_id = get_bot_account(self.chat_stream.platform) if not bot_user_id: - logger.warning(f"平台 {self.chat_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号") - bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip() + bot_user_id = get_bot_account("qq") + if not bot_user_id: + logger.error(f"平台 {self.chat_stream.platform} 无可用机器人账号,无法构建发送消息") + raise RuntimeError("机器人账号未配置") maim_message = MessageBase( message_info=BaseMessageInfo( diff --git a/src/chat/replyer/private_generator.py b/src/chat/replyer/private_generator.py index 008124d4..e3d5165e 100644 --- a/src/chat/replyer/private_generator.py +++ b/src/chat/replyer/private_generator.py @@ -957,8 +957,10 @@ class PrivateReplyer: """构建单个发送消息""" bot_user_id = get_bot_account(self.chat_stream.platform) if not bot_user_id: - logger.warning(f"平台 {self.chat_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号") - bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip() + bot_user_id = get_bot_account("qq") + if not bot_user_id: + logger.error(f"平台 {self.chat_stream.platform} 无可用机器人账号,无法构建发送消息") + raise RuntimeError("机器人账号未配置") maim_message = MessageBase( message_info=BaseMessageInfo( diff --git a/src/chat/utils/utils.py b/src/chat/utils/utils.py index 44eae4c0..07cec0b4 100644 --- a/src/chat/utils/utils.py +++ b/src/chat/utils/utils.py @@ -93,7 +93,7 @@ def get_all_bot_accounts() -> dict[str, str]: bot_accounts["tg"] = telegram_account for platform_name, account in platform_accounts.items(): - if platform_name in {"tg", "telegram"}: + if platform_name in {"tg", "telegram", "qq", "webui"}: continue bot_accounts[platform_name] = account diff --git a/src/common/message_repository.py b/src/common/message_repository.py index 81ae6360..eac5e837 100644 --- a/src/common/message_repository.py +++ b/src/common/message_repository.py @@ -162,7 +162,7 @@ def find_messages( after_time=after_time, ) if filter_bot: - from src.chat.utils.utils import _get_configured_qq_account, get_all_bot_accounts + from src.chat.utils.utils import get_all_bot_accounts, get_bot_account bot_accounts = get_all_bot_accounts() exclusion_conditions: list[Any] = [] @@ -177,7 +177,7 @@ def find_messages( ) # 兼容旧数据:历史机器人消息在所有平台上都使用 QQ 账号进行存储。 - if qq_fallback := _get_configured_qq_account(): + if qq_fallback := get_bot_account("qq"): exclusion_conditions.append(Messages.user_id == qq_fallback) if exclusion_conditions: diff --git a/src/services/send_service.py b/src/services/send_service.py index 6749adfc..e90009f0 100644 --- a/src/services/send_service.py +++ b/src/services/send_service.py @@ -85,8 +85,10 @@ async def _send_to_target( additional_config["selected_expressions"] = selected_expressions bot_user_id = get_bot_account(target_stream.platform) if not bot_user_id: - logger.warning(f"[SendService] 平台 {target_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号") - bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip() + bot_user_id = get_bot_account("qq") + if not bot_user_id: + logger.error(f"[SendService] 平台 {target_stream.platform} 无可用机器人账号,无法发送消息") + return False maim_message = MessageBase( message_info=BaseMessageInfo(