mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
优化代码
This commit is contained in:
@@ -77,10 +77,8 @@ async def _create_help_img(
|
||||
plugins2settings_manager.get_plugin_data(matcher.plugin_name).plugin_type
|
||||
)
|
||||
else:
|
||||
try:
|
||||
if hasattr(_module, "__plugin_type__"):
|
||||
plugin_type = _module.__getattribute__("__plugin_type__")
|
||||
except AttributeError:
|
||||
pass
|
||||
if len(plugin_type) > 1:
|
||||
try:
|
||||
text_type = int(plugin_type[1])
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
from typing import List, Tuple
|
||||
from typing import List, Tuple, Dict, Optional, Union
|
||||
from configs.path_config import IMAGE_PATH
|
||||
from utils.image_utils import BuildImage
|
||||
from pydantic import BaseModel
|
||||
import os
|
||||
import random
|
||||
|
||||
|
||||
background_path = IMAGE_PATH / "background" / "help" / "simple_help"
|
||||
|
||||
# class PluginType(BaseModel):
|
||||
#
|
||||
#
|
||||
# class Plugin(BaseModel):
|
||||
# name: str
|
||||
# plugin_type: PluginType # 插件内部类型,根据name [Hidden] [Admin] [SUPERUSER]
|
||||
# usage: Optional[str]
|
||||
# des: Optional[str]
|
||||
# cmd: Optional[List[str]]
|
||||
# task: Optional[Dict[str, str]]
|
||||
# type: Optional[Tuple[str, int]] # 菜单类型
|
||||
# version: Optional[Union[str, int]]
|
||||
# author: Optional[str]
|
||||
|
||||
|
||||
|
||||
|
||||
async def build_help_image(image_group: List[List[BuildImage]], h: int):
|
||||
bk = None
|
||||
@@ -103,3 +119,10 @@ def group_image(image_list: List[BuildImage]) -> Tuple[List[List[BuildImage]], i
|
||||
max_w -= max([x.w for x in image_group[-1]])
|
||||
image_group.pop(-1)
|
||||
return image_group, max(max_h + 250, max_w + 70)
|
||||
|
||||
|
||||
# class HelpImageBuild:
|
||||
#
|
||||
# def __init__(self):
|
||||
# self._data: Dict[str, List[]] = {}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ from .init_plugins_data import init_plugins_data, plugins_manager
|
||||
from .init_none_plugin_count_manager import init_none_plugin_count_manager
|
||||
from .init_plugins_resources import init_plugins_resources
|
||||
from .init_plugins_settings import init_plugins_settings
|
||||
from .init_plugin_info import init_plugin_info
|
||||
from .init_plugins_limit import (
|
||||
init_plugins_block_limit,
|
||||
init_plugins_count_limit,
|
||||
@@ -34,6 +35,7 @@ async def _():
|
||||
config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
||||
_flag = not config_file.exists()
|
||||
init()
|
||||
init_plugin_info()
|
||||
init_plugins_settings()
|
||||
init_plugins_cd_limit()
|
||||
init_plugins_block_limit()
|
||||
|
||||
@@ -48,7 +48,7 @@ def init_none_plugin_count_manager():
|
||||
logger.info(
|
||||
f"{module}:{plugin_name} 插件疑似已删除,"
|
||||
f"加载{none_plugin_count_manager._max_count}次失败后将清除对应插件数据,"
|
||||
f"当前次数:{none_plugin_count_manager._data[module]}"
|
||||
f"当前次数:{none_plugin_count_manager.get(module)}"
|
||||
)
|
||||
else:
|
||||
none_plugin_count_manager.reset(module)
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
from types import ModuleType
|
||||
from typing import Any
|
||||
|
||||
from services import logger
|
||||
from utils.manager import (
|
||||
plugin_data_manager,
|
||||
plugins2settings_manager,
|
||||
plugins2cd_manager,
|
||||
plugins2block_manager,
|
||||
plugins2count_manager,
|
||||
)
|
||||
from configs.config import Config
|
||||
from utils.manager.models import (
|
||||
PluginType,
|
||||
PluginSetting,
|
||||
PluginCd,
|
||||
PluginData,
|
||||
PluginBlock,
|
||||
PluginCount,
|
||||
)
|
||||
from utils.utils import get_matchers
|
||||
|
||||
|
||||
def get_attr(module: ModuleType, name: str, default: Any = None) -> Any:
|
||||
"""
|
||||
说明:
|
||||
获取属性
|
||||
参数:
|
||||
:param module: module
|
||||
:param name: name
|
||||
:param default: default
|
||||
"""
|
||||
if hasattr(module, name):
|
||||
return getattr(module, name)
|
||||
return default
|
||||
|
||||
|
||||
def init_plugin_info():
|
||||
|
||||
for matcher in [x for x in get_matchers(True) if x.plugin]:
|
||||
try:
|
||||
plugin = matcher.plugin
|
||||
metadata = plugin.metadata
|
||||
if hasattr(plugin, "module"):
|
||||
module = plugin.module
|
||||
plugin_model = matcher.plugin_name
|
||||
plugin_name = (
|
||||
metadata.name
|
||||
if metadata and metadata.name
|
||||
else get_attr(module, "__zx_plugin_name__", matcher.plugin_name)
|
||||
)
|
||||
if "[Admin]" in plugin_name:
|
||||
plugin_type = PluginType.ADMIN
|
||||
plugin_name = plugin_name.replace("[Admin]", "")
|
||||
elif "[Hidden]" in plugin_name:
|
||||
plugin_type = PluginType.HIDDEN
|
||||
plugin_name = plugin_name.replace("[Hidden]", "")
|
||||
elif "[Superuser]" in plugin_name:
|
||||
plugin_type = PluginType.SUPERUSER
|
||||
plugin_name = plugin_name.replace("[Superuser]", "")
|
||||
else:
|
||||
plugin_type = PluginType.NORMAL
|
||||
plugin_usage = (
|
||||
metadata.usage
|
||||
if metadata and metadata.usage
|
||||
else get_attr(module, "__plugin_usage__")
|
||||
)
|
||||
plugin_des = (
|
||||
metadata.description
|
||||
if metadata and metadata.description
|
||||
else get_attr(module, "__plugin_des__")
|
||||
)
|
||||
menu_type = get_attr(module, "__plugin_type__") or ("normal",)
|
||||
plugin_setting = get_attr(module, "__plugin_settings__")
|
||||
if plugin_setting:
|
||||
plugin_setting = PluginSetting(**plugin_setting)
|
||||
plugin_setting.plugin_type = menu_type
|
||||
plugin_task = get_attr(module, "__plugin_task__")
|
||||
plugin_version = get_attr(module, "__plugin_version__")
|
||||
plugin_author = get_attr(module, "__plugin_author__")
|
||||
plugin_cd = get_attr(module, "__plugin_cd_limit__")
|
||||
if plugin_cd:
|
||||
plugin_cd = PluginCd(**plugin_cd)
|
||||
plugin_block = get_attr(module, "__plugin_block_limit__")
|
||||
if plugin_block:
|
||||
plugin_block = PluginBlock(**plugin_block)
|
||||
plugin_count = get_attr(module, "__plugin_count_limit__")
|
||||
if plugin_count:
|
||||
plugin_count = PluginCount(**plugin_count)
|
||||
plugin_resources = get_attr(module, "__plugin_resources__")
|
||||
plugin_configs = get_attr(module, "__plugin_configs__")
|
||||
if settings := plugins2settings_manager.get(plugin_model):
|
||||
plugin_setting = settings
|
||||
if plugin_cd_limit := plugins2cd_manager.get(plugin_model):
|
||||
plugin_cd = plugin_cd_limit
|
||||
if plugin_block_limit := plugins2block_manager.get(plugin_model):
|
||||
plugin_block = plugin_block_limit
|
||||
if plugin_count_limit := plugins2count_manager.get(plugin_model):
|
||||
plugin_count = plugin_count_limit
|
||||
if plugin_cfg := Config.get(plugin_model):
|
||||
plugin_configs = plugin_cfg
|
||||
plugin_data = PluginData(
|
||||
model=plugin_model,
|
||||
name=plugin_name.strip(),
|
||||
plugin_type=plugin_type,
|
||||
usage=plugin_usage,
|
||||
des=plugin_des,
|
||||
task=plugin_task,
|
||||
menu_type=menu_type,
|
||||
version=plugin_version,
|
||||
author=plugin_author,
|
||||
plugin_setting=plugin_setting,
|
||||
plugin_cd=plugin_cd,
|
||||
plugin_block=plugin_block,
|
||||
plugin_count=plugin_count,
|
||||
plugin_resources=plugin_resources,
|
||||
plugin_configs=plugin_configs,
|
||||
)
|
||||
plugin_data_manager.add_plugin_info(plugin_data)
|
||||
except Exception as e:
|
||||
logger.error(f"构造插件数据失败 {matcher.plugin_name} - {type(e)}:{e}")
|
||||
@@ -1,13 +1,10 @@
|
||||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from ruamel.yaml import round_trip_load, round_trip_dump, YAML
|
||||
from utils.manager import admin_manager, plugins_manager
|
||||
from utils.manager import admin_manager, plugins_manager, plugin_data_manager
|
||||
from configs.config import Config
|
||||
from services.log import logger
|
||||
from utils.text_utils import prompt2cn
|
||||
from utils.utils import get_matchers
|
||||
from utils.utils import scheduler
|
||||
from configs.path_config import DATA_PATH
|
||||
from ruamel import yaml
|
||||
|
||||
@@ -20,67 +17,50 @@ def init_plugins_config():
|
||||
初始化插件数据配置
|
||||
"""
|
||||
plugins2config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
||||
plugins2config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
_data = {}
|
||||
if plugins2config_file.exists():
|
||||
_data = _yaml.load(open(plugins2config_file, "r", encoding="utf8"))
|
||||
_data = Config.get_data()
|
||||
# 优先使用 metadata 数据
|
||||
for matcher in [matcher for matcher in get_matchers(True) if matcher.plugin and matcher.plugin.module]:
|
||||
_plugin = matcher.plugin
|
||||
metadata = _plugin.metadata
|
||||
_module = _plugin.module
|
||||
plugin_version = None
|
||||
if metadata:
|
||||
plugin_version = metadata.extra.get("version")
|
||||
if not plugin_version and hasattr(_module, "__plugin_version__"):
|
||||
plugin_version = _module.__getattribute__("__plugin_version__")
|
||||
if metadata and metadata.config:
|
||||
plugin_configs = {}
|
||||
for key, value in metadata.config.__fields__.items():
|
||||
plugin_configs[key.upper()] = {
|
||||
"value": value.default,
|
||||
"default_value": value.default
|
||||
}
|
||||
else:
|
||||
try:
|
||||
plugin_configs = _module.__getattribute__("__plugin_configs__")
|
||||
except AttributeError:
|
||||
continue
|
||||
# 插件配置版本更新或为Version为None或不在存储配置内,当使用metadata时,必定更新
|
||||
if isinstance(plugin_version, str) or (
|
||||
plugin_version is None
|
||||
or (
|
||||
_data.get(matcher.plugin_name)
|
||||
and _data[matcher.plugin_name].keys() != plugin_configs.keys()
|
||||
)
|
||||
or plugin_version > plugins_manager.get(matcher.plugin_name).version
|
||||
or matcher.plugin_name not in _data.keys()
|
||||
):
|
||||
for key in plugin_configs:
|
||||
if isinstance(plugin_configs[key], dict):
|
||||
for matcher in get_matchers(True):
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
# 插件配置版本更新或为Version为None或不在存储配置内,当使用metadata时,必定更新
|
||||
if plugin_data.plugin_configs and (
|
||||
isinstance(plugin_data.version, str)
|
||||
or (
|
||||
plugin_data.version is None
|
||||
or (
|
||||
_data.get(matcher.plugin_name)
|
||||
and _data[matcher.plugin_name].keys()
|
||||
!= plugin_data.plugin_configs.keys()
|
||||
)
|
||||
or plugin_data.version
|
||||
> plugins_manager.get(matcher.plugin_name).version
|
||||
or matcher.plugin_name not in _data.keys()
|
||||
)
|
||||
):
|
||||
plugin_configs = plugin_data.plugin_configs
|
||||
for key in plugin_configs:
|
||||
if isinstance(plugin_data.plugin_configs[key], dict):
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs[key].get("value"),
|
||||
help_=plugin_configs[key].get("help"),
|
||||
default_value=plugin_configs[key].get("default_value"),
|
||||
_override=True,
|
||||
)
|
||||
else:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name, key, plugin_configs[key]
|
||||
)
|
||||
elif plugin_configs := _data.get(matcher.plugin_name):
|
||||
for key in plugin_configs:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs[key].get("value"),
|
||||
help_=plugin_configs[key].get("help"),
|
||||
default_value=plugin_configs[key].get("default_value"),
|
||||
plugin_configs[key]["value"],
|
||||
help_=plugin_configs[key]["help"],
|
||||
default_value=plugin_configs[key]["default_value"],
|
||||
_override=True,
|
||||
)
|
||||
else:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name, key, plugin_configs[key]
|
||||
)
|
||||
else:
|
||||
plugin_configs = _data[matcher.plugin_name]
|
||||
for key in plugin_configs:
|
||||
Config.add_plugin_config(
|
||||
matcher.plugin_name,
|
||||
key,
|
||||
plugin_configs[key]["value"],
|
||||
help_=plugin_configs[key]["help"],
|
||||
default_value=plugin_configs[key]["default_value"],
|
||||
_override=True,
|
||||
)
|
||||
if not Config.is_empty():
|
||||
Config.save()
|
||||
_data = round_trip_load(open(plugins2config_file, encoding="utf8"))
|
||||
@@ -92,26 +72,13 @@ def init_plugins_config():
|
||||
_data[plugin].yaml_set_start_comment(plugin_name, indent=2)
|
||||
# 初始化未设置的管理员权限等级
|
||||
for k, v in Config.get_admin_level_data():
|
||||
# try:
|
||||
admin_manager.set_admin_level(k, v)
|
||||
# except KeyError as e:
|
||||
# raise KeyError(f"{e} ****** 请检查是否有插件加载失败 ******")
|
||||
# 存完插件基本设置
|
||||
with open(plugins2config_file, "w", encoding="utf8") as wf:
|
||||
round_trip_dump(
|
||||
_data, wf, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
user_config_file = Path() / "configs" / "config.yaml"
|
||||
# if not user_config_file.exists():
|
||||
_replace_config()
|
||||
# else:
|
||||
# logger.info('五分钟后将进行配置数据替换,请注意...')
|
||||
# scheduler.add_job(
|
||||
# _replace_config,
|
||||
# "date",
|
||||
# run_date=datetime.now() + timedelta(minutes=5),
|
||||
# id=f"_replace_config"
|
||||
# )
|
||||
|
||||
|
||||
def _replace_config():
|
||||
@@ -151,9 +118,7 @@ def _replace_config():
|
||||
temp_file = Path() / "configs" / "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, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
with open(temp_file, "r", encoding="utf8") as rf:
|
||||
_data = round_trip_load(rf)
|
||||
# 添加注释
|
||||
@@ -187,9 +152,7 @@ def _replace_config():
|
||||
rst += f'{k}: {Config[plugin][k]["help"]}' + "\n"
|
||||
_data[plugin].yaml_set_start_comment(rst[:-1], indent=2)
|
||||
with open(Path() / "configs" / "config.yaml", "w", encoding="utf8") as wf:
|
||||
round_trip_dump(
|
||||
_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
round_trip_dump(_data, wf, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||
except Exception as e:
|
||||
logger.error(f"生成简易配置注释错误 {type(e)}:{e}")
|
||||
if temp_file.exists():
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
|
||||
from ruamel.yaml import YAML
|
||||
from utils.manager import plugins_manager
|
||||
from utils.manager import plugins_manager, plugin_data_manager
|
||||
from utils.utils import get_matchers
|
||||
from services.log import logger
|
||||
from configs.path_config import DATA_PATH
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
@@ -18,13 +17,10 @@ def init_plugins_data():
|
||||
"""
|
||||
初始化插件数据信息
|
||||
"""
|
||||
plugin2data_file = DATA_PATH / "manager" / "plugin_manager.json"
|
||||
plugin2data_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
for matcher in get_matchers(True):
|
||||
_plugin = matcher.plugin
|
||||
if not _plugin:
|
||||
continue
|
||||
metadata = _plugin.metadata
|
||||
try:
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
@@ -35,49 +31,27 @@ def init_plugins_data():
|
||||
else:
|
||||
plugins_manager[matcher.plugin_name].error = True
|
||||
else:
|
||||
try:
|
||||
plugin_version = None
|
||||
if metadata:
|
||||
plugin_version = metadata.extra.get("version")
|
||||
if not plugin_version and hasattr(_module, "__plugin_version__"):
|
||||
plugin_version = _module.__getattribute__("__plugin_version__")
|
||||
if metadata:
|
||||
plugin_name = metadata.name
|
||||
else:
|
||||
try:
|
||||
plugin_name = _module.__getattribute__("__zx_plugin_name__")
|
||||
except AttributeError:
|
||||
plugin_name = matcher.plugin_name
|
||||
plugin_author = None
|
||||
if metadata:
|
||||
plugin_author = metadata.extra.get('author')
|
||||
if not plugin_author and hasattr(_module, "__plugin_author__"):
|
||||
plugin_author = _module.__getattribute__("__plugin_author__")
|
||||
if matcher.plugin_name in plugins_manager.keys():
|
||||
plugins_manager[matcher.plugin_name].error = False
|
||||
if matcher.plugin_name not in plugins_manager.keys():
|
||||
plugins_manager.add_plugin_data(
|
||||
matcher.plugin_name,
|
||||
plugin_name=plugin_name,
|
||||
author=plugin_author,
|
||||
version=plugin_version,
|
||||
)
|
||||
# metadata不检测version
|
||||
elif isinstance(plugin_version, str) or plugins_manager[matcher.plugin_name].version is None or (
|
||||
plugin_version is not None
|
||||
and plugin_version > float(plugins_manager[matcher.plugin_name].version)
|
||||
):
|
||||
plugins_manager[matcher.plugin_name].plugin_name = plugin_name
|
||||
plugins_manager[matcher.plugin_name].author = plugin_author
|
||||
plugins_manager[matcher.plugin_name].version = plugin_version
|
||||
# if matcher.plugin_name in _data.keys():
|
||||
# plugins_manager[matcher.plugin_name].error = _data[matcher.plugin_name]["error"]
|
||||
# plugins_manager.set_module_data(
|
||||
# matcher.plugin_name, "error", _data[matcher.plugin_name]["error"]
|
||||
# )
|
||||
# plugins_manager.set_module_data(
|
||||
# matcher.plugin_name, "plugin_name", _data[matcher.plugin_name]["plugin_name"]
|
||||
# )
|
||||
except Exception as e:
|
||||
logger.error(f"插件数据 {matcher.plugin_name} 加载发生错误 {type(e)}:{e}")
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
try:
|
||||
plugin_version = plugin_data.version
|
||||
plugin_name = plugin_data.name
|
||||
plugin_author = plugin_data.author
|
||||
if matcher.plugin_name in plugins_manager.keys():
|
||||
plugins_manager[matcher.plugin_name].error = False
|
||||
if matcher.plugin_name not in plugins_manager.keys():
|
||||
plugins_manager.add_plugin_data(
|
||||
matcher.plugin_name,
|
||||
plugin_name=plugin_name,
|
||||
author=plugin_author,
|
||||
version=plugin_version,
|
||||
)
|
||||
elif isinstance(plugin_version, str) or plugins_manager[matcher.plugin_name].version is None or (
|
||||
plugin_version is not None
|
||||
and plugin_version > float(plugins_manager[matcher.plugin_name].version)
|
||||
):
|
||||
plugins_manager[matcher.plugin_name].plugin_name = plugin_name
|
||||
plugins_manager[matcher.plugin_name].author = plugin_author
|
||||
plugins_manager[matcher.plugin_name].version = plugin_version
|
||||
except Exception as e:
|
||||
logger.error(f"插件数据 {matcher.plugin_name} 加载发生错误 {type(e)}:{e}")
|
||||
plugins_manager.save()
|
||||
|
||||
@@ -2,10 +2,10 @@ from utils.manager import (
|
||||
plugins2cd_manager,
|
||||
plugins2block_manager,
|
||||
plugins2count_manager,
|
||||
plugin_data_manager,
|
||||
)
|
||||
from utils.utils import get_matchers
|
||||
from configs.path_config import DATA_PATH
|
||||
import nonebot
|
||||
|
||||
|
||||
def init_plugins_cd_limit():
|
||||
@@ -14,22 +14,16 @@ def init_plugins_cd_limit():
|
||||
"""
|
||||
plugins2cd_file = DATA_PATH / "configs" / "plugins2cd.yaml"
|
||||
plugins2cd_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
_data = {}
|
||||
for matcher in get_matchers(True):
|
||||
if not plugins2cd_manager.get_plugin_cd_data(matcher.plugin_name):
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
plugin_cd_limit = _module.__getattribute__("__plugin_cd_limit__")
|
||||
if not plugins2cd_manager.get_plugin_cd_data(matcher.plugin_name) and (
|
||||
plugin_data := plugin_data_manager.get(matcher.plugin_name)
|
||||
):
|
||||
if plugin_data.plugin_cd:
|
||||
plugins2cd_manager.add_cd_limit(
|
||||
matcher.plugin_name, **plugin_cd_limit
|
||||
matcher.plugin_name, plugin_data.plugin_cd
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
if not plugins2cd_manager.keys():
|
||||
plugins2cd_manager.add_cd_limit(
|
||||
"这是一个示例"
|
||||
)
|
||||
plugins2cd_manager.add_cd_limit("这是一个示例")
|
||||
plugins2cd_manager.save()
|
||||
plugins2cd_manager.reload_cd_limit()
|
||||
|
||||
@@ -38,23 +32,16 @@ def init_plugins_block_limit():
|
||||
"""
|
||||
加载阻塞限制
|
||||
"""
|
||||
plugins2block_file = DATA_PATH / "configs" / "plugins2block.yaml"
|
||||
plugins2block_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
for matcher in get_matchers(True):
|
||||
if not plugins2block_manager.get_plugin_block_data(matcher.plugin_name):
|
||||
_plugin = matcher.plugin
|
||||
try:
|
||||
_module = _plugin.module
|
||||
plugin_block_limit = _module.__getattribute__("__plugin_block_limit__")
|
||||
if not plugins2block_manager.get_plugin_block_data(matcher.plugin_name) and (
|
||||
plugin_data := plugin_data_manager.get(matcher.plugin_name)
|
||||
):
|
||||
if plugin_data.plugin_block:
|
||||
plugins2block_manager.add_block_limit(
|
||||
matcher.plugin_name, **plugin_block_limit
|
||||
matcher.plugin_name, plugin_data.plugin_block
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
if not plugins2block_manager.keys():
|
||||
plugins2block_manager.add_block_limit(
|
||||
"这是一个示例"
|
||||
)
|
||||
plugins2block_manager.add_block_limit("这是一个示例")
|
||||
plugins2block_manager.save()
|
||||
plugins2block_manager.reload_block_limit()
|
||||
|
||||
@@ -63,24 +50,15 @@ def init_plugins_count_limit():
|
||||
"""
|
||||
加载次数限制
|
||||
"""
|
||||
plugins2count_file = DATA_PATH / "configs" / "plugins2count.yaml"
|
||||
plugins2count_file.parent.mkdir(exist_ok=True, parents=True)
|
||||
_data = {}
|
||||
_matchers = get_matchers()
|
||||
for matcher in _matchers:
|
||||
if not plugins2count_manager.get_plugin_count_data(matcher.plugin_name):
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
try:
|
||||
_module = _plugin.module
|
||||
plugin_count_limit = _module.__getattribute__("__plugin_count_limit__")
|
||||
for matcher in get_matchers(True):
|
||||
if not plugins2count_manager.get_plugin_count_data(matcher.plugin_name) and (
|
||||
plugin_data := plugin_data_manager.get(matcher.plugin_name)
|
||||
):
|
||||
if plugin_data.plugin_count:
|
||||
plugins2count_manager.add_count_limit(
|
||||
matcher.plugin_name, **plugin_count_limit
|
||||
matcher.plugin_name, plugin_data.plugin_count
|
||||
)
|
||||
except AttributeError:
|
||||
pass
|
||||
if not plugins2count_manager.keys():
|
||||
plugins2count_manager.add_count_limit(
|
||||
"这是一个示例"
|
||||
)
|
||||
plugins2count_manager.add_count_limit("这是一个示例")
|
||||
plugins2count_manager.save()
|
||||
plugins2count_manager.reload_count_limit()
|
||||
|
||||
@@ -1,29 +1,21 @@
|
||||
from utils.manager import resources_manager
|
||||
from utils.manager import resources_manager, plugin_data_manager
|
||||
from utils.utils import get_matchers
|
||||
from services.log import logger
|
||||
from pathlib import Path
|
||||
import nonebot
|
||||
|
||||
|
||||
def init_plugins_resources():
|
||||
"""
|
||||
资源文件路径的移动
|
||||
"""
|
||||
_tmp = []
|
||||
for matcher in get_matchers():
|
||||
if matcher.plugin_name not in _tmp:
|
||||
_tmp.append(matcher.plugin_name)
|
||||
_plugin = nonebot.plugin.get_plugin(matcher.plugin_name)
|
||||
for matcher in get_matchers(True):
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
try:
|
||||
_module = _plugin.module
|
||||
_module = matcher.plugin.module
|
||||
except AttributeError:
|
||||
logger.warning(f"插件 {matcher.plugin_name} 加载失败...,资源控制未加载...")
|
||||
else:
|
||||
try:
|
||||
resources = _module.__getattribute__("__plugin_resources__")
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if resources := plugin_data.plugin_resources:
|
||||
path = Path(_module.__getattribute__("__file__")).parent
|
||||
for resource in resources.keys():
|
||||
resources_manager.add_resource(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from utils.manager import plugins2settings_manager, admin_manager
|
||||
from utils.manager import plugins2settings_manager, admin_manager, plugin_data_manager
|
||||
from services.log import logger
|
||||
from utils.manager.models import PluginType
|
||||
from utils.utils import get_matchers
|
||||
import nonebot
|
||||
|
||||
@@ -8,93 +9,33 @@ def init_plugins_settings():
|
||||
"""
|
||||
初始化插件设置,从插件中获取 __zx_plugin_name__,__plugin_cmd__,__plugin_settings__
|
||||
"""
|
||||
_tmp_module = {}
|
||||
for x in plugins2settings_manager.keys():
|
||||
try:
|
||||
_plugin = nonebot.plugin.get_plugin(x)
|
||||
_module = _plugin.module
|
||||
metadata = _plugin.metadata
|
||||
plugin_name = (
|
||||
metadata.name
|
||||
if metadata
|
||||
else _module.__getattribute__("__zx_plugin_name__")
|
||||
)
|
||||
_tmp_module[x] = plugin_name
|
||||
_module.__getattribute__("__zx_plugin_name__")
|
||||
except (KeyError, AttributeError) as e:
|
||||
logger.warning(f"配置文件 模块:{x} 获取 plugin_name 失败...{e}")
|
||||
_tmp_module[x] = ""
|
||||
for matcher in [x for x in get_matchers(True) if x.plugin]:
|
||||
for matcher in get_matchers(True):
|
||||
try:
|
||||
if matcher.plugin_name not in plugins2settings_manager.keys():
|
||||
_plugin = matcher.plugin
|
||||
metadata = _plugin.metadata
|
||||
try:
|
||||
_module = _plugin.module
|
||||
except AttributeError:
|
||||
logger.warning(f"插件 {matcher.plugin_name} 加载失败...,插件控制未加载.")
|
||||
else:
|
||||
try:
|
||||
plugin_name = metadata.name if metadata else _module.__getattribute__("__zx_plugin_name__")
|
||||
# 管理员命令
|
||||
if "[admin]" in plugin_name.lower():
|
||||
level = 5
|
||||
cmd = None
|
||||
if hasattr(_module, "__plugin_settings__"):
|
||||
admin_settings = _module.__getattribute__(
|
||||
"__plugin_settings__"
|
||||
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
||||
if plugin_settings := plugin_data.plugin_setting:
|
||||
# 管理员命令
|
||||
if plugin_data.plugin_type == PluginType.ADMIN:
|
||||
admin_manager.add_admin_plugin_settings(
|
||||
matcher.plugin_name, plugin_settings.cmd, plugin_settings.level
|
||||
)
|
||||
level = admin_settings.get("admin_level", 5)
|
||||
cmd = admin_settings.get("cmd")
|
||||
admin_manager.add_admin_plugin_settings(
|
||||
matcher.plugin_name, cmd, level
|
||||
)
|
||||
if (
|
||||
"[hidden]" in plugin_name.lower()
|
||||
or "[admin]" in plugin_name.lower()
|
||||
or "[superuser]" in plugin_name.lower()
|
||||
or matcher.plugin_name in plugins2settings_manager.keys()
|
||||
):
|
||||
continue
|
||||
except AttributeError:
|
||||
logger.warning(
|
||||
f"获取插件 {matcher.plugin_name} __zx_plugin_name__ 失败...,插件控制未加载."
|
||||
)
|
||||
else:
|
||||
_tmp_module[matcher.plugin_name] = plugin_name
|
||||
if hasattr(_module, "__plugin_settings__"):
|
||||
plugin_settings = _module.__getattribute__(
|
||||
"__plugin_settings__"
|
||||
)
|
||||
else:
|
||||
plugin_settings = {"cmd": [matcher.plugin_name, plugin_name]}
|
||||
if plugin_settings.get("cost_gold") is None:
|
||||
plugin_settings["cost_gold"] = 0
|
||||
if (
|
||||
plugin_settings.get("cmd") is not None
|
||||
and plugin_name not in plugin_settings["cmd"]
|
||||
):
|
||||
plugin_settings["cmd"].append(plugin_name)
|
||||
if plugins2settings_manager.get(
|
||||
matcher.plugin_name
|
||||
) and plugins2settings_manager[matcher.plugin_name].get(
|
||||
"plugin_type"
|
||||
):
|
||||
plugin_type = tuple(
|
||||
plugins2settings_manager.get_plugin_data(
|
||||
matcher.plugin_name
|
||||
).plugin_type
|
||||
)
|
||||
else:
|
||||
if hasattr(_module, "__plugin_type__"):
|
||||
plugin_type = _module.__getattribute__("__plugin_type__")
|
||||
else:
|
||||
plugin_type = ("normal",)
|
||||
if plugin_settings and matcher.plugin_name:
|
||||
plugins2settings_manager.add_plugin_settings(
|
||||
matcher.plugin_name,
|
||||
plugin_type=plugin_type,
|
||||
**plugin_settings,
|
||||
)
|
||||
plugins2settings_manager.add_plugin_settings(
|
||||
matcher.plugin_name, plugin_settings
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f'{matcher.plugin_name} 初始化 plugin_settings 发生错误 {type(e)}:{e}')
|
||||
plugins2settings_manager.save()
|
||||
|
||||
Reference in New Issue
Block a user