mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-09 13:50:00 +08:00
✨ 父级插件加载
This commit is contained in:
@@ -2,9 +2,9 @@ import nonebot
|
||||
import aiofiles
|
||||
import ujson as json
|
||||
from ruamel.yaml import YAML
|
||||
from nonebot.plugin import Plugin
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot import get_loaded_plugins
|
||||
from nonebot.plugin import Plugin, PluginMetadata
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.models.task_info import TaskInfo
|
||||
@@ -41,64 +41,68 @@ async def _handle_setting(
|
||||
plugin_list: 插件列表
|
||||
limit_list: 插件限制列表
|
||||
"""
|
||||
if metadata := plugin.metadata:
|
||||
extra = metadata.extra
|
||||
extra_data = PluginExtraData(**extra)
|
||||
logger.debug(f"{metadata.name}:{plugin.name} -> {extra}", "初始化插件数据")
|
||||
setting = extra_data.setting or PluginSetting()
|
||||
if metadata.type == "library":
|
||||
extra_data.plugin_type = PluginType.HIDDEN
|
||||
if (
|
||||
extra_data.plugin_type
|
||||
== PluginType.HIDDEN
|
||||
# and extra_data.plugin_type != "功能"
|
||||
):
|
||||
extra_data.menu_type = ""
|
||||
plugin_list.append(
|
||||
PluginInfo(
|
||||
metadata = plugin.metadata
|
||||
if not metadata:
|
||||
if not plugin.sub_plugins:
|
||||
return
|
||||
"""父插件"""
|
||||
metadata = PluginMetadata(name=plugin.name, description="", usage="")
|
||||
extra = metadata.extra
|
||||
extra_data = PluginExtraData(**extra)
|
||||
logger.debug(f"{metadata.name}:{plugin.name} -> {extra}", "初始化插件数据")
|
||||
setting = extra_data.setting or PluginSetting()
|
||||
if metadata.type == "library":
|
||||
extra_data.plugin_type = PluginType.HIDDEN
|
||||
if extra_data.plugin_type == PluginType.HIDDEN:
|
||||
extra_data.menu_type = ""
|
||||
if plugin.sub_plugins:
|
||||
extra_data.plugin_type = PluginType.PARENT
|
||||
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,
|
||||
parent=(plugin.parent_plugin.module_name if plugin.parent_plugin else None),
|
||||
)
|
||||
)
|
||||
if extra_data.limits:
|
||||
limit_list.extend(
|
||||
PluginLimit(
|
||||
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,
|
||||
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.limits:
|
||||
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:
|
||||
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
|
||||
if extra_data.tasks:
|
||||
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
|
||||
@@ -115,8 +119,7 @@ async def _():
|
||||
module2id = {m["module_path"]: m["id"] for m in module_list}
|
||||
for plugin in get_loaded_plugins():
|
||||
load_plugin.append(plugin.module_name)
|
||||
if plugin.metadata:
|
||||
await _handle_setting(plugin, plugin_list, limit_list, task_list)
|
||||
await _handle_setting(plugin, plugin_list, limit_list, task_list)
|
||||
create_list = []
|
||||
update_list = []
|
||||
for plugin in plugin_list:
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot_plugin_alconna import Alconna, Args, Subcommand, on_alconna
|
||||
from nonebot_plugin_session import EventSession
|
||||
from nonebot_plugin_alconna import Args, Alconna, Subcommand, on_alconna
|
||||
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.message import MessageUtils
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
|
||||
from .data_source import ShopManage
|
||||
|
||||
@@ -68,6 +68,7 @@ _matcher.shortcut(
|
||||
prefix=True,
|
||||
)
|
||||
|
||||
|
||||
@_matcher.assign("$main")
|
||||
async def _(session: EventSession):
|
||||
try:
|
||||
@@ -82,9 +83,7 @@ async def _(session: EventSession):
|
||||
@_matcher.assign("add")
|
||||
async def _(session: EventSession, plugin_id: int):
|
||||
try:
|
||||
await MessageUtils.build_message(
|
||||
f"正在添加插件 Id: {plugin_id}"
|
||||
).send()
|
||||
await MessageUtils.build_message(f"正在添加插件 Id: {plugin_id}").send()
|
||||
result = await ShopManage.add_plugin(plugin_id)
|
||||
except Exception as e:
|
||||
logger.error(f"添加插件 Id: {plugin_id}失败", "插件商店", session=session, e=e)
|
||||
@@ -107,24 +106,29 @@ async def _(session: EventSession, plugin_id: int):
|
||||
logger.info(f"移除插件 Id: {plugin_id}", "插件商店", session=session)
|
||||
await MessageUtils.build_message(result).send()
|
||||
|
||||
|
||||
@_matcher.assign("search")
|
||||
async def _(session: EventSession, plugin_name_or_author: str):
|
||||
try:
|
||||
result = await ShopManage.search_plugin(plugin_name_or_author)
|
||||
except Exception as e:
|
||||
logger.error(f"搜索插件 name: {plugin_name_or_author}失败", "插件商店", session=session, e=e)
|
||||
logger.error(
|
||||
f"搜索插件 name: {plugin_name_or_author}失败",
|
||||
"插件商店",
|
||||
session=session,
|
||||
e=e,
|
||||
)
|
||||
await MessageUtils.build_message(
|
||||
f"搜索插件 name: {plugin_name_or_author} 失败 e: {e}"
|
||||
).finish()
|
||||
logger.info(f"搜索插件 name: {plugin_name_or_author}", "插件商店", session=session)
|
||||
await MessageUtils.build_message(result).send()
|
||||
|
||||
|
||||
@_matcher.assign("update")
|
||||
async def _(session: EventSession, plugin_id: int):
|
||||
try:
|
||||
await MessageUtils.build_message(
|
||||
f"正在更新插件 Id: {plugin_id}"
|
||||
).send()
|
||||
await MessageUtils.build_message(f"正在更新插件 Id: {plugin_id}").send()
|
||||
result = await ShopManage.update_plugin(plugin_id)
|
||||
except Exception as e:
|
||||
logger.error(f"更新插件 Id: {plugin_id}失败", "插件商店", session=session, e=e)
|
||||
|
||||
@@ -211,7 +211,7 @@ class ShopManage:
|
||||
plugin_list = await PluginInfo.filter(load_status=True).values_list(
|
||||
"module", "version"
|
||||
)
|
||||
suc_plugin = {p[0]: p[1] for p in plugin_list if p[1]}
|
||||
suc_plugin = {p[0]: (p[1] or "0.1") for p in plugin_list}
|
||||
data_list = [
|
||||
[
|
||||
"已安装" if plugin_info[1]["module"] in suc_plugin else "",
|
||||
@@ -226,7 +226,7 @@ class ShopManage:
|
||||
]
|
||||
return await ImageTemplate.table_page(
|
||||
"插件列表",
|
||||
"通过安装/卸载插件 ID 来管理插件",
|
||||
"通过添加/移除插件 ID 来管理插件",
|
||||
column_name,
|
||||
data_list,
|
||||
text_style=row_style,
|
||||
|
||||
Reference in New Issue
Block a user