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>
151 lines
4.5 KiB
Python
151 lines
4.5 KiB
Python
import time
|
|
from typing import Literal
|
|
|
|
from nonebot_plugin_session import EventSession
|
|
|
|
from zhenxun.models.ban_console import BanConsole
|
|
from zhenxun.models.level_user import LevelUser
|
|
from zhenxun.services.log import logger
|
|
from zhenxun.utils.image_utils import BuildImage, ImageTemplate
|
|
|
|
|
|
async def call_ban(user_id: str, duration: int = 1):
|
|
"""调用ban
|
|
|
|
参数:
|
|
user_id: 用户id
|
|
"""
|
|
await BanConsole.ban(user_id, None, 9, duration * 60)
|
|
logger.info("被讨厌了,已将用户加入黑名单...", "ban", session=user_id)
|
|
|
|
|
|
class BanManage:
|
|
@classmethod
|
|
async def build_ban_image(
|
|
cls,
|
|
filter_type: Literal["group", "user"] | None,
|
|
user_id: str | None = None,
|
|
group_id: str | None = None,
|
|
) -> BuildImage | None:
|
|
"""构造Ban列表图片
|
|
|
|
参数:
|
|
filter_type: 过滤类型
|
|
user_id: 用户id
|
|
group_id: 群组id
|
|
|
|
返回:
|
|
BuildImage | None: Ban列表图片
|
|
"""
|
|
data_list = None
|
|
query = BanConsole
|
|
if user_id:
|
|
query = query.filter(user_id=user_id)
|
|
elif group_id:
|
|
query = query.filter(group_id=group_id)
|
|
elif filter_type == "user":
|
|
query = query.filter(group_id__isnull=True)
|
|
elif filter_type == "group":
|
|
query = query.filter(user_id__isnull=True)
|
|
data_list = await query.all()
|
|
if not data_list:
|
|
return None
|
|
column_name = [
|
|
"ID",
|
|
"用户ID",
|
|
"群组ID",
|
|
"BAN LEVEL",
|
|
"剩余时长(分钟)",
|
|
"操作员ID",
|
|
]
|
|
row_data = []
|
|
for data in data_list:
|
|
duration = int((data.ban_time + data.duration - time.time()) / 60)
|
|
if data.duration < 0:
|
|
duration = "∞"
|
|
row_data.append(
|
|
[
|
|
data.id,
|
|
data.user_id,
|
|
data.group_id,
|
|
data.ban_level,
|
|
duration,
|
|
data.operator,
|
|
]
|
|
)
|
|
return await ImageTemplate.table_page(
|
|
"Ban / UnBan 列表", "在黑屋中狠狠调教!", column_name, row_data
|
|
)
|
|
|
|
@classmethod
|
|
async def is_ban(cls, user_id: str, group_id: str | None):
|
|
"""判断用户是否被ban
|
|
|
|
参数:
|
|
user_id: 用户id
|
|
|
|
返回:
|
|
bool: 是否被ban
|
|
"""
|
|
return await BanConsole.is_ban(user_id, group_id)
|
|
|
|
@classmethod
|
|
async def unban(
|
|
cls,
|
|
user_id: str | None,
|
|
group_id: str | None,
|
|
session: EventSession,
|
|
idx: int | None = None,
|
|
is_superuser: bool = False,
|
|
) -> tuple[bool, str]:
|
|
"""unban目标用户
|
|
|
|
参数:
|
|
user_id: 用户id
|
|
group_id: 群组id
|
|
session: Session
|
|
idx: 指定id
|
|
is_superuser: 是否为超级用户操作
|
|
|
|
返回:
|
|
tuple[bool, str]: 是否unban成功, 群组/用户id或提示
|
|
"""
|
|
user_level = 9999
|
|
if not is_superuser and user_id and session.id1:
|
|
user_level = await LevelUser.get_user_level(session.id1, group_id)
|
|
if idx:
|
|
ban_data = await BanConsole.get_ban(id=idx)
|
|
if not ban_data:
|
|
return False, "该用户/群组不在黑名单中捏..."
|
|
if ban_data.ban_level > user_level:
|
|
return False, "unBan权限等级不足捏..."
|
|
await ban_data.delete()
|
|
return True, str(ban_data.user_id or ban_data.group_id)
|
|
elif await BanConsole.check_ban_level(user_id, group_id, user_level):
|
|
await BanConsole.unban(user_id, group_id)
|
|
return True, str(group_id)
|
|
return False, "该用户/群组不在黑名单中不足捏..."
|
|
|
|
@classmethod
|
|
async def ban(
|
|
cls,
|
|
user_id: str | None,
|
|
group_id: str | None,
|
|
duration: int,
|
|
session: EventSession,
|
|
is_superuser: bool,
|
|
):
|
|
"""ban掉目标用户
|
|
|
|
参数:
|
|
user_id: 用户id
|
|
group_id: 群组id
|
|
duration: 时长,秒
|
|
session: Session
|
|
is_superuser: 是否为超级用户操作
|
|
"""
|
|
level = 9999
|
|
if not is_superuser and user_id and session.id1:
|
|
level = await LevelUser.get_user_level(session.id1, group_id)
|
|
await BanConsole.ban(user_id, group_id, level, duration, session.id1)
|