📝 add configuration and matcher operation

This commit is contained in:
yanyongyu
2022-01-24 20:32:46 +08:00
parent 48b507e272
commit c8369f599f
4 changed files with 245 additions and 3 deletions

View File

@ -119,6 +119,8 @@ matcher = on_message(
### Bot
获取当前事件的 Bot 对象。
```python {7-9}
from typing import Union
@ -133,6 +135,8 @@ async def _(bot): ... # 兼容性处理
### Event
获取当前事件。
```python {6-8}
from typing import Union
@ -146,6 +150,8 @@ async def _(event): ... # 兼容性处理
### EventType
获取当前事件的类型。
```python {3}
from nonebot.params import EventType
@ -154,6 +160,8 @@ async def _(foo: str = EventType()): ...
### EventMessage
获取当前事件的消息。
```python {4}
from nonebot.adapters import Message
from nonebot.params import EventMessage
@ -163,6 +171,8 @@ async def _(foo: str = EventMessage()): ...
### EventPlainText
获取当前事件的消息纯文本部分。
```python {3}
from nonebot.params import EventPlainText
@ -171,6 +181,8 @@ async def _(foo: str = EventPlainText()): ...
### EventToMe
获取当前事件是否与机器人相关。
```python {3}
from nonebot.params import EventToMe
@ -179,15 +191,18 @@ async def _(foo: bool = EventToMe()): ...
### State
获取当前事件处理上下文状态。
```python {4}
from nonebot.params import State
from nonebot.typing import T_State
async def _(foo: T_State = State()): ...
async def _(foo: T_State): ...
```
### Command
获取当前命令型消息的元组形式命令名。
```python {7}
from nonebot import on_command
from nonebot.params import Command
@ -198,8 +213,32 @@ matcher = on_command("cmd")
async def _(foo: Tuple[str, ...] = Command()): ...
```
:::tip 提示
命令详情只能在首次接收到命令型消息时获取,如果在事件处理后续流程中获取,则会获取到不同的值。
:::
### RawCommand
获取当前命令型消息的文本形式命令名。
```python {7}
from nonebot import on_command
from nonebot.params import RawCommand
matcher = on_command("cmd")
@matcher.handle()
async def _(foo: str = RawCommand()): ...
```
:::tip 提示
命令详情只能在首次接收到命令型消息时获取,如果在事件处理后续流程中获取,则会获取到不同的值。
:::
### CommandArg
获取命令型消息命令后跟随的参数。
```python {8}
from nonebot import on_command
from nonebot.adapters import Message
@ -211,8 +250,20 @@ matcher = on_command("cmd")
async def _(foo: Message = CommandArg()): ...
```
:::tip 提示
命令详情只能在首次接收到命令型消息时获取,如果在事件处理后续流程中获取,则会获取到不同的值。
:::
### ShellCommandArgs
获取 shell 命令解析后的参数。
:::tip 提示
如果参数解析失败,则为 {ref}`nonebot.exception.ParserExit` 异常,并携带错误码与错误信息。
由于 `ArgumentParser` 在解析到 `--help` 参数时也会抛出异常,这种情况下错误码为 `0` 且错误信息即为帮助信息。
:::
```python {7}
from nonebot import on_shell_command
from nonebot.params import ShellCommandArgs
@ -225,6 +276,8 @@ async def _(foo: Dict[str, Any] = ShellCommandArgs()): ...
### ShellCommandArgv
获取 shell 命令解析前的参数列表。
```python {7}
from nonebot import on_shell_command
from nonebot.params import ShellCommandArgs
@ -237,6 +290,8 @@ async def _(foo: List[str] = ShellCommandArgv()): ...
### RegexMatched
获取正则匹配结果。
```python {7}
from nonebot import on_regex
from nonebot.params import RegexMatched
@ -249,6 +304,8 @@ async def _(foo: str = RegexMatched()): ...
### RegexGroup
获取正则匹配结果的 group 元组。
```python {7}
from nonebot import on_regex
from nonebot.params import RegexGroup
@ -261,6 +318,8 @@ async def _(foo: Tuple[Any, ...] = RegexGroup()): ...
### RegexDict
获取正则匹配结果的 group 字典。
```python {7}
from nonebot import on_regex
from nonebot.params import RegexDict
@ -273,6 +332,8 @@ async def _(foo: Dict[str, Any] = RegexDict()): ...
### Matcher
获取当前事件响应器实例。
```python {7}
from nonebot import on_message
from nonebot.matcher import Matcher
@ -285,6 +346,8 @@ async def _(matcher: Matcher): ...
### Received
获取某次 `receive` 接收的事件。
```python {8}
from nonebot import on_message
from nonebot.adapters import Event
@ -298,6 +361,8 @@ async def _(foo: Event = Received("id")): ...
### LastReceived
获取最近一次 `receive` 接收的事件。
```python {8}
from nonebot import on_message
from nonebot.adapters import Event
@ -311,6 +376,8 @@ async def _(foo: Event = LastReceived()): ...
### Arg
获取某次 `got` 接收的参数。
```python {8-9}
from nonebot.params import Arg
from nonebot import on_message
@ -325,6 +392,8 @@ async def _(foo: Message = Arg("key")): ...
### ArgStr
获取某次 `got` 接收的参数,并转换为字符串。
```python {7-8}
from nonebot import on_message
from nonebot.params import ArgStr
@ -338,6 +407,8 @@ async def _(foo: str = ArgStr("key")): ...
### ArgPlainText
获取某次 `got` 接收的参数的纯文本部分。
```python {7-8}
from nonebot import on_message
from nonebot.params import ArgPlainText
@ -351,6 +422,8 @@ async def _(foo: str = ArgPlainText("key")): ...
### Exception
获取事件响应器运行中抛出的异常。
```python {4}
from nonebot.message import run_postprocessor
@ -360,6 +433,8 @@ async def _(e: Exception): ...
### Default
带有默认值的参数,便于复用依赖。
```python {1}
async def _(foo="bar"): ...
```