diff --git a/website/docs/advanced/matcher.md b/website/docs/advanced/matcher.md index 635f2a1d3bdb..130b543574a5 100644 --- a/website/docs/advanced/matcher.md +++ b/website/docs/advanced/matcher.md @@ -77,6 +77,10 @@ NoneBot 为四种类型的事件响应器提供了五个基本的辅助函数: ## 内置响应规则 +:::tip +响应规则的使用方法可以参考 [深入 - 响应规则](../appendices/rule.md)。 +::: + ### `startswith` `startswith` 响应规则用于匹配消息纯文本部分的开头是否与指定字符串(或一系列字符串)相同。可选参数 `ignorecase` 用于指定是否忽略大小写,默认为 `False`。 diff --git a/website/docs/appendices/config.mdx b/website/docs/appendices/config.mdx index 59d8122f45c7..22429037ff2d 100644 --- a/website/docs/appendices/config.mdx +++ b/website/docs/appendices/config.mdx @@ -178,7 +178,7 @@ nonebot.init(_env_file=".env.dev") 这将忽略在 `.env` 文件或环境变量中指定的 `ENVIRONMENT` 配置项。 -## 读取配置项 +## 读取全局配置项 NoneBot 的全局配置对象可以通过 `driver` 获取,如: @@ -198,7 +198,7 @@ superusers = config.superusers ## 插件配置 -在一个涉及大量配置项的项目中,通过直接读取配置项的方式显然并不高效。同时,由于额外的全局配置项没有预先定义,开发时编辑器将无法提示字段与类型,并且运行时没有对配置项直接进行合法性检查。那么就需要一种方式来规范定义插件配置项。 +在一个涉及大量配置项的项目中,通过直接读取全局配置项的方式显然并不高效。同时,由于额外的全局配置项没有预先定义,开发时编辑器将无法提示字段与类型,并且运行时没有对配置项直接进行合法性检查。那么就需要一种方式来规范定义插件配置项。 在 NoneBot 中,我们使用强大高效的 `pydantic` 来定义配置模型,这个模型可以被用于配置的读取和类型检查等。例如在 `weather` 插件目录中新建 `config.py` 来定义一个模型: @@ -220,7 +220,7 @@ class Config(BaseModel): 在 `config.py` 中,我们定义了一个 `Config` 类,它继承自 `pydantic.BaseModel`,并定义了一些配置项。在 `Config` 类中,我们还定义了一个 `check_priority` 方法,它用于检查 `weather_command_priority` 配置项的合法性。更多关于 `pydantic` 的编写方式,可以参考 [pydantic 官方文档](https://docs.pydantic.dev/)。 -在定义好配置模型后,我们可以在插件加载时获取全局配置,导入插件自身的配置模型并使用: +在定义好配置模型后,我们可以在插件加载时通过配置模型获取插件配置: ```python {5,11} title=weather/__init__.py from nonebot import get_plugin_config diff --git a/website/docs/appendices/permission.mdx b/website/docs/appendices/permission.mdx index eac451839753..4c03acb89d61 100644 --- a/website/docs/appendices/permission.mdx +++ b/website/docs/appendices/permission.mdx @@ -29,8 +29,9 @@ import Messenger from "@site/src/components/Messenger"; 例如,我们可以在 `weather` 插件中添加一个超级用户可用的指令: -```python {2,8} title=weather/__init__.py +```python {3,9} title=weather/__init__.py from typing import Tuple +from nonebot.params import Command from nonebot.permission import SUPERUSER manage = on_command( diff --git a/website/docs/appendices/rule.md b/website/docs/appendices/rule.md index a4e55a87dfdf..7b9d7a244fad 100644 --- a/website/docs/appendices/rule.md +++ b/website/docs/appendices/rule.md @@ -20,7 +20,11 @@ options: `RuleChecker` 是一个返回值为 `bool` 类型的依赖函数,即 `RuleChecker` 支持依赖注入。我们可以根据上一节中添加的[配置项](./config.mdx#插件配置),在 `weather` 插件目录中编写一个响应规则: -```python {3,4} title=weather/__init__.py +```python {7,8} title=weather/__init__.py +from nonebot import get_plugin_config + +from .config import Config + plugin_config = get_plugin_config(Config) async def is_enable() -> bool: @@ -54,8 +58,11 @@ weather = on_command("天气", rule=rule) 在定义响应规则时,我们可以将规则进行细分,来更好地复用规则。而在使用时,我们需要合并多个规则。除了使用 `Rule` 对象来组合多个 `RuleChecker` 外,我们还可以对 `Rule` 对象进行合并。在原 `weather` 插件中,我们可以将 `rule=to_me()` 与 `rule=is_enable` 使用 `&` 运算符合并: -```python {10} title=weather/__init__.py +```python {13} title=weather/__init__.py from nonebot.rule import to_me +from nonebot import get_plugin_config + +from .config import Config plugin_config = get_plugin_config(Config) @@ -66,7 +73,7 @@ weather = on_command( "天气", rule=to_me() & is_enable, aliases={"weather", "查天气"}, - priority=plugin_config.weather_command_priority + priority=plugin_config.weather_command_priority, block=True, ) ``` diff --git a/website/docs/tutorial/application.md b/website/docs/tutorial/application.md index 2faa8b1a356c..3d6928917326 100644 --- a/website/docs/tutorial/application.md +++ b/website/docs/tutorial/application.md @@ -107,4 +107,4 @@ if __name__ == "__main__": python bot.py ``` -如果你后续使用了 `nb-cli` ,你仍可以使用 `nb run` 命令来运行机器人,`nb-cli` 会自动检测入口文件 `bot.py` 是否存在并运行。 +如果你后续使用了 `nb-cli` ,你仍可以使用 `nb run` 命令来运行机器人,`nb-cli` 会自动检测入口文件 `bot.py` 是否存在并运行。同时,你也可以使用 `nb run --reload` 来自动检测代码的更改并自动重新运行入口文件。 diff --git a/website/docs/tutorial/matcher.md b/website/docs/tutorial/matcher.md index 72ca52b8faf5..8d3bf3922b2b 100644 --- a/website/docs/tutorial/matcher.md +++ b/website/docs/tutorial/matcher.md @@ -51,8 +51,8 @@ from nonebot.rule import to_me weather = on_command("天气", rule=to_me(), aliases={"weather", "查天气"}, priority=10, block=True) ``` -这样,我们就获得了一个可以响应 `天气`、`weather`、`查天气` 三个命令,需要私聊或 `@bot` 时才会响应,优先级为 10 ,阻断事件传播的事件响应器了。这些内容的意义和使用方法将会在后续的章节中一一介绍。 +这样,我们就获得了一个可以响应 `天气`、`weather`、`查天气` 三个命令的响应规则,需要私聊或 `@bot` 时才会响应,优先级为 10(越小越优先),阻断事件向后续优先级传播的事件响应器了。这些内容的意义和使用方法将会在后续的章节中一一介绍。 :::tip 提示 -需要注意的是,不同的辅助函数有不同的可选参数,在使用之前可以参考[事件响应器进阶](../advanced/matcher.md)或编辑器的提示。 +需要注意的是,不同的辅助函数有不同的可选参数,在使用之前可以参考[事件响应器进阶 - 基本辅助函数](../advanced/matcher.md#基本辅助函数)或 [API 文档](../api/plugin/on.md#on)。 :::