Skip to content

Commit e2481c5

Browse files
committed
插件配置
1 parent e7bd86e commit e2481c5

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

dev/plugin.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ outline: deep
1717

1818
### 获取插件模板
1919

20-
打开 [helloworld](https://github.com/Soulter/helloworld)。点击右上角的 `Use this template`,然后点击 `Create new repository`。在 `Repository name` 处输入你的插件名字,不要中文。建议以 `astrbot_plugin_` 开头,如 `astrbot_plugin_yuanshen`
20+
打开 [helloworld](https://github.com/Soulter/helloworld)。点击右上角的 `Use this template`,然后点击 `Create new repository`。在 `Repository name` 处输入你的插件名字,不要中文。建议以 `astrbot_plugin_` 开头,如 `astrbot_plugin_genshin`
2121

2222
![](../source/images/plugin/image.png)
2323

@@ -374,7 +374,82 @@ async def check_ok(self, event: AstrMessageEvent):
374374
event.stop_event() # 停止事件传播
375375
```
376376

377-
当事件停止传播,**后续所有步骤将不会被执行。**
377+
当事件停止传播,**后续所有步骤将不会被执行。**假设有一个插件A,A终止事件传播之后所有后续操作都不会执行,比如执行其它插件的handler、请求LLM。
378+
379+
### 注册插件配置(beta)
380+
381+
> 大于等于 v3.4.15
382+
383+
随着插件功能的增加,可能需要定义一些配置以让用户自定义插件的行为。
384+
385+
AstrBot 提供了”强大“的配置解析和可视化功能。能够让用户在管理面板上直接配置插件,而不需要修改代码。
386+
387+
![](../source/images/plugin/QQ_1738149538737.png)
388+
389+
**Schema 介绍**
390+
391+
要注册配置,首先需要在您的插件目录下添加一个 `_config_schema.json` 的 json 文件。
392+
393+
文件内容是一个 `Schema`(模式),用于表示配置。Schema 是 json 格式的,例如上图的 Schema 是:
394+
395+
```json
396+
{
397+
"token": {
398+
"description": "Bot Token",
399+
"type": "string",
400+
"hint": "测试醒目提醒",
401+
"obvious_hint": true
402+
},
403+
"sub_config": {
404+
"description": "测试嵌套配置",
405+
"type": "object",
406+
"hint": "xxxx",
407+
"items": {
408+
"name": {
409+
"description": "testsub",
410+
"type": "string",
411+
"hint": "xxxx"
412+
},
413+
"id": {
414+
"description": "testsub",
415+
"type": "int",
416+
"hint": "xxxx"
417+
},
418+
"time": {
419+
"description": "testsub",
420+
"type": "int",
421+
"hint": "xxxx",
422+
"default": 123
423+
}
424+
}
425+
}
426+
}
427+
```
428+
- `type`: **此项必填**。配置的类型。支持 `string`, `int`, `float`, `bool`, `object`, `list`
429+
- `description`: 可选。配置的描述。建议一句话描述配置的行为。
430+
- `hint`: 可选。配置的提示信息,表现在上图中右边的问号按钮,当鼠标悬浮在问号按钮上时显示。
431+
- `obvious_hint`: 可选。配置的 hint 是否醒目显示。如上图的 `token`
432+
- `default`: 可选。配置的默认值。如果用户没有配置,将使用默认值。int 是 0,float 是 0.0,bool 是 False,string 是 "",object 是 {},list 是 []
433+
- `items`: 可选。如果配置的类型是 `object`,需要添加 `items` 字段。`items` 的内容是这个配置项的子 Schema。理论上可以无限嵌套,但是不建议过多嵌套。
434+
- `invisible`: 可选。配置是否隐藏。默认是 `false`。如果设置为 `true`,则不会在管理面板上显示。
435+
436+
**使用配置**
437+
438+
AstrBot 在载入插件时会检测插件目录下是否有 `_config_schema.json` 文件,如果有,会自动解析配置并保存在 `data/config/<plugin_name>_config.json` 下(依照 Schema 创建的配置文件实体),并在实例化插件类时传入给 `__init__()`
439+
440+
```py
441+
@register("config", "Soulter", "一个配置示例", "1.0.0")
442+
class ConfigPlugin(Star):
443+
def __init__(self, context: Context, config: dict):
444+
super().__init__(context)
445+
self.config = config
446+
print(self.config)
447+
```
448+
449+
**配置版本管理**
450+
451+
如果您在发布不同版本时更新了 Schema,请注意,AstrBot 会递归检查 Schema 的配置项,如果发现配置文件中缺失了某个配置项,会自动添加默认值。但是 AstrBot 不会删除配置文件中**多余的**配置项,即使这个配置项在新的 Schema 中不存在(您在新的 Schema 中删除了这个配置项)。
452+
378453

379454
### 文字渲染成图片
380455

0 commit comments

Comments
 (0)