2022-12-04 16:46:33 +08:00
|
|
|
|
from utils.manager import plugins2settings_manager, admin_manager, plugin_data_manager
|
2021-11-23 21:44:59 +08:00
|
|
|
|
from services.log import logger
|
2022-12-04 16:46:33 +08:00
|
|
|
|
from utils.manager.models import PluginType
|
2021-11-23 21:44:59 +08:00
|
|
|
|
from utils.utils import get_matchers
|
|
|
|
|
|
import nonebot
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-11-21 20:43:41 +08:00
|
|
|
|
def init_plugins_settings():
|
2021-11-23 21:44:59 +08:00
|
|
|
|
"""
|
|
|
|
|
|
初始化插件设置,从插件中获取 __zx_plugin_name__,__plugin_cmd__,__plugin_settings__
|
|
|
|
|
|
"""
|
2022-12-31 14:59:56 +08:00
|
|
|
|
# for x in plugins2settings_manager.keys():
|
|
|
|
|
|
# try:
|
|
|
|
|
|
# _plugin = nonebot.plugin.get_plugin(x)
|
|
|
|
|
|
# _module = _plugin.module
|
|
|
|
|
|
# _module.__getattribute__("__zx_plugin_name__")
|
|
|
|
|
|
# except (KeyError, AttributeError) as e:
|
|
|
|
|
|
# logger.warning(f"配置文件 模块:{x} 获取 plugin_name 失败...{e}")
|
2022-12-04 16:46:33 +08:00
|
|
|
|
for matcher in get_matchers(True):
|
2022-09-16 19:29:05 +08:00
|
|
|
|
try:
|
|
|
|
|
|
if matcher.plugin_name not in plugins2settings_manager.keys():
|
|
|
|
|
|
_plugin = matcher.plugin
|
2021-11-23 21:44:59 +08:00
|
|
|
|
try:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
_module = _plugin.module
|
2021-11-23 21:44:59 +08:00
|
|
|
|
except AttributeError:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
logger.warning(f"插件 {matcher.plugin_name} 加载失败...,插件控制未加载.")
|
2021-11-23 21:44:59 +08:00
|
|
|
|
else:
|
2022-12-04 16:46:33 +08:00
|
|
|
|
if plugin_data := plugin_data_manager.get(matcher.plugin_name):
|
|
|
|
|
|
if plugin_settings := plugin_data.plugin_setting:
|
2023-02-16 22:59:53 +08:00
|
|
|
|
if (name := _module.__getattribute__("__zx_plugin_name__")) not in plugin_settings.cmd:
|
|
|
|
|
|
plugin_settings.cmd.append(name)
|
2022-12-04 16:46:33 +08:00
|
|
|
|
# 管理员命令
|
|
|
|
|
|
if plugin_data.plugin_type == PluginType.ADMIN:
|
|
|
|
|
|
admin_manager.add_admin_plugin_settings(
|
|
|
|
|
|
matcher.plugin_name, plugin_settings.cmd, plugin_settings.level
|
2022-09-16 19:29:05 +08:00
|
|
|
|
)
|
2022-11-21 20:43:41 +08:00
|
|
|
|
else:
|
2022-12-04 16:46:33 +08:00
|
|
|
|
plugins2settings_manager.add_plugin_settings(
|
|
|
|
|
|
matcher.plugin_name, plugin_settings
|
|
|
|
|
|
)
|
2022-09-16 19:29:05 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f'{matcher.plugin_name} 初始化 plugin_settings 发生错误 {type(e)}:{e}')
|
2022-11-21 20:43:41 +08:00
|
|
|
|
plugins2settings_manager.save()
|
2021-11-23 21:44:59 +08:00
|
|
|
|
logger.info(f"已成功加载 {len(plugins2settings_manager.get_data())} 个非限制插件.")
|