2026-06-07 18:14:01 +08:00
|
|
|
|
from collections.abc import Mapping
|
2024-11-05 15:34:45 +08:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
2025-06-16 09:21:41 +08:00
|
|
|
|
from nonebot.adapters import Bot, Message
|
2024-11-05 15:34:45 +08:00
|
|
|
|
|
|
|
|
|
|
from zhenxun.services.log import logger
|
2025-10-01 18:41:46 +08:00
|
|
|
|
from zhenxun.utils.log_sanitizer import sanitize_for_logging
|
2024-11-05 15:34:45 +08:00
|
|
|
|
from zhenxun.utils.manager.message_manager import MessageManager
|
2025-06-16 09:21:41 +08:00
|
|
|
|
from zhenxun.utils.platform import PlatformUtils
|
|
|
|
|
|
|
2025-07-14 22:35:29 +08:00
|
|
|
|
LOG_COMMAND = "MessageHook"
|
|
|
|
|
|
|
2025-06-16 09:21:41 +08:00
|
|
|
|
|
|
|
|
|
|
def replace_message(message: Message) -> str:
|
|
|
|
|
|
"""将消息中的at、image、record、face替换为字符串
|
|
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
|
message: Message
|
|
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
|
str: 文本消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
result = ""
|
|
|
|
|
|
for msg in message:
|
|
|
|
|
|
if isinstance(msg, str):
|
|
|
|
|
|
result += msg
|
|
|
|
|
|
elif msg.type == "at":
|
|
|
|
|
|
result += f"@{msg.data['qq']}"
|
|
|
|
|
|
elif msg.type == "image":
|
|
|
|
|
|
result += "[image]"
|
|
|
|
|
|
elif msg.type == "record":
|
|
|
|
|
|
result += "[record]"
|
|
|
|
|
|
elif msg.type == "face":
|
|
|
|
|
|
result += f"[face:{msg.data['id']}]"
|
|
|
|
|
|
elif msg.type == "reply":
|
|
|
|
|
|
result += ""
|
|
|
|
|
|
else:
|
|
|
|
|
|
result += str(msg)
|
|
|
|
|
|
return result
|
2024-11-05 15:34:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bot.on_called_api
|
|
|
|
|
|
async def handle_api_result(
|
|
|
|
|
|
bot: Bot, exception: Exception | None, api: str, data: dict[str, Any], result: Any
|
|
|
|
|
|
):
|
2026-06-07 18:14:01 +08:00
|
|
|
|
if (
|
|
|
|
|
|
exception
|
|
|
|
|
|
or api != "send_msg"
|
|
|
|
|
|
or PlatformUtils.get_platform_scope(bot) != "qq_client"
|
|
|
|
|
|
):
|
2025-06-16 09:21:41 +08:00
|
|
|
|
return
|
|
|
|
|
|
user_id = data.get("user_id")
|
2026-06-07 18:14:01 +08:00
|
|
|
|
message_id = result.get("message_id") if isinstance(result, Mapping) else None
|
2025-06-16 09:21:41 +08:00
|
|
|
|
message: Message = data.get("message", "")
|
|
|
|
|
|
try:
|
|
|
|
|
|
if user_id and message_id:
|
|
|
|
|
|
MessageManager.add(str(user_id), str(message_id))
|
|
|
|
|
|
logger.debug(
|
2025-07-14 22:35:29 +08:00
|
|
|
|
f"收集消息id,user_id: {user_id}, msg_id: {message_id}", LOG_COMMAND
|
2024-11-05 15:34:45 +08:00
|
|
|
|
)
|
2025-06-16 09:21:41 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.warning(
|
2025-07-14 22:35:29 +08:00
|
|
|
|
f"收集消息id发生错误...data: {data}, result: {result}", LOG_COMMAND, e=e
|
2025-06-16 09:21:41 +08:00
|
|
|
|
)
|
2026-06-07 18:14:01 +08:00
|
|
|
|
sanitized_message = sanitize_for_logging(message, context="nonebot_message")
|
|
|
|
|
|
logger.debug(f"消息发送记录,message: {sanitized_message}")
|