mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-04 11:20:01 +08:00
添加uv支持 (#2119)
* bugfix:修复内存泄露和信号量饥饿问题 * 修复图片渲染按高度截断问题 * 优化图片渲染速度 * 权限检查去掉无效引用代码 * 添加uv支持 * 🚨 auto fix by pre-commit hooks * bugfix:修改gitignore换行 * bugfix:修复测试没有新生成uv.lock * 修复导入错误 * bugfix:移除重复调用 * 🚨 auto fix by pre-commit hooks * 清理残余poetry引用 * 更新uv安装方式 * 修复阿里云获取问题 * 增加资源下载提示 * 🚨 auto fix by pre-commit hooks * 修改资源下载为流式 * 🚨 auto fix by pre-commit hooks * 提高启动速度 * 移除bot.py支持 * 🚨 auto fix by pre-commit hooks * 优化win脚本逻辑 * 🚨 auto fix by pre-commit hooks * 清理残余无效逻辑 * 代码改进 * 🚨 auto fix by pre-commit hooks * 增加数据库迁移存在性检查 * 🚨 auto fix by pre-commit hooks * chore(test): 添加pytest超时控制和优雅关闭机制 - 在GitHub Actions工作流中添加作业级和步骤级超时限制,防止测试无限期挂起 - 添加pytest-timeout依赖并配置全局超时为120秒 - 在send_queue服务添加关闭钩子,确保worker任务正确取消 - 在priority_manager添加on_shutdown钩子,支持优先级生命周期的关闭阶段 * chore(lint): 禁用超长行的lint警告 * Modify restart logic for Windows platform * 🚨 auto fix by pre-commit hooks * bugfix:修复sys导入问题 * 清理无效结构 * bugfix:修复路径问题 * bugfix:修复shell语法传递给git导致资源获取失败问题 * 优化关闭显示 * bugfix:修复路径问题 * bugfix:增加路径安全 * bugfix:修复orm绕过问题 * 放宽numpy版本限制 * 修改重启方案 * bugfix:修复循环导入 * 优化逻辑 * Enhance disconnect function with error handling Added error handling for disconnect function and imported ConfigurationError. * Implement emergency restart mechanism Added emergency restart mechanism using atexit to ensure process restart even on severe exceptions during shutdown. * 🚨 auto fix by pre-commit hooks * 重启行为归一化 * 修复测试检测问题 * bugfix:修复测试侧类型报错问题 * 引入launcher机制 * 移除重启测试 * 收紧缓存调用路径 * 类型注解收敛 * 优化浏览器回收行为 * 优化浏览器渲染 * bugfix:解决重复关闭浏览器问题 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: ManyManyTomato <93612024+ATTomatoo@users.noreply.github.com> Co-authored-by: AkashiCoin <l1040186796@gmail.com>
This commit is contained in:
co-authored by
pre-commit-ci[bot]
ManyManyTomato
AkashiCoin
parent
74bf912d04
commit
8b16126e40
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Literal
|
||||
from typing import ClassVar, Literal
|
||||
from typing_extensions import Self
|
||||
|
||||
from tortoise import fields
|
||||
@@ -29,6 +29,11 @@ class ChatHistory(Model):
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "chat_history"
|
||||
table_description = "聊天记录数据表"
|
||||
indexes: ClassVar = [
|
||||
("user_id", "create_time"),
|
||||
("group_id", "create_time"),
|
||||
("user_id", "group_id"),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
async def get_group_msg_rank(
|
||||
|
||||
@@ -113,8 +113,9 @@ class GroupConsole(Model):
|
||||
"""
|
||||
return cast(
|
||||
list[str],
|
||||
await TaskInfo.filter(default_status=default_status).values_list(
|
||||
"module", flat=True
|
||||
await TaskInfo.get_modules(
|
||||
default_status=default_status,
|
||||
load_status=None,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -127,10 +128,13 @@ class GroupConsole(Model):
|
||||
"""
|
||||
return cast(
|
||||
list[str],
|
||||
await PluginInfo.filter(
|
||||
await PluginInfo.get_plugins_values_list(
|
||||
"module",
|
||||
load_status=None,
|
||||
filter_parent=False,
|
||||
plugin_type__in=[PluginType.NORMAL, PluginType.DEPENDANT],
|
||||
default_status=default_status,
|
||||
).values_list("module", flat=True),
|
||||
),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -1,140 +0,0 @@
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
class GroupInfo(Model):
|
||||
group_id = fields.CharField(255, pk=True, description="群组id")
|
||||
"""群聊id"""
|
||||
# channel_id = fields.CharField(255, description="群组id")
|
||||
# """频道id"""
|
||||
group_name = fields.TextField(default="", description="群组名称")
|
||||
"""群聊名称"""
|
||||
max_member_count = fields.IntField(default=0, description="最大人数")
|
||||
"""最大人数"""
|
||||
member_count = fields.IntField(default=0, description="当前人数")
|
||||
"""当前人数"""
|
||||
group_flag = fields.IntField(default=0, description="群认证标记")
|
||||
"""群认证标记"""
|
||||
block_plugin = fields.TextField(default="", description="禁用插件")
|
||||
"""禁用插件"""
|
||||
block_task = fields.TextField(default="", description="禁用插件")
|
||||
"""禁用插件"""
|
||||
platform = fields.CharField(255, default="qq", description="所属平台")
|
||||
"""所属平台"""
|
||||
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "group_info"
|
||||
table_description = "群聊信息表"
|
||||
|
||||
@classmethod
|
||||
async def is_block_task(cls, group_id: str, task: str) -> bool:
|
||||
"""查看群组是否禁用被动
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
|
||||
返回:
|
||||
bool: 是否禁用被动
|
||||
"""
|
||||
return await cls.exists(group_id=group_id, block_task__contains=f"{task},")
|
||||
|
||||
@classmethod
|
||||
async def is_block_plugin(cls, group_id: str, module: str) -> bool:
|
||||
"""查看群组是否禁用插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
plugin: 插件名称
|
||||
|
||||
返回:
|
||||
bool: 是否禁用插件
|
||||
"""
|
||||
return await cls.exists(
|
||||
group_id=group_id, block_plugin__contains=f"{module},"
|
||||
) or await cls.exists(
|
||||
group_id=group_id, superuser_block_plugin__contains=f"{module},"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def set_block_plugin(
|
||||
cls,
|
||||
group_id: str,
|
||||
module: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if "module," not in group.superuser_block_plugin: # type: ignore
|
||||
group.superuser_block_plugin += f"{module}," # type: ignore
|
||||
elif "module," not in group.block_plugin:
|
||||
group.block_plugin += f"{module},"
|
||||
await group.save(update_fields=["block_plugin", "superuser_block_plugin"])
|
||||
|
||||
@classmethod
|
||||
async def set_unblock_plugin(
|
||||
cls,
|
||||
group_id: str,
|
||||
module: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if "module," in group.superuser_block_plugin: # type: ignore
|
||||
group.superuser_block_plugin = group.superuser_block_plugin.replace( # type: ignore
|
||||
f"{module},", ""
|
||||
)
|
||||
elif "module," in group.block_plugin:
|
||||
group.block_plugin = group.block_plugin.replace(f"{module},", "")
|
||||
await group.save(update_fields=["block_plugin", "superuser_block_plugin"])
|
||||
|
||||
@classmethod
|
||||
def _run_script(cls):
|
||||
db_type = (BotConfig.get_sql_type() or "").lower()
|
||||
|
||||
scripts = [
|
||||
"ALTER TABLE group_info ADD group_flag Integer NOT NULL DEFAULT 0;",
|
||||
# group_info表添加一个group_flag
|
||||
"ALTER TABLE group_info ALTER COLUMN group_id TYPE character varying(255);",
|
||||
"ALTER TABLE group_info ADD block_plugin Text NOT NULL DEFAULT '';",
|
||||
"ALTER TABLE group_info ADD block_task Text NOT NULL DEFAULT '';",
|
||||
"ALTER TABLE group_info ADD platform character varying(255) NOT NULL"
|
||||
" DEFAULT 'qq';",
|
||||
]
|
||||
|
||||
if "postgres" in db_type:
|
||||
scripts.extend(
|
||||
[
|
||||
"ALTER TABLE group_info ALTER COLUMN block_plugin TYPE TEXT;",
|
||||
"ALTER TABLE group_info ALTER COLUMN block_task TYPE TEXT;",
|
||||
]
|
||||
)
|
||||
elif "mysql" in db_type:
|
||||
scripts.extend(
|
||||
[
|
||||
"ALTER TABLE group_info MODIFY COLUMN block_plugin TEXT;",
|
||||
"ALTER TABLE group_info MODIFY COLUMN block_task TEXT;",
|
||||
]
|
||||
)
|
||||
|
||||
return scripts
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import ClassVar
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.db_context import Model
|
||||
@@ -23,6 +25,7 @@ class GroupInfoUser(Model):
|
||||
table = "group_info_users"
|
||||
table_description = "群员信息数据表"
|
||||
unique_together = ("user_id", "group_id")
|
||||
indexes: ClassVar = [("group_id",), ("user_id",)]
|
||||
|
||||
@classmethod
|
||||
async def get_all_uid(cls, group_id: str) -> set[str]:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing_extensions import Self
|
||||
from typing import ClassVar
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
@@ -63,6 +63,7 @@ class PluginInfo(Model):
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "plugin_info"
|
||||
table_description = "插件基本信息"
|
||||
indexes: ClassVar = [("module",), ("module_path",)]
|
||||
|
||||
cache_type = CacheType.PLUGINS
|
||||
"""缓存类型"""
|
||||
@@ -91,10 +92,58 @@ class PluginInfo(Model):
|
||||
await super().delete(*args, **kwargs)
|
||||
await PluginInfoMemoryCache.remove(module, module_path)
|
||||
|
||||
@staticmethod
|
||||
def _supports_cached_filter(key: str) -> bool:
|
||||
if "__" not in key:
|
||||
return True
|
||||
return key.rsplit("__", 1)[1] in {"in", "not", "not_in"}
|
||||
|
||||
@staticmethod
|
||||
def _match_filter_value(current, operator: str, expected) -> bool:
|
||||
if operator == "in":
|
||||
return current in expected
|
||||
if operator == "not":
|
||||
return current != expected
|
||||
if operator == "not_in":
|
||||
return current not in expected
|
||||
return current == expected
|
||||
|
||||
@classmethod
|
||||
def _can_use_cached_filters(cls, filters: dict) -> bool:
|
||||
return all(cls._supports_cached_filter(key) for key in filters)
|
||||
|
||||
@classmethod
|
||||
async def _get_cached_plugins(cls) -> list["PluginInfo"]:
|
||||
plugins = await PluginInfoMemoryCache.get_all()
|
||||
return sorted(
|
||||
plugins.values(),
|
||||
key=lambda item: (int(getattr(item, "id", 0) or 0), item.module or ""),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _filter_cached_plugins(
|
||||
cls, plugins: list["PluginInfo"], filters: dict
|
||||
) -> list["PluginInfo"]:
|
||||
result: list["PluginInfo"] = []
|
||||
for plugin in plugins:
|
||||
matched = True
|
||||
for key, expected in filters.items():
|
||||
if "__" in key:
|
||||
field, operator = key.rsplit("__", 1)
|
||||
else:
|
||||
field, operator = key, ""
|
||||
current = getattr(plugin, field, None)
|
||||
if not cls._match_filter_value(current, operator, expected):
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
result.append(plugin)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
async def get_plugin(
|
||||
cls, load_status: bool = True, filter_parent: bool = True, **kwargs
|
||||
) -> Self | None:
|
||||
cls, load_status: bool | None = True, filter_parent: bool = True, **kwargs
|
||||
) -> "PluginInfo | None":
|
||||
"""获取插件列表
|
||||
|
||||
参数:
|
||||
@@ -104,16 +153,15 @@ class PluginInfo(Model):
|
||||
返回:
|
||||
Self | None: 插件
|
||||
"""
|
||||
if not kwargs.get("plugin_type") and filter_parent:
|
||||
return await cls.get_or_none(
|
||||
load_status=load_status, plugin_type__not=PluginType.PARENT, **kwargs
|
||||
)
|
||||
return await cls.get_or_none(load_status=load_status, **kwargs)
|
||||
plugins = await cls.get_plugins(
|
||||
load_status=load_status, filter_parent=filter_parent, **kwargs
|
||||
)
|
||||
return plugins[0] if plugins else None
|
||||
|
||||
@classmethod
|
||||
async def get_plugins(
|
||||
cls, load_status: bool = True, filter_parent: bool = True, **kwargs
|
||||
) -> list[Self]:
|
||||
cls, load_status: bool | None = True, filter_parent: bool = True, **kwargs
|
||||
) -> list["PluginInfo"]:
|
||||
"""获取插件列表
|
||||
|
||||
参数:
|
||||
@@ -123,11 +171,38 @@ class PluginInfo(Model):
|
||||
返回:
|
||||
list[Self]: 插件列表
|
||||
"""
|
||||
if not kwargs.get("plugin_type") and filter_parent:
|
||||
return await cls.filter(
|
||||
load_status=load_status, plugin_type__not=PluginType.PARENT, **kwargs
|
||||
).all()
|
||||
return await cls.filter(load_status=load_status, **kwargs).all()
|
||||
filters = dict(kwargs)
|
||||
if load_status is not None:
|
||||
filters.setdefault("load_status", load_status)
|
||||
if filter_parent and not any(key.startswith("plugin_type") for key in filters):
|
||||
filters["plugin_type__not"] = PluginType.PARENT
|
||||
|
||||
if cls._can_use_cached_filters(filters):
|
||||
plugins = await cls._get_cached_plugins()
|
||||
return cls._filter_cached_plugins(plugins, filters)
|
||||
|
||||
return await PluginInfo.filter(**filters).all()
|
||||
|
||||
@classmethod
|
||||
async def get_plugins_values_list(
|
||||
cls,
|
||||
*fields: str,
|
||||
load_status: bool | None = True,
|
||||
filter_parent: bool = True,
|
||||
**kwargs,
|
||||
) -> list:
|
||||
plugins = await cls.get_plugins(
|
||||
load_status=load_status,
|
||||
filter_parent=filter_parent,
|
||||
**kwargs,
|
||||
)
|
||||
if len(fields) == 1:
|
||||
field = fields[0]
|
||||
return [getattr(plugin, field, None) for plugin in plugins]
|
||||
return [
|
||||
tuple(getattr(plugin, field, None) for field in fields)
|
||||
for plugin in plugins
|
||||
]
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import ClassVar
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.cache.runtime_cache import PluginLimitMemoryCache
|
||||
@@ -39,6 +41,7 @@ class PluginLimit(Model):
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "plugin_limit"
|
||||
table_description = "插件限制"
|
||||
indexes: ClassVar = [("module", "status")]
|
||||
|
||||
@classmethod
|
||||
async def create(cls, *args, **kwargs):
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
class SignGroupUser(Model):
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_id = fields.CharField(255)
|
||||
"""用户id"""
|
||||
group_id = fields.CharField(255)
|
||||
"""群聊id"""
|
||||
checkin_count = fields.IntField(default=0)
|
||||
"""签到次数"""
|
||||
checkin_time_last = fields.DatetimeField(default=datetime.min)
|
||||
"""最后签到时间"""
|
||||
impression = fields.DecimalField(10, 3, default=0)
|
||||
"""好感度"""
|
||||
add_probability = fields.DecimalField(10, 3, default=0)
|
||||
"""双倍签到增加概率"""
|
||||
specify_probability = fields.DecimalField(10, 3, default=0)
|
||||
"""使用指定双倍概率"""
|
||||
# specify_probability = fields.DecimalField(10, 3, default=0)
|
||||
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "sign_group_users"
|
||||
table_description = "群员签到数据表"
|
||||
unique_together = ("user_id", "group_id")
|
||||
|
||||
@classmethod
|
||||
async def sign(cls, user: "SignGroupUser", impression: float):
|
||||
"""
|
||||
说明:
|
||||
签到
|
||||
说明:
|
||||
:param user: 用户
|
||||
:param impression: 增加的好感度
|
||||
"""
|
||||
user.checkin_time_last = datetime.now()
|
||||
user.checkin_count = user.checkin_count + 1
|
||||
user.add_probability = 0
|
||||
user.specify_probability = 0
|
||||
user.impression = float(user.impression) + impression
|
||||
await user.save()
|
||||
|
||||
@classmethod
|
||||
async def get_all_impression(
|
||||
cls, group_id: int | str
|
||||
) -> tuple[list[str], list[float], list[str]]:
|
||||
"""
|
||||
说明:
|
||||
获取该群所有用户 id 及对应 好感度
|
||||
参数:
|
||||
:param group_id: 群号
|
||||
"""
|
||||
if group_id:
|
||||
query = cls.filter(group_id=str(group_id))
|
||||
else:
|
||||
query = cls
|
||||
value_list = await query.all().values_list("user_id", "group_id", "impression") # type: ignore
|
||||
user_list = []
|
||||
group_list = []
|
||||
impression_list = []
|
||||
for value in value_list:
|
||||
user_list.append(value[0])
|
||||
group_list.append(value[1])
|
||||
impression_list.append(float(value[2]))
|
||||
return user_list, impression_list, group_list
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
# 将user_id改为user_id
|
||||
"ALTER TABLE sign_group_users RENAME COLUMN user_qq TO user_id;",
|
||||
"ALTER TABLE sign_group_users "
|
||||
"ALTER COLUMN user_id TYPE character varying(255);",
|
||||
# 将user_id字段类型改为character varying(255)
|
||||
"ALTER TABLE sign_group_users "
|
||||
"ALTER COLUMN group_id TYPE character varying(255);",
|
||||
]
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import ClassVar
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.db_context import Model
|
||||
@@ -20,6 +22,12 @@ class Statistics(Model):
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "statistics"
|
||||
table_description = "插件调用统计数据库"
|
||||
indexes: ClassVar = [
|
||||
("user_id", "plugin_name"),
|
||||
("group_id", "plugin_name"),
|
||||
("plugin_name", "create_time"),
|
||||
("user_id", "create_time"),
|
||||
]
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from typing import ClassVar
|
||||
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.cache.runtime_cache import TaskInfoMemoryCache
|
||||
from zhenxun.services.cache.runtime_cache import TaskInfoMemoryCache, TaskInfoSnapshot
|
||||
from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
@@ -25,6 +27,7 @@ class TaskInfo(Model):
|
||||
class Meta: # pyright: ignore [reportIncompatibleVariableOverride]
|
||||
table = "task_info"
|
||||
table_description = "被动技能基本信息"
|
||||
indexes: ClassVar = [("module",)]
|
||||
|
||||
@classmethod
|
||||
async def create(cls, *args, **kwargs):
|
||||
@@ -47,6 +50,55 @@ class TaskInfo(Model):
|
||||
await super().delete(*args, **kwargs)
|
||||
await TaskInfoMemoryCache.remove(module)
|
||||
|
||||
@classmethod
|
||||
async def get_task(
|
||||
cls, *, module: str | None = None, name: str | None = None
|
||||
) -> TaskInfoSnapshot | None:
|
||||
if module:
|
||||
return await TaskInfoMemoryCache.get(module)
|
||||
if name:
|
||||
return await TaskInfoMemoryCache.get_by_name(name)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
async def get_tasks(
|
||||
cls,
|
||||
*,
|
||||
status: bool | None = None,
|
||||
load_status: bool | None = None,
|
||||
default_status: bool | None = None,
|
||||
modules: list[str] | None = None,
|
||||
) -> list[TaskInfoSnapshot]:
|
||||
tasks = await TaskInfoMemoryCache.get_all()
|
||||
module_set = set(modules) if modules else None
|
||||
result: list[TaskInfoSnapshot] = []
|
||||
for task in tasks:
|
||||
if status is not None and task.status != status:
|
||||
continue
|
||||
if load_status is not None and task.load_status != load_status:
|
||||
continue
|
||||
if default_status is not None and task.default_status != default_status:
|
||||
continue
|
||||
if module_set is not None and task.module not in module_set:
|
||||
continue
|
||||
result.append(task)
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
async def get_modules(
|
||||
cls,
|
||||
*,
|
||||
status: bool | None = None,
|
||||
load_status: bool | None = None,
|
||||
default_status: bool | None = None,
|
||||
) -> list[str]:
|
||||
tasks = await cls.get_tasks(
|
||||
status=status,
|
||||
load_status=load_status,
|
||||
default_status=default_status,
|
||||
)
|
||||
return [task.module for task in tasks]
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user