🔀 Merge pull request #344

Docs: update load a plugin
This commit is contained in:
Ju4tCode 2021-05-01 10:59:14 +08:00 committed by GitHub
commit fa73c13800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,10 +39,11 @@ if __name__ == "__main__":
`bot.py` 文件中添加以下行:
```python{5}
```python{6}
import nonebot
nonebot.init()
# 加载插件目录,该目录下为各插件,以下划线开头的插件将不会被加载
nonebot.load_plugins("awesome_bot/plugins")
@ -68,10 +69,11 @@ if __name__ == "__main__":
`bot.py` 文件中添加以下行:
```python{5,7}
```python{6,8}
import nonebot
nonebot.init()
# 加载一个 pip 安装的插件
nonebot.load_plugin("nonebot_plugin_status")
# 加载本地的单独插件
@ -83,6 +85,63 @@ if __name__ == "__main__":
nonebot.run()
```
## 从 json 文件中加载插件
`bot.py` 文件中添加以下行:
```python{6}
import nonebot
nonebot.init()
# 从 plugin.json 加载插件
nonebot.load_from_json("plugin.json")
app = nonebot.get_asgi()
if __name__ == "__main__":
nonebot.run()
```
**json 文件示例**
```json
{
"plugins": ["nonebot_plugin_status", "awesome_bot.plugins.xxx"],
"plugin_dirs": ["awesome_bot/plugins"]
}
```
## 从 toml 文件中加载插件
`bot.py` 文件中添加以下行:
```python{6}
import nonebot
nonebot.init()
# 从 pyproject.toml 加载插件
nonebot.load_from_toml("pyproject.toml")
app = nonebot.get_asgi()
if __name__ == "__main__":
nonebot.run()
```
**toml 文件示例:**
```toml
[nonebot.plugins]
plugins = ["nonebot_plugin_status", "awesome_bot.plugins.xxx"]
plugin_dirs = ["awesome_bot/plugins"]
```
::: tip
nb-cli 默认使用 `pyproject.toml` 加载插件。
:::
## 子插件(嵌套插件)
在插件中同样可以加载子插件,例如如下插件目录结构: