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>
123 lines
4.2 KiB
Python
123 lines
4.2 KiB
Python
from typing import overload
|
|
|
|
from nonebot.adapters import Bot
|
|
from nonebot_plugin_uninfo import Session, SupportScope, Uninfo, get_interface
|
|
|
|
from zhenxun.configs.config import BotConfig
|
|
from zhenxun.models.ban_console import BanConsole
|
|
from zhenxun.models.bot_console import BotConsole
|
|
from zhenxun.models.group_console import GroupConsole
|
|
from zhenxun.models.task_info import TaskInfo
|
|
from zhenxun.services.log import logger
|
|
|
|
|
|
class CommonUtils:
|
|
@classmethod
|
|
async def task_is_block(
|
|
cls, session: Uninfo | Bot, module: str, group_id: str | None = None
|
|
) -> bool:
|
|
"""判断被动技能是否可以发送
|
|
|
|
参数:
|
|
module: 被动技能模块名
|
|
group_id: 群组id
|
|
|
|
返回:
|
|
bool: 是否可以发送
|
|
"""
|
|
if isinstance(session, Bot):
|
|
if interface := get_interface(session):
|
|
info = interface.basic_info()
|
|
if info["scope"] == SupportScope.qq_api:
|
|
logger.info("q官bot放弃所有被动技能发言...")
|
|
"""q官bot放弃所有被动技能发言"""
|
|
return False
|
|
if session.scene == SupportScope.qq_api:
|
|
"""q官bot放弃所有被动技能发言"""
|
|
logger.info("q官bot放弃所有被动技能发言...")
|
|
return False
|
|
if not group_id and isinstance(session, Session):
|
|
group_id = session.group.id if session.group else None
|
|
if task := await TaskInfo.get_or_none(module=module):
|
|
"""被动全局状态"""
|
|
if not task.status:
|
|
return True
|
|
if not await BotConsole.get_bot_status(session.self_id):
|
|
"""bot是否休眠"""
|
|
return True
|
|
block_tasks = await BotConsole.get_tasks(session.self_id, False)
|
|
if module in block_tasks:
|
|
"""bot是否禁用被动"""
|
|
return True
|
|
if group_id:
|
|
if await GroupConsole.is_block_task(group_id, module):
|
|
"""群组是否禁用被动"""
|
|
return True
|
|
if g := await GroupConsole.get_group(group_id=group_id):
|
|
"""群组权限是否小于0"""
|
|
if g.level < 0:
|
|
return True
|
|
if await BanConsole.is_ban(None, group_id):
|
|
"""群组是否被ban"""
|
|
return True
|
|
return False
|
|
|
|
@staticmethod
|
|
def format(name: str) -> str:
|
|
return f"<{name},"
|
|
|
|
@overload
|
|
@classmethod
|
|
def convert_module_format(cls, data: str) -> list[str]: ...
|
|
|
|
@overload
|
|
@classmethod
|
|
def convert_module_format(cls, data: list[str]) -> str: ...
|
|
|
|
@classmethod
|
|
def convert_module_format(cls, data: str | list[str]) -> str | list[str]:
|
|
"""
|
|
在 `<aaa,<bbb,<ccc,` 和 `["aaa", "bbb", "ccc"]` 之间进行相互转换。
|
|
|
|
参数:
|
|
data (str | list[str]): 输入数据,可能是格式化字符串或字符串列表。
|
|
|
|
返回:
|
|
str | list[str]: 根据输入类型返回转换后的数据。
|
|
"""
|
|
if isinstance(data, str):
|
|
return [item.strip(",") for item in data.split("<") if item]
|
|
elif isinstance(data, list):
|
|
return "".join(cls.format(item) for item in data)
|
|
|
|
|
|
class SqlUtils:
|
|
@classmethod
|
|
def random(cls, query, limit: int = 1) -> str:
|
|
db_class_name = BotConfig.get_sql_type()
|
|
if "postgres" in db_class_name or "sqlite" in db_class_name:
|
|
query = f"{query.sql()} ORDER BY RANDOM() LIMIT {limit};"
|
|
elif "mysql" in db_class_name:
|
|
query = f"{query.sql()} ORDER BY RAND() LIMIT {limit};"
|
|
else:
|
|
logger.warning(
|
|
f"Unsupported database type: {db_class_name}", query.__module__
|
|
)
|
|
return query
|
|
|
|
@classmethod
|
|
def add_column(
|
|
cls,
|
|
table_name: str,
|
|
column_name: str,
|
|
column_type: str,
|
|
default: str | None = None,
|
|
not_null: bool = False,
|
|
) -> str:
|
|
sql = f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type}"
|
|
if default:
|
|
sql += f" DEFAULT {default}"
|
|
if not_null:
|
|
sql += " NOT NULL"
|
|
return sql
|