Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: 修改文档示例代码与部分表述 #2797

Merged
merged 12 commits into from
Sep 29, 2024
4 changes: 4 additions & 0 deletions website/docs/advanced/matcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ NoneBot 为四种类型的事件响应器提供了五个基本的辅助函数:

## 内置响应规则

:::tip
响应规则的使用方法可以参考 [深入 - 响应规则](../appendices/rule.md)。
:::

### `startswith`

`startswith` 响应规则用于匹配消息纯文本部分的开头是否与指定字符串(或一系列字符串)相同。可选参数 `ignorecase` 用于指定是否忽略大小写,默认为 `False`。
Expand Down
6 changes: 3 additions & 3 deletions website/docs/appendices/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ nonebot.init(_env_file=".env.dev")

这将忽略在 `.env` 文件或环境变量中指定的 `ENVIRONMENT` 配置项。

## 读取配置项
## 读取全局配置项

NoneBot 的全局配置对象可以通过 `driver` 获取,如:

Expand All @@ -196,7 +196,7 @@ superusers = config.superusers

## 插件配置

在一个涉及大量配置项的项目中,通过直接读取配置项的方式显然并不高效。同时,由于额外的全局配置项没有预先定义,开发时编辑器将无法提示字段与类型,并且运行时没有对配置项直接进行合法性检查。那么就需要一种方式来规范定义插件配置项。
在一个涉及大量配置项的项目中,通过直接读取全局配置项的方式显然并不高效。同时,由于额外的全局配置项没有预先定义,开发时编辑器将无法提示字段与类型,并且运行时没有对配置项直接进行合法性检查。那么就需要一种方式来规范定义插件配置项。

在 NoneBot 中,我们使用强大高效的 `pydantic` 来定义配置模型,这个模型可以被用于配置的读取和类型检查等。例如在 `weather` 插件目录中新建 `config.py` 来定义一个模型:

Expand All @@ -218,7 +218,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
Expand Down
3 changes: 2 additions & 1 deletion website/docs/appendices/permission.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 10 additions & 3 deletions website/docs/appendices/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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,
)
```
Expand Down
2 changes: 1 addition & 1 deletion website/docs/tutorial/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 来自动检测代码的更改并自动重新运行入口文件。
4 changes: 2 additions & 2 deletions website/docs/tutorial/matcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
:::