2020-09-13 13:01:23 +08:00
|
|
|
|
---
|
|
|
|
|
contentSidebar: true
|
|
|
|
|
sidebarDepth: 0
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# NoneBot.rule 模块
|
|
|
|
|
|
|
|
|
|
## 规则
|
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
每个事件响应器 `Matcher` 拥有一个匹配规则 `Rule` ,其中是 **异步** `RuleChecker` 的集合,只有当所有 `RuleChecker` 检查结果为 `True` 时继续运行。
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
:::tip 提示
|
2020-10-06 02:08:48 +08:00
|
|
|
|
`RuleChecker` 既可以是 async function 也可以是 sync function,但在最终会被 `nonebot.utils.run_sync` 转换为 async function
|
2020-09-13 13:01:23 +08:00
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## _class_ `Rule`
|
|
|
|
|
|
|
|
|
|
基类:`object`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
`Matcher` 规则类,当事件传递时,在 `Matcher` 运行前进行检查。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **示例**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
Rule(async_function) & sync_function
|
|
|
|
|
# 等价于
|
|
|
|
|
from nonebot.utils import run_sync
|
|
|
|
|
Rule(async_function, run_sync(sync_function))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 12:37:46 +08:00
|
|
|
|
### `__init__(*checkers, dependency_overrides_provider=None)`
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:20:20 +08:00
|
|
|
|
* `*checkers: T_RuleChecker`: RuleChecker
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### `checkers`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
存储 `RuleChecker`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **类型**
|
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:20:20 +08:00
|
|
|
|
* `Set[Handler]`
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:20:20 +08:00
|
|
|
|
### _async_ `__call__(bot, event, state, stack=None, dependency_cache=None)`
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
检查是否符合所有规则
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `bot: Bot`: Bot 对象
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `event: Event`: Event 对象
|
|
|
|
|
|
|
|
|
|
|
2020-12-19 15:31:14 +08:00
|
|
|
|
* `state: T_State`: 当前 State
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-21 12:37:46 +08:00
|
|
|
|
* `stack: Optional[AsyncExitStack]`: 异步上下文栈
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `dependency_cache: Optional[Dict[Callable[..., Any], Any]]`: 依赖缓存
|
|
|
|
|
|
|
|
|
|
|
2020-09-13 13:01:23 +08:00
|
|
|
|
|
|
|
|
|
* **返回**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `bool`
|
2020-09-14 20:48:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-05 13:52:25 +08:00
|
|
|
|
## `startswith(msg, ignorecase=False)`
|
2020-09-14 20:48:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
匹配消息开头
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `msg: str`: 消息开头字符串
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-05 13:52:25 +08:00
|
|
|
|
## `endswith(msg, ignorecase=False)`
|
2020-09-14 20:48:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
匹配消息结尾
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `msg: str`: 消息结尾字符串
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-30 16:26:04 +08:00
|
|
|
|
## `keyword(*keywords)`
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
匹配消息关键词
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
2020-10-30 16:26:04 +08:00
|
|
|
|
* `*keywords: str`: 关键词
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-22 22:08:19 +08:00
|
|
|
|
## `command(*cmds)`
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
命令形式匹配,根据配置里提供的 `command_start`, `command_sep` 判断消息是否为命令。
|
|
|
|
|
|
2020-10-28 13:17:41 +08:00
|
|
|
|
可以通过 `state["_prefix"]["command"]` 获取匹配成功的命令(例:`("test",)`),通过 `state["_prefix"]["raw_command"]` 获取匹配成功的原始命令文本(例:`"/test"`)。
|
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
2020-10-22 22:08:19 +08:00
|
|
|
|
* `*cmds: Union[str, Tuple[str, ...]]`: 命令内容
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **示例**
|
|
|
|
|
|
|
|
|
|
使用默认 `command_start`, `command_sep` 配置
|
|
|
|
|
|
|
|
|
|
命令 `("test",)` 可以匹配:`/test` 开头的消息
|
|
|
|
|
命令 `("test", "sub")` 可以匹配”`/test.sub` 开头的消息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:::tip 提示
|
|
|
|
|
命令内容与后续消息间无需空格!
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
|
2021-02-02 12:16:37 +08:00
|
|
|
|
## _class_ `ArgumentParser`
|
|
|
|
|
|
|
|
|
|
基类:`argparse.ArgumentParser`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
`shell_like` 命令参数解析器,解析出错时不会退出程序。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
## `shell_command(*cmds, parser=None)`
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
|
|
|
|
支持 `shell_like` 解析参数的命令形式匹配,根据配置里提供的 `command_start`, `command_sep` 判断消息是否为命令。
|
|
|
|
|
|
|
|
|
|
可以通过 `state["_prefix"]["command"]` 获取匹配成功的命令(例:`("test",)`),通过 `state["_prefix"]["raw_command"]` 获取匹配成功的原始命令文本(例:`"/test"`)。
|
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
可以通过 `state["argv"]` 获取用户输入的原始参数列表
|
|
|
|
|
|
|
|
|
|
添加 `parser` 参数后, 可以自动处理消息并将结果保存在 `state["args"]` 中。
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
* `*cmds: Union[str, Tuple[str, ...]]`: 命令内容
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
* `parser: Optional[ArgumentParser]`: `nonebot.rule.ArgumentParser` 对象
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **示例**
|
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
使用默认 `command_start`, `command_sep` 配置,更多示例参考 `argparse` 标准库文档。
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
```python
|
|
|
|
|
from nonebot.rule import ArgumentParser
|
|
|
|
|
|
|
|
|
|
parser = ArgumentParser()
|
2021-02-02 12:39:23 +08:00
|
|
|
|
parser.add_argument("-a", action="store_true")
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
2021-02-02 12:06:57 +08:00
|
|
|
|
rule = shell_command("ls", parser=parser)
|
|
|
|
|
```
|
2021-02-02 01:22:53 +08:00
|
|
|
|
|
|
|
|
|
:::tip 提示
|
|
|
|
|
命令内容与后续消息间无需空格!
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
## `regex(regex, flags=0)`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2020-10-30 16:26:04 +08:00
|
|
|
|
根据正则表达式进行匹配。
|
|
|
|
|
|
2021-02-01 11:49:26 +08:00
|
|
|
|
可以通过 `state["_matched"]` `state["_matched_groups"]` `state["_matched_dict"]`
|
|
|
|
|
获取正则表达式匹配成功的文本。
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `regex: str`: 正则表达式
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `flags: Union[int, re.RegexFlag]`: 正则标志
|
|
|
|
|
|
|
|
|
|
|
2020-10-18 15:04:45 +08:00
|
|
|
|
:::tip 提示
|
|
|
|
|
正则表达式匹配使用 search 而非 match,如需从头匹配请使用 `r"^xxx"` 来确保匹配开头
|
|
|
|
|
:::
|
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
## `to_me()`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **说明**
|
|
|
|
|
|
2020-12-31 17:58:09 +08:00
|
|
|
|
通过 `event.is_tome()` 判断事件是否与机器人有关
|
2020-10-06 02:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* **参数**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* 无
|