mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-27 16:21:28 +00:00
📝 add di docs
This commit is contained in:
108
website/docs/advanced/handler/dependency-injection.md
Normal file
108
website/docs/advanced/handler/dependency-injection.md
Normal file
@ -0,0 +1,108 @@
|
||||
---
|
||||
sidebar_position: 3
|
||||
|
||||
options:
|
||||
menu:
|
||||
weight: 62
|
||||
category: advanced
|
||||
---
|
||||
|
||||
# 依赖注入
|
||||
|
||||
受 [`FastApi`](https://fastapi.tiangolo.com/tutorial/dependencies/) 启发,NoneBot 同样编写了一个简易的依赖注入模块,使得开发者可以通过事件处理函数参数的类型标注来自动注入依赖。
|
||||
|
||||
## 什么是依赖注入?
|
||||
|
||||
~~交给 mix 了~~
|
||||
|
||||
## 使用依赖注入
|
||||
|
||||
以下通过一个简单的例子来说明依赖注入的使用方法:
|
||||
|
||||
### 编写依赖函数
|
||||
|
||||
这里我们编写了一个简单的函数 `depend` 作为依赖函数
|
||||
|
||||
```python {7-9}
|
||||
from nonebot.log import logger
|
||||
from nonebot.dependencies import Depends
|
||||
from nonebot import on_command, on_message
|
||||
|
||||
test = on_command("123")
|
||||
|
||||
def depend(state: dict):
|
||||
# do something with state
|
||||
return {**state, "depend": "depend"}
|
||||
|
||||
@test.handle()
|
||||
async def _(x: dict = Depends(depend)):
|
||||
print(x)
|
||||
```
|
||||
|
||||
它和普通的事件处理函数并无区别,同样可以接受 `bot`, `event` 等参数,你可以把它当作一个普通的事件处理函数但是去除了装饰器(并没有使用 `matcher.handle()` 等来进行装饰),并且它可以返回任何类型的值。
|
||||
|
||||
在这个例子中,依赖函数接受一个参数:
|
||||
|
||||
- `state: dict`:当前事件处理状态字典。
|
||||
|
||||
并且返回了一个 `state` 的复制以及一个附加的键值 `depend` 。
|
||||
|
||||
### Import `Depends`
|
||||
|
||||
```python {2}
|
||||
from nonebot.log import logger
|
||||
from nonebot.dependencies import Depends
|
||||
from nonebot import on_command, on_message
|
||||
|
||||
test = on_command("123")
|
||||
|
||||
def depend(state: dict):
|
||||
# do something with state
|
||||
return {**state, "depend": "depend"}
|
||||
|
||||
@test.handle()
|
||||
async def _(x: dict = Depends(depend)):
|
||||
print(x)
|
||||
```
|
||||
|
||||
### 在事件处理函数里声明依赖函数
|
||||
|
||||
与 FastAPI 类似,你可以在函数中添加一个新的参数,并且使用 `Depends` 来声明它的依赖。
|
||||
|
||||
```python {12}
|
||||
from nonebot.log import logger
|
||||
from nonebot.dependencies import Depends
|
||||
from nonebot import on_command, on_message
|
||||
|
||||
test = on_command("123")
|
||||
|
||||
def depend(state: dict):
|
||||
# do something with state
|
||||
return {**state, "depend": "depend"}
|
||||
|
||||
@test.handle()
|
||||
async def _(x: dict = Depends(depend)):
|
||||
print(x)
|
||||
```
|
||||
|
||||
你需要给 `Depends` 指定一个依赖函数,这个依赖函数的返回值会被作为 `x` 的值。
|
||||
|
||||
`Depends` 的首个参数即是依赖函数,或者其他 `Callable` 对象,在之后会对更多形式的依赖对象进行介绍。
|
||||
|
||||
:::tip
|
||||
参数 `x` 的类型标注并不会影响事件处理函数的运行,类型检查并不会对依赖函数的返回值以及类型标注进行检查。
|
||||
:::
|
||||
|
||||
当接收到事件时,NoneBot 会进行以下处理:
|
||||
|
||||
1. 查询缓存,如果缓存中有相应的值,则直接返回。
|
||||
2. 准备依赖函数所需要的参数。
|
||||
3. 调用依赖函数并获得返回值。
|
||||
4. 将返回值存入缓存。
|
||||
5. 将返回值作为事件处理函数中的参数值传入。
|
||||
|
||||
## 依赖缓存
|
||||
|
||||
## Class 作为依赖
|
||||
|
||||
## Generator 作为依赖
|
73
website/docs/advanced/handler/overload.md
Normal file
73
website/docs/advanced/handler/overload.md
Normal file
@ -0,0 +1,73 @@
|
||||
---
|
||||
sidebar_position: 2
|
||||
|
||||
options:
|
||||
menu:
|
||||
weight: 61
|
||||
category: advanced
|
||||
---
|
||||
|
||||
# 事件处理函数重载
|
||||
|
||||
当我们在编写 `nonebot2` 应用时,常常会遇到这样一个问题:该怎么让同一类型的不同事件执行不同的响应逻辑?又或者如何让不同的 `adapter` 针对同一类型的事件作出不同响应?
|
||||
|
||||
针对这个问题, `nonebot2` 提供一个便捷而高效的解决方案:事件处理函数重载机制。简单地说,`handler` (事件处理函数) 会根据其参数的 `type hints` ([PEP484 类型标注](https://www.python.org/dev/peps/pep-0484/)) 来对相对应的 `adapter` 和 `Event` 进行响应,并且会忽略不符合其参数类型标注的情况。
|
||||
|
||||
必须要注意的是,该机制利用了 `inspect` 标准库获取到了事件处理函数的 `singnature` (签名) ,进一步获取到参数名称和类型标注。故而,我们在编写 `handler` 时,参数的名称和类型标注必须要符合 `T_Handler` 规定,详情可以参看 **指南** 中的[事件处理](../guide/creating-a-handler)。
|
||||
|
||||
:::tip 提示
|
||||
|
||||
如果想了解更多关于 `inspect` 标准库的信息,可以查看[官方文档](https://docs.python.org/zh-cn/3.9/library/inspect.html)。
|
||||
|
||||
:::
|
||||
|
||||
下面,我们会以 `CQHTTP` 中的 `群聊消息事件` 和 `私聊消息事件` 为例,对该机制的应用进行简单的介绍。
|
||||
|
||||
## 一个例子
|
||||
|
||||
首先,我们需要导入需要的方法、类型。
|
||||
|
||||
```python
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent
|
||||
```
|
||||
|
||||
之后,我们可以注册一个 `Matcher` 来响应 `消息事件` 。
|
||||
|
||||
```python
|
||||
matcher = on_command("testoverload")
|
||||
```
|
||||
|
||||
最后, 我们编写不同的 `handler` 并编写不同的类型标注来实现事件处理函数重载:
|
||||
|
||||
```python
|
||||
@matcher.handle()
|
||||
async def _(bot: Bot, event: GroupMessageEvent):
|
||||
await matcher.send("群聊消息事件响应成功!")
|
||||
|
||||
|
||||
@matcher.handle()
|
||||
async def _(bot: Bot, event: PrivateMessageEvent):
|
||||
await matcher.send("私聊消息事件响应成功!")
|
||||
```
|
||||
|
||||
此时,我们可以在群聊或私聊中对我们的机器人发送 `testoverload` ,它会在不同的场景做出不同的应答。
|
||||
|
||||
这样一个简单的事件处理函数重载就完成了。
|
||||
|
||||
## 进阶
|
||||
|
||||
事件处理函数重载机制同样支持被 `matcher.got` 等装饰器装饰的函数。 例如:
|
||||
|
||||
```python
|
||||
@matcher.got("key1", prompt="群事件提问")
|
||||
async def _(bot: Bot, event: GroupMessageEvent):
|
||||
await matcher.send("群聊消息事件响应成功!")
|
||||
|
||||
|
||||
@matcher.got("key2", prompt="私聊事件提问")
|
||||
async def _(bot: Bot, event: PrivateMessageEvent):
|
||||
await matcher.send("私聊消息事件响应成功!")
|
||||
```
|
||||
|
||||
只有触发事件符合的函数才会触发装饰器。
|
10
website/docs/advanced/handler/sync-support.md
Normal file
10
website/docs/advanced/handler/sync-support.md
Normal file
@ -0,0 +1,10 @@
|
||||
---
|
||||
sidebar_position: 1
|
||||
|
||||
options:
|
||||
menu:
|
||||
weight: 60
|
||||
category: advanced
|
||||
---
|
||||
|
||||
# 同步支持
|
Reference in New Issue
Block a user