mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
init🎉: 首次提交
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
|
||||
nonebot.load_plugins(str(Path(__file__).parent.resolve()))
|
||||
@@ -0,0 +1,123 @@
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from nonebot import get_loaded_plugins
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot.plugin import Plugin
|
||||
from ruamel import yaml
|
||||
from ruamel.yaml import YAML, round_trip_dump, round_trip_load
|
||||
from ruamel.yaml.comments import CommentedMap
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
from zhenxun.configs.utils import RegisterConfig
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
_yaml = YAML(pure=True)
|
||||
_yaml.allow_unicode = True
|
||||
_yaml.indent = 2
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
SIMPLE_CONFIG_FILE = DATA_PATH / "config.yaml"
|
||||
|
||||
old_config_file = Path() / "zhenxun" / "configs" / "config.yaml"
|
||||
if old_config_file.exists():
|
||||
old_config_file.rename(SIMPLE_CONFIG_FILE)
|
||||
|
||||
|
||||
def _handle_config(plugin: Plugin):
|
||||
"""处理配置项
|
||||
|
||||
参数:
|
||||
plugin: Plugin
|
||||
"""
|
||||
if plugin.metadata and plugin.metadata.extra:
|
||||
extra = plugin.metadata.extra
|
||||
if configs := extra.get("configs"):
|
||||
for config in configs:
|
||||
reg_config = RegisterConfig(**config)
|
||||
module = reg_config.module or plugin.name
|
||||
g_config = Config.get(module)
|
||||
g_config.name = plugin.metadata.name
|
||||
Config.add_plugin_config(
|
||||
module,
|
||||
reg_config.key,
|
||||
reg_config.value,
|
||||
help=reg_config.help,
|
||||
default_value=reg_config.default_value,
|
||||
type=reg_config.type,
|
||||
arg_parser=reg_config.arg_parser,
|
||||
_override=False,
|
||||
)
|
||||
|
||||
|
||||
def _generate_simple_config():
|
||||
"""
|
||||
生成简易配置
|
||||
|
||||
异常:
|
||||
AttributeError: _description_
|
||||
"""
|
||||
# 读取用户配置
|
||||
_data = {}
|
||||
_tmp_data = {}
|
||||
if SIMPLE_CONFIG_FILE.exists():
|
||||
_data = _yaml.load(SIMPLE_CONFIG_FILE.open(encoding="utf8"))
|
||||
# 将简易配置文件的数据填充到配置文件
|
||||
for module in Config.keys():
|
||||
_tmp_data[module] = {}
|
||||
for k in Config[module].configs.keys():
|
||||
try:
|
||||
if _data.get(module) and k in _data[module].keys():
|
||||
Config.set_config(module, k, _data[module][k])
|
||||
_tmp_data[module][k] = Config.get_config(module, k)
|
||||
except AttributeError as e:
|
||||
raise AttributeError(f"{e}\n" + "可能为config.yaml配置文件填写不规范")
|
||||
Config.save()
|
||||
temp_file = DATA_PATH / "temp_config.yaml"
|
||||
# 重新生成简易配置文件
|
||||
try:
|
||||
with open(temp_file, "w", encoding="utf8") as wf:
|
||||
# yaml.dump(_tmp_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
_yaml.dump(_tmp_data, wf)
|
||||
with open(temp_file, "r", encoding="utf8") as rf:
|
||||
_data = _yaml.load(rf)
|
||||
# 添加注释
|
||||
for module in _data.keys():
|
||||
help_text = ""
|
||||
plugin_name = Config.get(module).name or module
|
||||
help_text += plugin_name + "\n"
|
||||
for k in _data[module].keys():
|
||||
help_text += f"{k}: {Config[module].configs[k].help}" + "\n"
|
||||
_data.yaml_set_comment_before_after_key(after=help_text[:-1], key=module)
|
||||
with SIMPLE_CONFIG_FILE.open("w", encoding="utf8") as wf:
|
||||
_yaml.dump(_data, wf)
|
||||
except Exception as e:
|
||||
logger.error(f"生成简易配置注释错误...", e=e)
|
||||
if temp_file.exists():
|
||||
temp_file.unlink()
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
def _():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
"""
|
||||
plugins2config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
||||
for plugin in get_loaded_plugins():
|
||||
if plugin.metadata:
|
||||
_handle_config(plugin)
|
||||
if not Config.is_empty():
|
||||
Config.save()
|
||||
_data: CommentedMap = _yaml.load(plugins2config_file.open(encoding="utf8"))
|
||||
for module in _data.keys():
|
||||
plugin_name = Config.get(module).name
|
||||
_data.yaml_set_comment_before_after_key(
|
||||
after=f"{plugin_name}",
|
||||
key=module,
|
||||
)
|
||||
# 存完插件基本设置
|
||||
with plugins2config_file.open("w", encoding="utf8") as wf:
|
||||
_yaml.dump(_data, wf)
|
||||
_generate_simple_config()
|
||||
@@ -0,0 +1,126 @@
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import nonebot
|
||||
from nonebot import get_loaded_plugins
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot.plugin import Plugin
|
||||
from ruamel import yaml
|
||||
from ruamel.yaml import YAML, round_trip_dump, round_trip_load
|
||||
from ruamel.yaml.comments import CommentedMap
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
from zhenxun.configs.utils import (
|
||||
BaseBlock,
|
||||
PluginExtraData,
|
||||
PluginSetting,
|
||||
RegisterConfig,
|
||||
)
|
||||
from zhenxun.models.plugin_info import PluginInfo
|
||||
from zhenxun.models.plugin_limit import PluginLimit
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginLimitType
|
||||
|
||||
_yaml = YAML(pure=True)
|
||||
_yaml.allow_unicode = True
|
||||
_yaml.indent = 2
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
|
||||
def _handle_setting(
|
||||
plugin: Plugin, plugin_list: List[PluginInfo], limit_list: List[PluginLimit]
|
||||
):
|
||||
"""处理插件设置
|
||||
|
||||
参数:
|
||||
plugin: Plugin
|
||||
plugin_list: 插件列表
|
||||
limit_list: 插件限制列表
|
||||
"""
|
||||
metadata = plugin.metadata
|
||||
if metadata:
|
||||
extra = metadata.extra
|
||||
extra_data = PluginExtraData(**extra)
|
||||
logger.debug(f"{metadata.name}:{plugin.name} -> {extra}", "初始化插件数据")
|
||||
setting = extra_data.setting or PluginSetting()
|
||||
plugin_list.append(
|
||||
PluginInfo(
|
||||
module=plugin.name,
|
||||
module_path=plugin.module_name,
|
||||
name=metadata.name,
|
||||
author=extra_data.author,
|
||||
version=extra_data.version,
|
||||
level=setting.level,
|
||||
default_status=setting.default_status,
|
||||
limit_superuser=setting.limit_superuser,
|
||||
menu_type=extra_data.menu_type,
|
||||
cost_gold=setting.cost_gold,
|
||||
plugin_type=extra_data.plugin_type,
|
||||
admin_level=extra_data.admin_level,
|
||||
)
|
||||
)
|
||||
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),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def _():
|
||||
"""
|
||||
初始化插件数据配置
|
||||
"""
|
||||
plugin_list: List[PluginInfo] = []
|
||||
limit_list: List[PluginLimit] = []
|
||||
module2id = {}
|
||||
if module_list := await PluginInfo.all().values("id", "module_path"):
|
||||
module2id = {m["module_path"]: m["id"] for m in module_list}
|
||||
for plugin in get_loaded_plugins():
|
||||
if plugin.metadata:
|
||||
_handle_setting(plugin, plugin_list, limit_list)
|
||||
create_list = []
|
||||
update_list = []
|
||||
for plugin in plugin_list:
|
||||
if plugin.module_path not in module2id:
|
||||
create_list.append(plugin)
|
||||
else:
|
||||
plugin.id = module2id[plugin.module_path]
|
||||
update_list.append(plugin)
|
||||
if create_list:
|
||||
await PluginInfo.bulk_create(create_list, 10)
|
||||
if update_list:
|
||||
await PluginInfo.bulk_update(
|
||||
update_list,
|
||||
["name", "author", "version", "admin_level"],
|
||||
10,
|
||||
)
|
||||
if limit_list:
|
||||
limit_create = []
|
||||
plugins = []
|
||||
if module_path_list := [limit.module_path for limit in limit_list]:
|
||||
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]
|
||||
limit_type_list = [
|
||||
_limit.limit_type for _limit in await plugin.plugin_limit.all() # type: ignore
|
||||
]
|
||||
if limit.limit_type not in limit_type_list:
|
||||
limit.plugin = plugin
|
||||
limit_create.append(limit)
|
||||
if limit_create:
|
||||
await PluginLimit.bulk_create(limit_create, 10)
|
||||
Reference in New Issue
Block a user