mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
* bugfix:更换数据库初始化超时路径以修复连接超时问题 * 移除部分观测链路 * 细节修改 * 权限检查细节修改2 * bugfix:修复金币懒加载造成插件金币消耗不了的问题 * bugfix:整理鉴权逻辑 * 完善缓存系统 * 优化官端使用 * bugfix:增加官机和频道的前导统一剥离,新增获取群列表自愈 * bugfix:修复导入问题
67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
from collections.abc import Mapping
|
||
from typing import Any
|
||
|
||
from nonebot.adapters import Bot, Message
|
||
|
||
from zhenxun.services.log import logger
|
||
from zhenxun.utils.log_sanitizer import sanitize_for_logging
|
||
from zhenxun.utils.manager.message_manager import MessageManager
|
||
from zhenxun.utils.platform import PlatformUtils
|
||
|
||
LOG_COMMAND = "MessageHook"
|
||
|
||
|
||
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
|
||
|
||
|
||
@Bot.on_called_api
|
||
async def handle_api_result(
|
||
bot: Bot, exception: Exception | None, api: str, data: dict[str, Any], result: Any
|
||
):
|
||
if (
|
||
exception
|
||
or api != "send_msg"
|
||
or PlatformUtils.get_platform_scope(bot) != "qq_client"
|
||
):
|
||
return
|
||
user_id = data.get("user_id")
|
||
message_id = result.get("message_id") if isinstance(result, Mapping) else None
|
||
message: Message = data.get("message", "")
|
||
try:
|
||
if user_id and message_id:
|
||
MessageManager.add(str(user_id), str(message_id))
|
||
logger.debug(
|
||
f"收集消息id,user_id: {user_id}, msg_id: {message_id}", LOG_COMMAND
|
||
)
|
||
except Exception as e:
|
||
logger.warning(
|
||
f"收集消息id发生错误...data: {data}, result: {result}", LOG_COMMAND, e=e
|
||
)
|
||
sanitized_message = sanitize_for_logging(message, context="nonebot_message")
|
||
logger.debug(f"消息发送记录,message: {sanitized_message}")
|