mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 19:11:00 +00:00
📝 Docs: 修改文档示例代码与部分表述 (#2797)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Komorebi <110453675+KomoriDev@users.noreply.github.com> Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
This commit is contained in:
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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,
|
||||
)
|
||||
```
|
||||
|
Reference in New Issue
Block a user