2020-09-13 13:01:23 +08:00
|
|
|
---
|
|
|
|
contentSidebar: true
|
|
|
|
sidebarDepth: 0
|
|
|
|
---
|
|
|
|
|
|
|
|
# NoneBot.rule 模块
|
|
|
|
|
|
|
|
## 规则
|
|
|
|
|
2020-09-14 20:48:03 +08:00
|
|
|
每个 `Matcher` 拥有一个 `Rule` ,其中是 **异步** `RuleChecker` 的集合,只有当所有 `RuleChecker` 检查结果为 `True` 时继续运行。
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
:::tip 提示
|
|
|
|
`RuleChecker` 既可以是 async function 也可以是 sync function
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
|
|
## _class_ `Rule`
|
|
|
|
|
|
|
|
基类:`object`
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
`Matcher` 规则类,当事件传递时,在 `Matcher` 运行前进行检查。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **示例**
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
Rule(async_function) & sync_function
|
|
|
|
# 等价于
|
|
|
|
from nonebot.utils import run_sync
|
|
|
|
Rule(async_function, run_sync(sync_function))
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### `__init__(*checkers)`
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
* `*checkers: Callable[[Bot, Event, dict], Awaitable[bool]]`: **异步** RuleChecker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### `checkers`
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
存储 `RuleChecker`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **类型**
|
|
|
|
|
|
|
|
|
|
|
|
* `Set[Callable[[Bot, Event, dict], Awaitable[bool]]]`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### _async_ `__call__(bot, event, state)`
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
检查是否符合所有规则
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
* `bot: Bot`: Bot 对象
|
|
|
|
|
|
|
|
|
|
|
|
* `event: Event`: Event 对象
|
|
|
|
|
|
|
|
|
|
|
|
* `state: dict`: 当前 State
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **返回**
|
|
|
|
|
|
|
|
|
|
|
|
* `bool`
|
2020-09-14 20:48:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## `startswith(msg)`
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
匹配消息开头
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
* `msg: str`: 消息开头字符串
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## `endswith(msg)`
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
匹配消息结尾
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
* `msg: str`: 消息结尾字符串
|