mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
* 添加全局cache * ✨ 构建缓存,hook使用缓存 * ✨ 新增数据库Model方法监控 * ✨ 数据库添加semaphore锁 * 🩹 优化webapi返回数据 * ✨ 添加增量缓存与缓存过期 * 🎨 优化检测代码结构 * ⚡ 优化hook权限检测性能 * 🐛 添加新异常判断跳过权限检测 * ✨ 添加插件limit缓存 * 🎨 代码格式优化 * 🐛 修复代码导入 * 🐛 修复刷新时检查 * 👽 Rename exception for missing database URL in initialization * ♿ Update default database URL to SQLite in configuration * 🔧 Update tortoise-orm and aiocache dependencies restrictions; add optional redis and asyncpg support * 🐛 修复ban检测 * 🐛 修复所有插件关闭时缓存更新 * 🐛 尝试迁移至aiocache * 🐛 完善aiocache缓存 * ⚡ 代码性能优化 * 🐛 移除获取封禁缓存时的日志记录 * 🐛 修复缓存类型声明,优化封禁用户处理逻辑 * 🐛 优化LevelUser权限更新逻辑及数据库迁移 * ✨ cache支持redis连接 * 🚨 auto fix by pre-commit hooks * ⚡ :增强获取群组的安全性和准确性。同时,优化了缓存管理中的相关逻辑,确保缓存操作的一致性。 * ✨ feat(auth_limit): 将插件初始化逻辑的启动装饰器更改为优先级管理器 * 🔧 修复日志记录级别 * 🔧 更新数据库连接字符串 * 🔧 更新数据库连接字符串为内存数据库,并优化权限检查逻辑 * ✨ feat(cache): 增加缓存功能配置项,并新增数据访问层以支持缓存逻辑 * ♻️ 重构cache * ✨ feat(cache): 增强缓存管理,新增缓存字典和缓存列表功能,支持过期时间管理 * 🔧 修复Notebook类中的viewport高度设置,将其从1000调整为10 * ✨ 更新插件管理逻辑,替换缓存服务为CacheRoot并优化缓存失效处理 * ✨ 更新RegisterConfig类中的type字段 * ✨ 修复清理重复记录逻辑,确保检查记录的id属性有效性 * ⚡ 超级无敌大优化,解决延迟与卡死问题 * ✨ 更新封禁功能,增加封禁时长参数和描述,优化插件信息返回结构 * ✨ 更新zhenxun_help.py中的viewport高度,将其从453调整为10,以优化页面显示效果 * ✨ 优化插件分类逻辑,增加插件ID排序,并更新插件信息返回结构 --------- Co-authored-by: BalconyJH <balconyjh@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
from typing import Any
|
||
|
||
from nonebot.adapters import Bot, Message
|
||
|
||
from zhenxun.configs.config import Config
|
||
from zhenxun.models.bot_message_store import BotMessageStore
|
||
from zhenxun.services.log import logger
|
||
from zhenxun.utils.enum import BotSentType
|
||
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":
|
||
return
|
||
user_id = data.get("user_id")
|
||
group_id = data.get("group_id")
|
||
message_id = result.get("message_id")
|
||
message: Message = data.get("message", "")
|
||
message_type = data.get("message_type")
|
||
try:
|
||
# 记录消息id
|
||
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
|
||
)
|
||
if not Config.get_config("hook", "RECORD_BOT_SENT_MESSAGES"):
|
||
return
|
||
try:
|
||
await BotMessageStore.create(
|
||
bot_id=bot.self_id,
|
||
user_id=user_id,
|
||
group_id=group_id,
|
||
sent_type=BotSentType.GROUP
|
||
if message_type == "group"
|
||
else BotSentType.PRIVATE,
|
||
text=replace_message(message),
|
||
plain_text=message.extract_plain_text()
|
||
if isinstance(message, Message)
|
||
else replace_message(message),
|
||
platform=PlatformUtils.get_platform(bot),
|
||
)
|
||
logger.debug(f"消息发送记录,message: {message}")
|
||
except Exception as e:
|
||
logger.warning(
|
||
f"消息发送记录发生错误...data: {data}, result: {result}",
|
||
LOG_COMMAND,
|
||
e=e,
|
||
)
|