Files
HeXi/dev/plugin_template/nonebot_plugin_template/config.py
T
sansenhoshi 131b92b319 结构调整
视频解析多图/多媒体结构 消息体适配
2026-09-08 14:25:32 +08:00

93 lines
2.4 KiB
Python

"""配置注册:按《插件配置文件标准》统一接入 Web 管理台。
此处演示三类:
1) register_model_config → 读 .env / NoneBot config 的 pydantic Config
2) register_config_items → 来源无关(模块常量)
3) register_object_set → 一组结构化条目(数组/对象集合)
"""
from __future__ import annotations
from nonebot import get_plugin_config
from pydantic import BaseModel
from hexi.web_hub.config_standard import register_config_items, register_model_config, register_object_set
# plugin_id = NoneBot 插件模块名
_PLUGIN_ID = __package__
class Config(BaseModel):
enable: bool = True
reply: str = "你好,我是模板插件!"
config = get_plugin_config(Config)
# —— 来源无关示例:模块级常量(Web 可读可改) ——
_COLOR = "red"
def _get_color() -> str:
return _COLOR
def _set_color(v: str) -> None:
global _COLOR
_COLOR = v
# —— 对象集示例:一组条目(如服务器/关键词/白名单) ——
_ITEMS: dict[str, dict] = {}
def _obj_getter() -> list[dict]:
return [{"name": k, "value": v.get("value", "")} for k, v in _ITEMS.items()]
def _obj_setter(rows: list[dict]) -> None:
_ITEMS.clear()
for row in rows:
_ITEMS[str(row["name"])] = {"value": row.get("value", "")}
def register_config() -> None:
# 1) env/pydantic 配置
register_model_config(
_PLUGIN_ID,
config,
fields=["enable", "reply"],
labels={"enable": "启用", "reply": "回复内容"},
descriptions={"enable": "关闭后指令直接提示已停用", "reply": "模板指令的默认回复"},
)
# 2) 来源无关(模块常量)
register_config_items(
_PLUGIN_ID,
[
{
"key": "color",
"label": "主题色",
"type": "string",
"getter": _get_color,
"setter": _set_color,
"secret": False,
}
],
)
# 3) 对象集(数组/结构化条目)
register_object_set(
_PLUGIN_ID,
"dict_items",
[
{"key": "name", "label": "名称", "type": "string"},
{"key": "value", "label": "值", "type": "string"},
],
key_field="name",
getter=_obj_getter,
setter=_obj_setter,
label="条目表",
# nosave=True 表示权威源在插件自身(文件/DB);此处写进 plugin_config.json,故为 False
)