fix: make bot identity platform-aware

This commit is contained in:
晴猫
2026-03-15 07:22:08 +09:00
parent 34ffc56b85
commit 267b42001e
15 changed files with 311 additions and 141 deletions

View File

@@ -1,17 +1,16 @@
import traceback
from datetime import datetime
from typing import Any
import json
import traceback
from sqlalchemy import func
from sqlalchemy import and_, func, not_, or_
from sqlmodel import col, select
from src.common.database.database import get_db_session
from src.common.database.database_model import Messages
from src.chat.message_receive.message import SessionMessage
from src.common.logger import get_logger
from src.config.config import global_config
logger = get_logger(__name__)
@@ -163,7 +162,17 @@ def find_messages(
after_time=after_time,
)
if filter_bot:
conditions.append(Messages.user_id != global_config.bot.qq_account)
from src.chat.utils.utils import get_all_bot_accounts
bot_accounts = get_all_bot_accounts()
if bot_accounts:
bot_identity_predicate = or_(
*[
and_(Messages.platform == platform_name, Messages.user_id == account)
for platform_name, account in bot_accounts.items()
]
)
conditions.append(not_(bot_identity_predicate))
if filter_command:
conditions.append(Messages.is_command == False) # noqa: E712

View File

@@ -1,8 +1,10 @@
# TODO: 这个函数的实现非常临时,后续需要替换为更完善的实现,比如直接从配置文件中读取机器人自己的 ID或者通过 API 获取机器人自己的信息等
def is_bot_self(user_id: str, platform: str) -> bool:
# TODO: 这个兼容包装层后续可以删除,统一直接使用 src.chat.utils.utils.is_bot_self
def is_bot_self(platform: str, user_id: str) -> bool:
"""
判断用户 ID 是否是机器人自己
判断用户 ID 是否是机器人自己
临时方法,后续会替换为更完善的实现
当前仅保留兼容入口,真实实现委托给统一的多平台判断函数。
"""
return user_id == "bot_self" and platform == "test_platform"
from src.chat.utils.utils import is_bot_self as _is_bot_self
return _is_bot_self(platform, user_id)

View File

@@ -367,7 +367,7 @@ class MessageUtils:
anonymous_name = anonymize_mapping[msg_usr_info.user_id][0]
new_message.message_info.user_info.user_nickname = anonymous_name
new_message.message_info.user_info.user_cardname = anonymous_name
if replace_bot_name and target_bot_name and is_bot_self(msg_usr_info.user_id, platform):
if replace_bot_name and target_bot_name and is_bot_self(platform, msg_usr_info.user_id):
new_message.message_info.user_info.user_nickname = target_bot_name
new_message.message_info.user_info.user_cardname = target_bot_name
return new_message
@@ -437,7 +437,7 @@ class MessageUtils:
anonymous_name = anonymize_mapping[user_id][0]
component.target_user_nickname = anonymous_name
component.target_user_cardname = anonymous_name
if replace_bot_name and target_bot_name and is_bot_self(user_id, platform):
if replace_bot_name and target_bot_name and is_bot_self(platform, user_id):
component.target_user_nickname = target_bot_name
component.target_user_cardname = target_bot_name
return component
@@ -473,7 +473,7 @@ class MessageUtils:
anonymous_name = anonymize_mapping[user_id][0]
comp.user_nickname = anonymous_name
comp.user_cardname = anonymous_name
if replace_bot_name and target_bot_name and is_bot_self(user_id, platform):
if replace_bot_name and target_bot_name and is_bot_self(platform, user_id):
comp.user_nickname = target_bot_name
comp.user_cardname = target_bot_name
comp.content = [ # 递归处理转发消息中的组件
@@ -512,7 +512,7 @@ class MessageUtils:
anonymous_name = anonymize_mapping[user_id][0]
component.target_message_sender_nickname = anonymous_name
component.target_message_sender_cardname = anonymous_name
if replace_bot_name and target_bot_name and is_bot_self(user_id, platform):
if replace_bot_name and target_bot_name and is_bot_self(platform, user_id):
component.target_message_sender_nickname = target_bot_name
component.target_message_sender_cardname = target_bot_name
else: