From c2c8906b7d5566c342883a42dd9dcb824d7b9408 Mon Sep 17 00:00:00 2001 From: Jigsaw Date: Fri, 30 Apr 2021 16:24:23 +0800 Subject: [PATCH 1/2] :memo: update loading-a-plugin docs --- docs/guide/loading-a-plugin.md | 61 ++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/docs/guide/loading-a-plugin.md b/docs/guide/loading-a-plugin.md index e3c7af2f..33de447e 100644 --- a/docs/guide/loading-a-plugin.md +++ b/docs/guide/loading-a-plugin.md @@ -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,61 @@ if __name__ == "__main__": nonebot.run() ``` +## 从 json 文件中加载插件 + +在 `bot.py` 文件中添加以下行: + +```python{6} +import nonebot + +nonebot.init() + +# 从 plugin.json 加载插件 +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 加载插件 +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"] +``` + +nb-cli 默认使用 `pyproject.toml` 加载插件。 + ## 子插件(嵌套插件) 在插件中同样可以加载子插件,例如如下插件目录结构: From 593d593a629d54eb560eeba49856fa0cb51f36ea Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Sat, 1 May 2021 10:53:18 +0800 Subject: [PATCH 2/2] :memo: update doc --- docs/guide/loading-a-plugin.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/guide/loading-a-plugin.md b/docs/guide/loading-a-plugin.md index 33de447e..4ed55434 100644 --- a/docs/guide/loading-a-plugin.md +++ b/docs/guide/loading-a-plugin.md @@ -95,7 +95,7 @@ import nonebot nonebot.init() # 从 plugin.json 加载插件 -load_from_json("plugin.json") +nonebot.load_from_json("plugin.json") app = nonebot.get_asgi() @@ -122,7 +122,7 @@ import nonebot nonebot.init() # 从 pyproject.toml 加载插件 -load_from_toml("pyproject.toml") +nonebot.load_from_toml("pyproject.toml") app = nonebot.get_asgi() @@ -138,7 +138,9 @@ plugins = ["nonebot_plugin_status", "awesome_bot.plugins.xxx"] plugin_dirs = ["awesome_bot/plugins"] ``` +::: tip nb-cli 默认使用 `pyproject.toml` 加载插件。 +::: ## 子插件(嵌套插件)