2021-11-23 21:44:59 +08:00
|
|
|
|
from utils.manager import plugins2settings_manager, admin_manager
|
|
|
|
|
|
from services.log import logger
|
|
|
|
|
|
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__
|
|
|
|
|
|
"""
|
|
|
|
|
|
_tmp_module = {}
|
|
|
|
|
|
for x in plugins2settings_manager.keys():
|
|
|
|
|
|
try:
|
|
|
|
|
|
_plugin = nonebot.plugin.get_plugin(x)
|
|
|
|
|
|
_module = _plugin.module
|
2022-07-31 17:30:29 +08:00
|
|
|
|
metadata = _plugin.metadata
|
2022-08-21 18:09:31 +08:00
|
|
|
|
plugin_name = (
|
|
|
|
|
|
metadata.name
|
|
|
|
|
|
if metadata
|
|
|
|
|
|
else _module.__getattribute__("__zx_plugin_name__")
|
|
|
|
|
|
)
|
2021-11-23 21:44:59 +08:00
|
|
|
|
_tmp_module[x] = plugin_name
|
|
|
|
|
|
except (KeyError, AttributeError) as e:
|
2021-12-01 14:03:34 +08:00
|
|
|
|
logger.warning(f"配置文件 模块:{x} 获取 plugin_name 失败...{e}")
|
2021-11-23 21:44:59 +08:00
|
|
|
|
_tmp_module[x] = ""
|
2022-11-21 20:43:41 +08:00
|
|
|
|
for matcher in [x for x in get_matchers(True) if x.plugin]:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
try:
|
|
|
|
|
|
if matcher.plugin_name not in plugins2settings_manager.keys():
|
|
|
|
|
|
_plugin = matcher.plugin
|
|
|
|
|
|
metadata = _plugin.metadata
|
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:
|
|
|
|
|
|
try:
|
2022-11-21 20:43:41 +08:00
|
|
|
|
plugin_name = metadata.name if metadata else _module.__getattribute__("__zx_plugin_name__")
|
|
|
|
|
|
# 管理员命令
|
2022-09-16 19:29:05 +08:00
|
|
|
|
if "[admin]" in plugin_name.lower():
|
2022-11-21 20:43:41 +08:00
|
|
|
|
level = 5
|
|
|
|
|
|
cmd = None
|
|
|
|
|
|
if hasattr(_module, "__plugin_settings__"):
|
2022-09-16 19:29:05 +08:00
|
|
|
|
admin_settings = _module.__getattribute__(
|
|
|
|
|
|
"__plugin_settings__"
|
|
|
|
|
|
)
|
2022-11-21 20:43:41 +08:00
|
|
|
|
level = admin_settings.get("admin_level", 5)
|
2022-09-16 19:29:05 +08:00
|
|
|
|
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
|
2021-11-23 21:44:59 +08:00
|
|
|
|
except AttributeError:
|
2022-11-21 20:43:41 +08:00
|
|
|
|
logger.warning(
|
|
|
|
|
|
f"获取插件 {matcher.plugin_name} __zx_plugin_name__ 失败...,插件控制未加载."
|
|
|
|
|
|
)
|
2022-07-31 17:30:29 +08:00
|
|
|
|
else:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
_tmp_module[matcher.plugin_name] = plugin_name
|
2022-11-21 20:43:41 +08:00
|
|
|
|
if hasattr(_module, "__plugin_settings__"):
|
2022-09-16 19:29:05 +08:00
|
|
|
|
plugin_settings = _module.__getattribute__(
|
|
|
|
|
|
"__plugin_settings__"
|
|
|
|
|
|
)
|
2022-11-21 20:43:41 +08:00
|
|
|
|
else:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
plugin_settings = {"cmd": [matcher.plugin_name, plugin_name]}
|
2022-11-21 20:43:41 +08:00
|
|
|
|
if plugin_settings.get("cost_gold") is None:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
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
|
2022-11-21 20:43:41 +08:00
|
|
|
|
).plugin_type
|
2022-09-16 19:29:05 +08:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
2022-11-21 20:43:41 +08:00
|
|
|
|
if hasattr(_module, "__plugin_type__"):
|
2022-09-16 19:29:05 +08:00
|
|
|
|
plugin_type = _module.__getattribute__("__plugin_type__")
|
2022-11-21 20:43:41 +08:00
|
|
|
|
else:
|
2022-09-16 19:29:05 +08:00
|
|
|
|
plugin_type = ("normal",)
|
|
|
|
|
|
if plugin_settings and matcher.plugin_name:
|
|
|
|
|
|
plugins2settings_manager.add_plugin_settings(
|
|
|
|
|
|
matcher.plugin_name,
|
|
|
|
|
|
plugin_type=plugin_type,
|
|
|
|
|
|
**plugin_settings,
|
|
|
|
|
|
)
|
|
|
|
|
|
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())} 个非限制插件.")
|