mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-03 10:40:02 +08:00
🎨 修复bug,移除plugins文件夹
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
import nonebot
|
||||
import aiofiles
|
||||
import ujson as json
|
||||
from nonebot import get_loaded_plugins
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot.plugin import Plugin
|
||||
from ruamel.yaml import YAML
|
||||
from nonebot.plugin import Plugin
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot import get_loaded_plugins
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
from zhenxun.configs.utils import PluginExtraData, PluginSetting
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.models.plugin_limit import PluginLimit
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.models.group_console import GroupConsole
|
||||
from zhenxun.configs.utils import PluginSetting, PluginExtraData
|
||||
from zhenxun.utils.enum import (
|
||||
BlockType,
|
||||
PluginType,
|
||||
LimitCheckType,
|
||||
LimitWatchType,
|
||||
PluginLimitType,
|
||||
PluginType,
|
||||
)
|
||||
|
||||
_yaml = YAML(pure=True)
|
||||
@@ -31,7 +32,7 @@ async def _handle_setting(
|
||||
plugin: Plugin,
|
||||
plugin_list: list[PluginInfo],
|
||||
limit_list: list[PluginLimit],
|
||||
task_list: list[TaskInfo],
|
||||
task_list: list[tuple[bool, TaskInfo]],
|
||||
):
|
||||
"""处理插件设置
|
||||
|
||||
@@ -40,8 +41,7 @@ async def _handle_setting(
|
||||
plugin_list: 插件列表
|
||||
limit_list: 插件限制列表
|
||||
"""
|
||||
metadata = plugin.metadata
|
||||
if metadata:
|
||||
if metadata := plugin.metadata:
|
||||
extra = metadata.extra
|
||||
extra_data = PluginExtraData(**extra)
|
||||
logger.debug(f"{metadata.name}:{plugin.name} -> {extra}", "初始化插件数据")
|
||||
@@ -71,30 +71,34 @@ async def _handle_setting(
|
||||
)
|
||||
)
|
||||
if extra_data.limits:
|
||||
for limit in extra_data.limits:
|
||||
limit_list.append(
|
||||
PluginLimit(
|
||||
module=plugin.name,
|
||||
module_path=plugin.module_name,
|
||||
limit_type=limit._type,
|
||||
watch_type=limit.watch_type,
|
||||
status=limit.status,
|
||||
check_type=limit.check_type,
|
||||
result=limit.result,
|
||||
cd=getattr(limit, "cd", None),
|
||||
max_count=getattr(limit, "max_count", None),
|
||||
)
|
||||
limit_list.extend(
|
||||
PluginLimit(
|
||||
module=plugin.name,
|
||||
module_path=plugin.module_name,
|
||||
limit_type=limit._type,
|
||||
watch_type=limit.watch_type,
|
||||
status=limit.status,
|
||||
check_type=limit.check_type,
|
||||
result=limit.result,
|
||||
cd=getattr(limit, "cd", None),
|
||||
max_count=getattr(limit, "max_count", None),
|
||||
)
|
||||
for limit in extra_data.limits
|
||||
)
|
||||
if extra_data.tasks:
|
||||
for task in extra_data.tasks:
|
||||
task_list.append((task.create_status,
|
||||
task_list.extend(
|
||||
(
|
||||
task.create_status,
|
||||
TaskInfo(
|
||||
module=task.module,
|
||||
name=task.name,
|
||||
status=task.status,
|
||||
run_time=task.run_time,
|
||||
)
|
||||
))
|
||||
default_status=task.default_status,
|
||||
),
|
||||
)
|
||||
for task in extra_data.tasks
|
||||
)
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
@@ -104,7 +108,7 @@ async def _():
|
||||
"""
|
||||
plugin_list: list[PluginInfo] = []
|
||||
limit_list: list[PluginLimit] = []
|
||||
task_list: list[TaskInfo] = []
|
||||
task_list = []
|
||||
module2id = {}
|
||||
load_plugin = []
|
||||
if module_list := await PluginInfo.all().values("id", "module_path"):
|
||||
@@ -132,14 +136,15 @@ async def _():
|
||||
update_list.append(plugin)
|
||||
if create_list:
|
||||
await PluginInfo.bulk_create(create_list, 10)
|
||||
if update_list:
|
||||
# TODO: 批量更新无法更新plugin_type: tortoise.exceptions.OperationalError: column "superuser" does not exist
|
||||
pass
|
||||
# await PluginInfo.bulk_update(
|
||||
# update_list,
|
||||
# ["name", "author", "version", "admin_level", "plugin_type"],
|
||||
# 10,
|
||||
# )
|
||||
# if update_list:
|
||||
# # TODO: 批量更新无法更新plugin_type: tortoise.exceptions.OperationalError:
|
||||
# column "superuser" does not exist
|
||||
# pass
|
||||
# await PluginInfo.bulk_update(
|
||||
# update_list,
|
||||
# ["name", "author", "version", "admin_level", "plugin_type"],
|
||||
# 10,
|
||||
# )
|
||||
if limit_list:
|
||||
limit_create = []
|
||||
plugins = []
|
||||
@@ -147,8 +152,8 @@ async def _():
|
||||
plugins = await PluginInfo.filter(module_path__in=module_path_list).all()
|
||||
if plugins:
|
||||
for limit in limit_list:
|
||||
if l := [p for p in plugins if p.module_path == limit.module_path]:
|
||||
plugin = l[0]
|
||||
if lmt := [p for p in plugins if p.module_path == limit.module_path]:
|
||||
plugin = lmt[0]
|
||||
limit_type_list = [
|
||||
_limit.limit_type for _limit in await plugin.plugin_limit.all() # type: ignore
|
||||
]
|
||||
@@ -170,7 +175,7 @@ async def _():
|
||||
task.id = module_dict[task.module]
|
||||
update_list.append(task)
|
||||
if create_list:
|
||||
_create_list = [t[1] for t in create_list]
|
||||
_create_list = [t[1] for t in create_list]
|
||||
await TaskInfo.bulk_create(_create_list, 10)
|
||||
if block := [t[1].module for t in create_list if not t[0]]:
|
||||
block_task = ",".join(block) + ","
|
||||
@@ -202,14 +207,14 @@ async def limit_migration():
|
||||
count_file = DATA_PATH / "configs" / "plugins2count.yaml"
|
||||
limit_data: dict[str, list[tuple[str, dict]]] = {}
|
||||
if cd_file.exists():
|
||||
with open(cd_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(f):
|
||||
async with aiofiles.open(cd_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(await f.read()):
|
||||
for k in data["PluginCdLimit"]:
|
||||
limit_data[k] = [("CD", data["PluginCdLimit"][k])]
|
||||
cd_file.unlink()
|
||||
if block_file.exists():
|
||||
with open(block_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(f):
|
||||
async with aiofiles.open(block_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(await f.read()):
|
||||
for k in data["PluginBlockLimit"]:
|
||||
if k in limit_data:
|
||||
limit_data[k].append(("BLOCK", data["PluginBlockLimit"][k]))
|
||||
@@ -217,8 +222,8 @@ async def limit_migration():
|
||||
limit_data[k] = [("BLOCK", data["PluginBlockLimit"][k])]
|
||||
block_file.unlink()
|
||||
if count_file.exists():
|
||||
with open(count_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(f):
|
||||
async with aiofiles.open(count_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(await f.read()):
|
||||
for k in data["PluginCountLimit"]:
|
||||
if k in limit_data:
|
||||
limit_data[k].append(("COUNT", data["PluginCountLimit"][k]))
|
||||
@@ -294,7 +299,8 @@ async def limit_migration():
|
||||
max_count=_limit.get("max_count"),
|
||||
)
|
||||
)
|
||||
# TODO: 批量错误 tortoise.exceptions.OperationalError: syntax error at or near "ALL"
|
||||
# TODO: 批量错误 tortoise.exceptions.OperationalError:
|
||||
# syntax error at or near "ALL"
|
||||
# if update_list:
|
||||
# await PluginLimit.bulk_update(
|
||||
# update_list,
|
||||
@@ -318,8 +324,8 @@ async def plugin_migration():
|
||||
setting_file = DATA_PATH / "configs" / "plugins2settings.yaml"
|
||||
plugin_file = DATA_PATH / "manager" / "plugins_manager.json"
|
||||
if setting_file.exists():
|
||||
with open(setting_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(f):
|
||||
async with aiofiles.open(setting_file, encoding="utf8") as f:
|
||||
if data := _yaml.load(await f.read()):
|
||||
logger.info("开始迁移插件setting数据...")
|
||||
data = data["PluginSettings"]
|
||||
plugins = await PluginInfo.filter(module__in=data.keys())
|
||||
@@ -349,8 +355,8 @@ async def plugin_migration():
|
||||
setting_file.unlink()
|
||||
logger.info("迁移插件setting数据完成!")
|
||||
if plugin_file.exists():
|
||||
with open(plugin_file, encoding="utf8") as f:
|
||||
if data := json.load(f):
|
||||
async with aiofiles.open(plugin_file, encoding="utf8") as f:
|
||||
if data := json.loads(await f.read()):
|
||||
logger.info("开始迁移插件数据...")
|
||||
plugins = await PluginInfo.filter(module__in=data.keys())
|
||||
for plugin in plugins:
|
||||
@@ -366,7 +372,8 @@ async def plugin_migration():
|
||||
block_type = BlockType.GROUP
|
||||
plugin.block_type = block_type
|
||||
await plugin.save(update_fields=["status", "block_type"])
|
||||
# TODO: tortoise.exceptions.OperationalError: syntax error at or near "ALL"
|
||||
# TODO: tortoise.exceptions.OperationalError: syntax error at
|
||||
# or near "ALL"
|
||||
# await PluginInfo.bulk_update(plugins, ["status", "block_type"], 10)
|
||||
plugin_file.unlink()
|
||||
logger.info("迁移插件数据完成!")
|
||||
@@ -378,22 +385,20 @@ async def group_migration():
|
||||
"""
|
||||
group_file = DATA_PATH / "manager" / "group_manager.json"
|
||||
if group_file.exists():
|
||||
with open(group_file, encoding="utf8") as f:
|
||||
if data := json.load(f):
|
||||
async with aiofiles.open(group_file, encoding="utf8") as f:
|
||||
if data := json.loads(await f.read()):
|
||||
logger.info("开始迁移群组数据...")
|
||||
update_list = []
|
||||
create_list = []
|
||||
white_group = data["white_group"]
|
||||
close_task = data["close_task"]
|
||||
old_group_list: dict = data["group_manager"]
|
||||
if close_task:
|
||||
if close_task := data["close_task"]:
|
||||
"""全局被动关闭"""
|
||||
await TaskInfo.filter(module__in=close_task).update(status=False)
|
||||
group_list = await GroupConsole.filter(
|
||||
group_id__in=old_group_list.keys()
|
||||
)
|
||||
for old_group_id in old_group_list:
|
||||
old_group = old_group_list[old_group_id]
|
||||
for old_group_id, old_group in old_group_list.items():
|
||||
block_plugin = ""
|
||||
block_task = ""
|
||||
status = old_group.get("status", True)
|
||||
|
||||
Reference in New Issue
Block a user