mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-27 16:21:28 +00:00
📝 update some advanced docs
This commit is contained in:
@ -26,7 +26,7 @@ options:
|
||||
|
||||
```python {7-9}
|
||||
from nonebot.log import logger
|
||||
from nonebot.dependencies import Depends
|
||||
from nonebot.params import Depends
|
||||
from nonebot import on_command, on_message
|
||||
|
||||
test = on_command("123")
|
||||
@ -52,7 +52,7 @@ async def _(x: dict = Depends(depend)):
|
||||
|
||||
```python {2}
|
||||
from nonebot.log import logger
|
||||
from nonebot.dependencies import Depends
|
||||
from nonebot.params import Depends
|
||||
from nonebot import on_command, on_message
|
||||
|
||||
test = on_command("123")
|
||||
@ -72,7 +72,7 @@ async def _(x: dict = Depends(depend)):
|
||||
|
||||
```python {12}
|
||||
from nonebot.log import logger
|
||||
from nonebot.dependencies import Depends
|
||||
from nonebot.params import Depends
|
||||
from nonebot import on_command, on_message
|
||||
|
||||
test = on_command("123")
|
||||
|
@ -10,9 +10,9 @@ options:
|
||||
|
||||
# 事件处理函数重载
|
||||
|
||||
当我们在编写 `nonebot2` 应用时,常常会遇到这样一个问题:该怎么让同一类型的不同事件执行不同的响应逻辑?又或者如何让不同的 `adapter` 针对同一类型的事件作出不同响应?
|
||||
当我们在编写 `nonebot2` 应用时,常常会遇到这样一个问题:该怎么让同一类型的不同事件执行不同的响应逻辑?又或者如何让不同的 `bot` 针对同一类型的事件作出不同响应?
|
||||
|
||||
针对这个问题, `nonebot2` 提供一个便捷而高效的解决方案:事件处理函数重载机制。简单地说,`handler` (事件处理函数) 会根据其参数的 `type hints` ([PEP484 类型标注](https://www.python.org/dev/peps/pep-0484/)) 来对相对应的 `adapter` 和 `Event` 进行响应,并且会忽略不符合其参数类型标注的情况。
|
||||
针对这个问题, `nonebot2` 提供一个便捷而高效的解决方案:事件处理函数重载机制。简单地说,`handler` (事件处理函数) 会根据其参数的 `type hints` ([PEP484 类型标注](https://www.python.org/dev/peps/pep-0484/)) 来对相对应的 `bot` 和 `Event` 进行响应,并且会忽略不符合其参数类型标注的情况。
|
||||
|
||||
<!-- 必须要注意的是,该机制利用了 `inspect` 标准库获取到了事件处理函数的 `singnature` (签名) ,进一步获取到参数名称和类型标注。故而,我们在编写 `handler` 时,参数的名称和类型标注必须要符合 `T_Handler` 规定,详情可以参看 **指南** 中的[事件处理](../../guide/creating-a-handler)。 -->
|
||||
|
||||
@ -22,7 +22,7 @@ options:
|
||||
|
||||
:::
|
||||
|
||||
下面,我们会以 `CQHTTP` 中的 `群聊消息事件` 和 `私聊消息事件` 为例,对该机制的应用进行简单的介绍。
|
||||
下面,我们会以 `onebot` 中的 `群聊消息事件` 和 `私聊消息事件` 为例,对该机制的应用进行简单的介绍。
|
||||
|
||||
## 一个例子
|
||||
|
||||
@ -30,7 +30,7 @@ options:
|
||||
|
||||
```python
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent
|
||||
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, PrivateMessageEvent
|
||||
```
|
||||
|
||||
之后,我们可以注册一个 `Matcher` 来响应 `消息事件` 。
|
||||
|
Reference in New Issue
Block a user