mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
* 添加测试插件 * ✨ feat(auth): 添加缓存机制以优化用户和插件数据查询性能 * 🗑️ chore(help): 删除笨蛋检测插件代码 * ✨ feat(bot): 添加对扩展插件的加载支持 * ✨ feat(auth): 优化权限检查和缓存机制,增加用户和插件数据的并行查询 * ✨ feat(cache): 引入运行时缓存机制,优化用户和插件的ban记录管理 * ✨ feat(auth): 更新is_ban函数文档,添加参数和返回值说明 * ``` feat(auth): 使用内存缓存优化权限验证性能 - 移除数据库查询超时控制,改用 LevelUserMemoryCache、BotMemoryCache 和 PluginLimitMemoryCache 进行缓存查询 - 优化 auth_admin、auth_bot、auth_limit 权限验证逻辑,提升响应速度 - 添加 background 参数支持异步发送权限不足提示消息 - 移除 asyncio 依赖,简化代码结构 fix(auth): 修复限制通知频率控制问题 - 实现限制通知冷却机制,避免重复发送相同限制消息 - 添加 AUTH_LIMIT_NOTICE_CD 配置项,默认值为 2 秒 - 使用 FreqLimiter 控制限制通知发送频率 refactor(models): 增强模型数据变更时的缓存同步 - 在 BotConsole、GroupConsole、LevelUser、PluginLimit 模型的 create、update_or_create、save、delete 方法中自动更新对应缓存 - 确保数据库和内存缓存数据一致性 docs(ban_console): 修正文档注释并优化日志信息 - 修正 BanConsole 类中方法的文档字符串,使用标准参数和返回值格式 - 优化调试日志信息,使描述更加清晰准确 ``` * ✨ feat(auth): 更新Limit类以支持PluginLimitSnapshot,优化限制信息处理 * ✨ feat(bot_manage): 优化Bot控制台初始化逻辑,处理IntegrityError异常 * ✨ feat(mmm1): 新增消息推送功能,支持私聊和群聊事件处理 * ✨ feat(group_member_update): 优化群组成员更新逻辑,增加活动跟踪和消息记录功能 * ✨ feat(mmm1): 删除冗余的消息推送功能代码 * ✨ feat(auth): 优化权限检查逻辑,增加模块阻止功能和缓存处理 * ✨ feat(auth): 优化权限检查逻辑,增加快速ban检测和前置检查功能 * ✨ feat(chat_history): 增强消息处理规则,添加时间间隔限制以防止重复消息 ✨ feat(data_source): 引入异步获取群成员信息的功能,优化用户信息更新逻辑 * ✨ feat(send_queue): 添加异步发送队列以优化API调用和速率限制 * ✨ feat(auth): 添加缓存就绪检查以优化权限处理逻辑 * ✨ feat(plugins): 移除不必要的插件加载以简化插件管理 * ✨ feat(group_console): 优化群组获取逻辑,添加缓存检查以提升性能 ✨ 只接收缓存完成之后时间的消息 * ✨ feat(auth): 添加异步任务管理和超载检测,优化权限处理逻辑 ✨ feat(chat_history): 修改规则函数为异步,提升消息处理效率 ✨ feat(group_handle): 增加安全获取群组信息的异步方法,添加超时处理 ✨ feat(record_request): 引入安全获取群组信息的异步方法,优化群邀请处理 ✨ feat(ban_memory_cache): 增强禁言内存缓存,添加负缓存机制 ✨ feat(message_load): 新增消息负载检测功能,优化任务调度 ✨ feat(scheduler): 在调度器中集成消息压力检测,优化任务执行 ✨ feat(send_queue): 引入异步任务管理,优化发送队列处理 * ✨ feat(auth): 添加对 LevelUserSnapshot 和 BotSnapshot 的支持,优化权限检查逻辑 ✨ 格式化 * ✨ feat(db_context): 增强 get_or_create 方法,处理并发创建冲突并回退查询已存在记录 * ✨ feat(bot_manage): 增强 init_bot_console 方法,处理并发创建冲突并回退查询已存在的 bot 数据 * 🚨 auto fix by pre-commit hooks * ✨ feat(runtime_cache): 优化消息处理逻辑,支持 bytes 和 bytearray 类型的联合判断 * ✨ style(runtime_cache): 格式化代码,优化多行表达式的可读性 * 🚨 auto fix by pre-commit hooks * ✨ refactor: 优化类型注解,改进群组成员更新逻辑和平台处理 ✨ 格式化 * 🚨 auto fix by pre-commit hooks * ✨ refactor: 优化代码格式,增强可读性并修复类型注解 * 🚨 auto fix by pre-commit hooks * ✨ refactor: 更新文档注释,增强is_ban函数的可读性 * ✨ refactor: 优化代码结构,移除冗余函数,增强可读性并改进任务调度逻辑 * ✨ refactor: 调整定时任务时间,优化渲染服务的初始化逻辑,增强代码可读性 --------- Co-authored-by: ATTomatoo <1126160939@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
525 lines
18 KiB
Python
525 lines
18 KiB
Python
from typing import TYPE_CHECKING, Any, ClassVar, cast, overload
|
|
from typing_extensions import Self
|
|
|
|
from tortoise import fields
|
|
from tortoise.backends.base.client import BaseDBAsyncClient
|
|
|
|
from zhenxun.models.plugin_info import PluginInfo
|
|
from zhenxun.models.task_info import TaskInfo
|
|
from zhenxun.services.cache import CacheRoot
|
|
from zhenxun.services.cache.runtime_cache import GroupMemoryCache
|
|
from zhenxun.services.data_access import DataAccess
|
|
from zhenxun.services.db_context import Model
|
|
from zhenxun.utils.enum import CacheType, DbLockType, PluginType
|
|
|
|
if TYPE_CHECKING:
|
|
from zhenxun.services.cache.runtime_cache import GroupSnapshot
|
|
|
|
|
|
def add_disable_marker(name: str) -> str:
|
|
"""添加模块禁用标记符
|
|
|
|
Args:
|
|
name: 模块名称
|
|
|
|
Returns:
|
|
添加了禁用标记的模块名 (前缀'<'和后缀',')
|
|
"""
|
|
return f"<{name},"
|
|
|
|
|
|
@overload
|
|
def convert_module_format(data: str) -> list[str]: ...
|
|
|
|
|
|
@overload
|
|
def convert_module_format(data: list[str]) -> str: ...
|
|
|
|
|
|
def convert_module_format(data: str | list[str]) -> str | list[str]:
|
|
"""
|
|
在 `<aaa,<bbb,<ccc,` 和 `["aaa", "bbb", "ccc"]` (即禁用启用)之间进行相互转换。
|
|
|
|
参数:
|
|
data: 要转换的数据
|
|
|
|
返回:
|
|
str | list[str]: 根据输入类型返回转换后的数据。
|
|
"""
|
|
if isinstance(data, str):
|
|
return [item.strip(",") for item in data.split("<") if item.strip()]
|
|
else:
|
|
return "".join(add_disable_marker(item) for item in data)
|
|
|
|
|
|
class GroupConsole(Model):
|
|
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
|
"""自增id"""
|
|
group_id = fields.CharField(255, description="群组id")
|
|
"""群聊id"""
|
|
channel_id = fields.CharField(255, null=True, description="频道id")
|
|
"""频道id"""
|
|
group_name = fields.TextField(default="", description="群组名称")
|
|
"""群聊名称"""
|
|
max_member_count = fields.IntField(default=0, description="最大人数")
|
|
"""最大人数"""
|
|
member_count = fields.IntField(default=0, description="当前人数")
|
|
"""当前人数"""
|
|
status = fields.BooleanField(default=True, description="群状态")
|
|
"""群状态"""
|
|
level = fields.IntField(default=5, description="群权限")
|
|
"""群权限"""
|
|
is_super = fields.BooleanField(
|
|
default=False, description="超级用户指定,可以使用全局关闭的功能"
|
|
)
|
|
"""超级用户指定群,可以使用全局关闭的功能"""
|
|
group_flag = fields.IntField(default=0, description="群认证标记")
|
|
"""群认证标记"""
|
|
block_plugin = fields.TextField(default="", description="禁用插件")
|
|
"""禁用插件"""
|
|
superuser_block_plugin = fields.TextField(
|
|
default="", description="超级用户禁用插件"
|
|
)
|
|
"""超级用户禁用插件"""
|
|
block_task = fields.TextField(default="", description="禁用被动技能")
|
|
"""禁用被动技能"""
|
|
superuser_block_task = fields.TextField(default="", description="超级用户禁用被动")
|
|
"""超级用户禁用被动"""
|
|
platform = fields.CharField(255, default="qq", description="所属平台")
|
|
"""所属平台"""
|
|
|
|
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
|
table = "group_console"
|
|
table_description = "群组信息表"
|
|
unique_together = ("group_id", "channel_id")
|
|
indexes = [ # noqa: RUF012
|
|
("group_id",)
|
|
]
|
|
|
|
cache_type = CacheType.GROUPS
|
|
"""缓存类型"""
|
|
cache_key_field = ("group_id", "channel_id")
|
|
"""缓存键字段"""
|
|
enable_lock: ClassVar[list[DbLockType]] = [DbLockType.CREATE, DbLockType.UPSERT]
|
|
"""开启锁"""
|
|
|
|
@classmethod
|
|
async def _get_task_modules(cls, *, default_status: bool) -> list[str]:
|
|
"""获取默认禁用的任务模块
|
|
|
|
返回:
|
|
list[str]: 任务模块列表
|
|
"""
|
|
return cast(
|
|
list[str],
|
|
await TaskInfo.filter(default_status=default_status).values_list(
|
|
"module", flat=True
|
|
),
|
|
)
|
|
|
|
@classmethod
|
|
async def _get_plugin_modules(cls, *, default_status: bool) -> list[str]:
|
|
"""获取默认禁用的插件模块
|
|
|
|
返回:
|
|
list[str]: 插件模块列表
|
|
"""
|
|
return cast(
|
|
list[str],
|
|
await PluginInfo.filter(
|
|
plugin_type__in=[PluginType.NORMAL, PluginType.DEPENDANT],
|
|
default_status=default_status,
|
|
).values_list("module", flat=True),
|
|
)
|
|
|
|
@classmethod
|
|
async def _update_cache(cls, instance):
|
|
"""更新缓存
|
|
|
|
参数:
|
|
instance: 需要更新缓存的实例
|
|
"""
|
|
if cache_type := cls.get_cache_type():
|
|
key = cls.get_cache_key(instance)
|
|
if key is not None:
|
|
await CacheRoot.invalidate_cache(cache_type, key)
|
|
|
|
@classmethod
|
|
async def create(
|
|
cls, using_db: BaseDBAsyncClient | None = None, **kwargs: Any
|
|
) -> Self:
|
|
"""覆盖create方法"""
|
|
group = await super().create(using_db=using_db, **kwargs)
|
|
|
|
task_modules = await cls._get_task_modules(default_status=False)
|
|
plugin_modules = await cls._get_plugin_modules(default_status=False)
|
|
|
|
if task_modules or plugin_modules:
|
|
await cls._update_modules(group, task_modules, plugin_modules, using_db)
|
|
|
|
# 更新缓存
|
|
await cls._update_cache(group)
|
|
await GroupMemoryCache.upsert_from_model(group)
|
|
|
|
return group
|
|
|
|
@classmethod
|
|
async def _update_modules(
|
|
cls,
|
|
group: Self,
|
|
task_modules: list[str],
|
|
plugin_modules: list[str],
|
|
using_db: BaseDBAsyncClient | None = None,
|
|
) -> None:
|
|
"""更新模块设置
|
|
|
|
参数:
|
|
group: 群组实例
|
|
task_modules: 任务模块列表
|
|
plugin_modules: 插件模块列表
|
|
using_db: 数据库连接
|
|
"""
|
|
update_fields = []
|
|
|
|
if task_modules:
|
|
group.block_task = convert_module_format(task_modules)
|
|
update_fields.append("block_task")
|
|
|
|
if plugin_modules:
|
|
group.block_plugin = convert_module_format(plugin_modules)
|
|
update_fields.append("block_plugin")
|
|
|
|
if update_fields:
|
|
await group.save(using_db=using_db, update_fields=update_fields)
|
|
|
|
@classmethod
|
|
async def get_or_create(
|
|
cls,
|
|
defaults: dict | None = None,
|
|
using_db: BaseDBAsyncClient | None = None,
|
|
**kwargs: Any,
|
|
) -> tuple[Self, bool]:
|
|
"""覆盖get_or_create方法"""
|
|
group, is_create = await super().get_or_create(
|
|
defaults=defaults, using_db=using_db, **kwargs
|
|
)
|
|
if not is_create:
|
|
return group, is_create
|
|
|
|
task_modules = await cls._get_task_modules(default_status=False)
|
|
plugin_modules = await cls._get_plugin_modules(default_status=False)
|
|
|
|
if task_modules or plugin_modules:
|
|
await cls._update_modules(group, task_modules, plugin_modules, using_db)
|
|
|
|
# 更新缓存
|
|
if is_create:
|
|
await cls._update_cache(group)
|
|
await GroupMemoryCache.upsert_from_model(group)
|
|
|
|
return group, is_create
|
|
|
|
@classmethod
|
|
async def update_or_create(
|
|
cls,
|
|
defaults: dict | None = None,
|
|
using_db: BaseDBAsyncClient | None = None,
|
|
**kwargs: Any,
|
|
) -> tuple[Self, bool]:
|
|
"""覆盖update_or_create方法"""
|
|
group, is_create = await super().update_or_create(
|
|
defaults=defaults, using_db=using_db, **kwargs
|
|
)
|
|
if not is_create:
|
|
return group, is_create
|
|
|
|
task_modules = await cls._get_task_modules(default_status=False)
|
|
plugin_modules = await cls._get_plugin_modules(default_status=False)
|
|
|
|
if task_modules or plugin_modules:
|
|
await cls._update_modules(group, task_modules, plugin_modules, using_db)
|
|
|
|
# 更新缓存
|
|
await cls._update_cache(group)
|
|
await GroupMemoryCache.upsert_from_model(group)
|
|
|
|
return group, is_create
|
|
|
|
async def save(self, *args, **kwargs):
|
|
await super().save(*args, **kwargs)
|
|
await GroupMemoryCache.upsert_from_model(self)
|
|
|
|
async def delete(self, *args, **kwargs):
|
|
group_id = self.group_id
|
|
channel_id = self.channel_id
|
|
await super().delete(*args, **kwargs)
|
|
await GroupMemoryCache.remove(group_id, channel_id)
|
|
|
|
@classmethod
|
|
async def get_group(
|
|
cls,
|
|
group_id: str,
|
|
channel_id: str | None = None,
|
|
clean_duplicates: bool = True,
|
|
) -> "GroupSnapshot | None":
|
|
return GroupMemoryCache.get_if_ready(group_id, channel_id)
|
|
|
|
@classmethod
|
|
async def get_group_db(
|
|
cls,
|
|
group_id: str,
|
|
channel_id: str | None = None,
|
|
clean_duplicates: bool = True,
|
|
) -> Self | None:
|
|
"""获取群组(数据库)"""
|
|
dao = DataAccess(cls)
|
|
if channel_id:
|
|
return await dao.safe_get_or_none(
|
|
group_id=group_id,
|
|
channel_id=channel_id,
|
|
clean_duplicates=clean_duplicates,
|
|
)
|
|
return await dao.safe_get_or_none(
|
|
group_id=group_id,
|
|
channel_id__isnull=True,
|
|
clean_duplicates=clean_duplicates,
|
|
)
|
|
|
|
@classmethod
|
|
async def is_super_group(cls, group_id: str) -> bool:
|
|
group = GroupMemoryCache.get_if_ready(group_id, None)
|
|
return bool(group and group.is_super)
|
|
|
|
@classmethod
|
|
async def is_superuser_block_plugin(cls, group_id: str, module: str) -> bool:
|
|
group = GroupMemoryCache.get_if_ready(group_id, None)
|
|
if not group:
|
|
return False
|
|
return bool(
|
|
group.superuser_block_plugin_set
|
|
and module in group.superuser_block_plugin_set
|
|
)
|
|
|
|
@classmethod
|
|
async def is_block_plugin(cls, group_id: str, module: str) -> bool:
|
|
group = GroupMemoryCache.get_if_ready(group_id, None)
|
|
if not group:
|
|
return False
|
|
if group.block_plugin_set and module in group.block_plugin_set:
|
|
return True
|
|
if (
|
|
group.superuser_block_plugin_set
|
|
and module in group.superuser_block_plugin_set
|
|
):
|
|
return True
|
|
return False
|
|
|
|
@classmethod
|
|
async def set_block_plugin(
|
|
cls,
|
|
group_id: str,
|
|
module: str,
|
|
is_superuser: bool = False,
|
|
platform: str | None = None,
|
|
):
|
|
"""禁用群组插件
|
|
|
|
参数:
|
|
group_id: 群组id
|
|
task: 任务模块
|
|
is_superuser: 是否为超级用户
|
|
platform: 平台
|
|
"""
|
|
group, _ = await cls.get_or_create(
|
|
group_id=group_id, defaults={"platform": platform}
|
|
)
|
|
update_fields = []
|
|
if is_superuser:
|
|
superuser_block_plugin = convert_module_format(group.superuser_block_plugin)
|
|
if module not in superuser_block_plugin:
|
|
superuser_block_plugin.append(module)
|
|
group.superuser_block_plugin = convert_module_format(
|
|
superuser_block_plugin
|
|
)
|
|
update_fields.append("superuser_block_plugin")
|
|
elif add_disable_marker(module) not in group.block_plugin:
|
|
block_plugin = convert_module_format(group.block_plugin)
|
|
block_plugin.append(module)
|
|
group.block_plugin = convert_module_format(block_plugin)
|
|
update_fields.append("block_plugin")
|
|
if update_fields:
|
|
await group.save(update_fields=update_fields)
|
|
|
|
# 更新缓存
|
|
await cls._update_cache(group)
|
|
|
|
@classmethod
|
|
async def set_unblock_plugin(
|
|
cls,
|
|
group_id: str,
|
|
module: str,
|
|
is_superuser: bool = False,
|
|
platform: str | None = None,
|
|
):
|
|
"""禁用群组插件
|
|
|
|
参数:
|
|
group_id: 群组id
|
|
task: 任务模块
|
|
is_superuser: 是否为超级用户
|
|
platform: 平台
|
|
"""
|
|
group, _ = await cls.get_or_create(
|
|
group_id=group_id, defaults={"platform": platform}
|
|
)
|
|
update_fields = []
|
|
if is_superuser:
|
|
superuser_block_plugin = convert_module_format(group.superuser_block_plugin)
|
|
if module in superuser_block_plugin:
|
|
superuser_block_plugin.remove(module)
|
|
group.superuser_block_plugin = convert_module_format(
|
|
superuser_block_plugin
|
|
)
|
|
update_fields.append("superuser_block_plugin")
|
|
elif add_disable_marker(module) in group.block_plugin:
|
|
block_plugin = convert_module_format(group.block_plugin)
|
|
block_plugin.remove(module)
|
|
group.block_plugin = convert_module_format(block_plugin)
|
|
update_fields.append("block_plugin")
|
|
if update_fields:
|
|
await group.save(update_fields=update_fields)
|
|
|
|
# 更新缓存
|
|
await cls._update_cache(group)
|
|
|
|
@classmethod
|
|
async def is_normal_block_plugin(
|
|
cls, group_id: str, module: str, channel_id: str | None = None
|
|
) -> bool:
|
|
group = GroupMemoryCache.get_if_ready(group_id, channel_id)
|
|
if not group:
|
|
return False
|
|
return bool(group.block_plugin_set and module in group.block_plugin_set)
|
|
|
|
@classmethod
|
|
async def is_superuser_block_task(cls, group_id: str, task: str) -> bool:
|
|
group = GroupMemoryCache.get_if_ready(group_id, None)
|
|
if not group:
|
|
return False
|
|
return bool(
|
|
group.superuser_block_task_set and task in group.superuser_block_task_set
|
|
)
|
|
|
|
@classmethod
|
|
async def is_block_task(
|
|
cls, group_id: str, task: str, channel_id: str | None = None
|
|
) -> bool:
|
|
if not channel_id:
|
|
group = GroupMemoryCache.get_if_ready(group_id, None)
|
|
if not group:
|
|
return False
|
|
if group.block_task_set and task in group.block_task_set:
|
|
return True
|
|
if (
|
|
group.superuser_block_task_set
|
|
and task in group.superuser_block_task_set
|
|
):
|
|
return True
|
|
return False
|
|
group = GroupMemoryCache.get_if_ready(group_id, channel_id)
|
|
if group and group.block_task_set and task in group.block_task_set:
|
|
return True
|
|
super_group = GroupMemoryCache.get_if_ready(group_id, None)
|
|
if (
|
|
super_group
|
|
and super_group.superuser_block_task_set
|
|
and task in super_group.superuser_block_task_set
|
|
):
|
|
return True
|
|
return False
|
|
|
|
@classmethod
|
|
async def set_block_task(
|
|
cls,
|
|
group_id: str,
|
|
task: str,
|
|
is_superuser: bool = False,
|
|
platform: str | None = None,
|
|
):
|
|
"""禁用群组插件
|
|
|
|
参数:
|
|
group_id: 群组id
|
|
task: 任务模块
|
|
is_superuser: 是否为超级用户
|
|
platform: 平台
|
|
"""
|
|
group, _ = await cls.get_or_create(
|
|
group_id=group_id, defaults={"platform": platform}
|
|
)
|
|
update_fields = []
|
|
if is_superuser:
|
|
superuser_block_task = convert_module_format(group.superuser_block_task)
|
|
if task not in group.superuser_block_task:
|
|
superuser_block_task.append(task)
|
|
group.superuser_block_task = convert_module_format(superuser_block_task)
|
|
update_fields.append("superuser_block_task")
|
|
elif add_disable_marker(task) not in group.block_task:
|
|
block_task = convert_module_format(group.block_task)
|
|
block_task.append(task)
|
|
group.block_task = convert_module_format(block_task)
|
|
update_fields.append("block_task")
|
|
if update_fields:
|
|
await group.save(update_fields=update_fields)
|
|
|
|
# 更新缓存
|
|
await cls._update_cache(group)
|
|
|
|
@classmethod
|
|
async def set_unblock_task(
|
|
cls,
|
|
group_id: str,
|
|
task: str,
|
|
is_superuser: bool = False,
|
|
platform: str | None = None,
|
|
):
|
|
"""禁用群组插件
|
|
|
|
参数:
|
|
group_id: 群组id
|
|
task: 任务模块
|
|
is_superuser: 是否为超级用户
|
|
platform: 平台
|
|
"""
|
|
group, _ = await cls.get_or_create(
|
|
group_id=group_id, defaults={"platform": platform}
|
|
)
|
|
update_fields = []
|
|
if is_superuser:
|
|
superuser_block_task = convert_module_format(group.superuser_block_task)
|
|
if task in superuser_block_task:
|
|
superuser_block_task.remove(task)
|
|
group.superuser_block_task = convert_module_format(superuser_block_task)
|
|
update_fields.append("superuser_block_task")
|
|
elif add_disable_marker(task) in group.block_task:
|
|
block_task = convert_module_format(group.block_task)
|
|
block_task.remove(task)
|
|
group.block_task = convert_module_format(block_task)
|
|
update_fields.append("block_task")
|
|
if update_fields:
|
|
await group.save(update_fields=update_fields)
|
|
|
|
# 更新缓存
|
|
await cls._update_cache(group)
|
|
|
|
@classmethod
|
|
def _run_script(cls):
|
|
return [
|
|
"ALTER TABLE group_console ADD superuser_block_plugin"
|
|
" character varying(255) NOT NULL DEFAULT '';",
|
|
"ALTER TABLE group_console ADD superuser_block_task"
|
|
" character varying(255) NOT NULL DEFAULT '';",
|
|
"CREATE INDEX idx_group_console_group_id ON group_console(group_id);",
|
|
"CREATE INDEX idx_group_console_group_null_channel ON group_console(group_id) WHERE channel_id IS NULL;", # 单独创建channel为空的索引 # noqa: E501
|
|
]
|