mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-02 10:10:01 +08:00
webui主页和插件管理api更新
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
from typing import List, Optional
|
||||
|
||||
import cattrs
|
||||
from fastapi import APIRouter
|
||||
|
||||
from configs.config import Config
|
||||
from services.log import logger
|
||||
from utils.manager import plugin_data_manager, plugins2settings_manager, plugins_manager
|
||||
from utils.manager.models import PluginData, PluginSetting, PluginType
|
||||
|
||||
from ....base_model import Result
|
||||
from ....utils import authentication
|
||||
from .model import PluginCount, PluginInfo, PluginSwitch, UpdateConfig, UpdatePlugin
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/get_plugin_list", dependencies=[authentication()], deprecated="获取插件列表")
|
||||
def _(
|
||||
plugin_type: PluginType, menu_type: Optional[str] = None
|
||||
) -> Result:
|
||||
"""
|
||||
获取插件列表
|
||||
:param plugin_type: 类型 normal, superuser, hidden, admin
|
||||
"""
|
||||
try:
|
||||
plugin_list: List[PluginInfo] = []
|
||||
for module in plugin_data_manager.keys():
|
||||
plugin_data: Optional[PluginData] = plugin_data_manager[module]
|
||||
if plugin_data and plugin_data.plugin_type == plugin_type:
|
||||
setting = plugin_data.plugin_setting or PluginSetting()
|
||||
plugin = plugin_data.plugin_status
|
||||
menu_type_ = getattr(setting, "plugin_type", ["无"])[0]
|
||||
if menu_type and menu_type != menu_type_:
|
||||
continue
|
||||
plugin_info = PluginInfo(
|
||||
module=module,
|
||||
plugin_name=plugin_data.name,
|
||||
default_switch=getattr(setting, "default_status", False),
|
||||
limit_superuser=getattr(setting, "limit_superuser", False),
|
||||
cost_gold=getattr(setting, "cost_gold", 0),
|
||||
menu_type=menu_type_,
|
||||
version=(plugin.version or 0) if plugin else 0,
|
||||
level=getattr(setting, "level", 5),
|
||||
status=plugin.status if plugin else False,
|
||||
author=plugin.author if plugin else None
|
||||
)
|
||||
plugin_info.version = (plugin.version or 0) if plugin else 0
|
||||
plugin_list.append(plugin_info)
|
||||
except Exception as e:
|
||||
logger.error("调用API错误", "/get_plugins", e=e)
|
||||
return Result.fail(f"{type(e)}: {e}")
|
||||
return Result.ok(plugin_list, "拿到了新鲜出炉的数据!")
|
||||
|
||||
@router.get("/get_plugin_count", dependencies=[authentication()], deprecated="获取插件数量")
|
||||
def _() -> Result:
|
||||
plugin_count = PluginCount()
|
||||
for module in plugin_data_manager.keys():
|
||||
plugin_data: Optional[PluginData] = plugin_data_manager[module]
|
||||
if plugin_data and plugin_data.plugin_type == PluginType.NORMAL:
|
||||
plugin_count.normal += 1
|
||||
elif plugin_data and plugin_data.plugin_type == PluginType.ADMIN:
|
||||
plugin_count.admin += 1
|
||||
elif plugin_data and plugin_data.plugin_type == PluginType.SUPERUSER:
|
||||
plugin_count.superuser += 1
|
||||
else:
|
||||
plugin_count.other += 1
|
||||
return Result.ok(plugin_count)
|
||||
|
||||
@router.post("/update_plugins", dependencies=[authentication()], description="更新插件参数")
|
||||
def _(plugin: UpdatePlugin) -> Result:
|
||||
"""
|
||||
修改插件信息
|
||||
:param plugin: 插件内容
|
||||
"""
|
||||
try:
|
||||
module = plugin.module
|
||||
if p2s := plugins2settings_manager.get(module):
|
||||
p2s.default_status = plugin.default_status
|
||||
p2s.limit_superuser = plugin.limit_superuser
|
||||
p2s.cost_gold = plugin.cost_gold
|
||||
p2s.cmd = plugin.cmd.split(",") if plugin.cmd else []
|
||||
p2s.level = plugin.level
|
||||
if pd := plugin_data_manager.get(module):
|
||||
menu_lin = None
|
||||
if len(pd.menu_type) > 1:
|
||||
menu_lin = pd.menu_type[1]
|
||||
if menu_lin is not None:
|
||||
pd.menu_type = (plugin.menu_type, menu_lin)
|
||||
else:
|
||||
pd.menu_type = (plugin.menu_type,)
|
||||
if pm := plugins_manager.get(module):
|
||||
if plugin.block_type:
|
||||
pm.block_type = plugin.block_type
|
||||
pm.status = False
|
||||
else:
|
||||
pm.block_type = None
|
||||
pm.status = True
|
||||
plugins2settings_manager.save()
|
||||
plugins_manager.save()
|
||||
except Exception as e:
|
||||
logger.error("调用API错误", "/update_plugins", e=e)
|
||||
return Result.fail(f"{type(e)}: {e}")
|
||||
return Result.ok(info="已经帮你写好啦!")
|
||||
|
||||
|
||||
@router.post("/update_config", dependencies=[authentication()], description="更新配置")
|
||||
def _(config_list: List[UpdateConfig]) -> Result:
|
||||
try:
|
||||
for config in config_list:
|
||||
if cg := Config.get(config.module):
|
||||
if c := cg.configs.get(config.key):
|
||||
if isinstance(c.value, (list, tuple)) or isinstance(
|
||||
c.default_value, (list, tuple)
|
||||
):
|
||||
value = config.value.split(",")
|
||||
else:
|
||||
value = config.value
|
||||
if c.type and value is not None:
|
||||
value = cattrs.structure(value, c.type)
|
||||
Config.set_config(config.module, config.key, value)
|
||||
except Exception as e:
|
||||
logger.error("调用API错误", "/update_config", e=e)
|
||||
return Result.fail(f"{type(e)}: {e}")
|
||||
Config.save(save_simple_data=True)
|
||||
return Result.ok(info="写入配置项了哦!")
|
||||
|
||||
|
||||
@router.post("/change_switch", dependencies=[authentication()], description="开关插件")
|
||||
def _(param: PluginSwitch) -> Result:
|
||||
if pm := plugins_manager.get(param.module):
|
||||
pm.block_type = None if param.status else 'all'
|
||||
pm.status = param.status
|
||||
plugins_manager.save()
|
||||
return Result.ok(info="成功改变了开关状态!")
|
||||
return Result.warning_("未获取该插件的配置!")
|
||||
|
||||
|
||||
@router.get("/get_plugin_menu_type", dependencies=[authentication()], description="获取插件类型")
|
||||
def _() -> Result:
|
||||
menu_type_list = []
|
||||
for module in plugin_data_manager.keys():
|
||||
plugin_data: Optional[PluginData] = plugin_data_manager[module]
|
||||
if plugin_data:
|
||||
setting = plugin_data.plugin_setting or PluginSetting()
|
||||
menu_type = getattr(setting, "plugin_type", ["无"])[0]
|
||||
if menu_type not in menu_type_list:
|
||||
menu_type_list.append(menu_type)
|
||||
return Result.ok(menu_type_list)
|
||||
@@ -0,0 +1,135 @@
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from utils.manager.models import Plugin as PluginManager
|
||||
from utils.manager.models import (
|
||||
PluginBlock,
|
||||
PluginCd,
|
||||
PluginCount,
|
||||
PluginSetting,
|
||||
PluginType,
|
||||
)
|
||||
from utils.typing import BLOCK_TYPE
|
||||
|
||||
|
||||
class PluginSwitch(BaseModel):
|
||||
"""
|
||||
插件开关
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""模块"""
|
||||
status: bool
|
||||
"""开关状态"""
|
||||
|
||||
|
||||
class UpdateConfig(BaseModel):
|
||||
"""
|
||||
配置项修改参数
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""模块"""
|
||||
key: str
|
||||
"""配置项key"""
|
||||
value: Any
|
||||
"""配置项值"""
|
||||
|
||||
|
||||
class UpdatePlugin(BaseModel):
|
||||
"""
|
||||
插件修改参数
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""模块"""
|
||||
default_status: bool
|
||||
"""默认开关"""
|
||||
limit_superuser: bool
|
||||
"""限制超级用户"""
|
||||
cost_gold: int
|
||||
"""金币花费"""
|
||||
cmd: str
|
||||
"""插件别名"""
|
||||
menu_type: str
|
||||
"""插件菜单类型"""
|
||||
level: int
|
||||
"""插件所需群权限"""
|
||||
block_type: BLOCK_TYPE
|
||||
"""禁用类型"""
|
||||
|
||||
|
||||
class PluginInfo(BaseModel):
|
||||
"""
|
||||
基本插件信息
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""插件名称"""
|
||||
plugin_name: str
|
||||
"""插件中文名称"""
|
||||
default_switch: bool
|
||||
"""默认开关"""
|
||||
limit_superuser: bool
|
||||
"""限制超级用户"""
|
||||
cost_gold: int
|
||||
"""花费金币"""
|
||||
menu_type: str
|
||||
"""插件菜单类型"""
|
||||
version: Union[int, str, float]
|
||||
"""插件版本"""
|
||||
level: int
|
||||
"""群权限"""
|
||||
status: bool
|
||||
"""当前状态"""
|
||||
author: Optional[str] = None
|
||||
"""作者"""
|
||||
|
||||
|
||||
class PluginConfig(BaseModel):
|
||||
"""
|
||||
插件配置项
|
||||
"""
|
||||
|
||||
module: str
|
||||
key: str
|
||||
value: Any
|
||||
help: Optional[str]
|
||||
default_value: Any
|
||||
has_type: bool
|
||||
|
||||
|
||||
class Plugin(BaseModel):
|
||||
"""
|
||||
插件
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""模块名称"""
|
||||
plugin_settings: Optional[PluginSetting]
|
||||
"""settings"""
|
||||
plugin_manager: Optional[PluginManager]
|
||||
"""manager"""
|
||||
plugin_config: Optional[Dict[str, PluginConfig]]
|
||||
"""配置项"""
|
||||
cd_limit: Optional[PluginCd]
|
||||
"""cd限制"""
|
||||
block_limit: Optional[PluginBlock]
|
||||
"""阻断限制"""
|
||||
count_limit: Optional[PluginCount]
|
||||
"""次数限制"""
|
||||
|
||||
class PluginCount(BaseModel):
|
||||
"""
|
||||
插件数量
|
||||
"""
|
||||
|
||||
normal: int = 0
|
||||
"""普通插件"""
|
||||
admin: int = 0
|
||||
"""管理员插件"""
|
||||
superuser: int = 0
|
||||
"""超级用户插件"""
|
||||
other: int = 0
|
||||
"""其他插件"""
|
||||
Reference in New Issue
Block a user