2021-12-01 16:28:55 +08:00
|
|
|
|
---
|
|
|
|
|
options:
|
|
|
|
|
menu:
|
|
|
|
|
weight: 40
|
2021-12-02 03:17:10 +08:00
|
|
|
|
category: advanced
|
2021-12-01 16:28:55 +08:00
|
|
|
|
---
|
|
|
|
|
|
2021-02-02 18:20:51 +08:00
|
|
|
|
# 钩子函数
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
[钩子编程](https://zh.wikipedia.org/wiki/%E9%92%A9%E5%AD%90%E7%BC%96%E7%A8%8B)
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
|
|
|
|
> 钩子编程(hooking),也称作“挂钩”,是计算机程序设计术语,指通过拦截软件模块间的函数调用、消息传递、事件传递来修改或扩展操作系统、应用程序或其他软件组件的行为的各种技术。处理被拦截的函数调用、事件、消息的代码,被称为钩子(hook)。
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
在 NoneBot2 中有一系列预定义的钩子函数,分为两类:**全局钩子函数**和**事件钩子函数**,这些钩子函数可以用装饰器的形式来使用。
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
2021-03-24 19:52:47 +08:00
|
|
|
|
## 全局钩子函数
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
全局钩子函数是指 NoneBot2 针对其本身运行过程的钩子函数。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这些钩子函数是由其后端驱动 `Driver` 来运行的,故需要先获得全局 `Driver` 对象:
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot import get_driver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
driver=get_driver()
|
|
|
|
|
```
|
|
|
|
|
|
2021-04-01 20:27:27 +08:00
|
|
|
|
共分为六种函数:
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
### 启动准备
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 NoneBot2 启动时运行。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@driver.on_startup
|
|
|
|
|
async def do_something():
|
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 终止处理
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 NoneBot2 终止时运行。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@driver.on_shutdown
|
|
|
|
|
async def do_something():
|
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
### Bot 连接处理
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 `Bot` 通过 websocket 连接到 NoneBot2 时运行。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@driver.on_bot_connect
|
|
|
|
|
async def do_something(bot: Bot):
|
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### bot 断开处理
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 `Bot` 断开与 NoneBot2 的 websocket 连接时运行。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@driver.on_bot_disconnect
|
|
|
|
|
async def do_something(bot: Bot):
|
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-31 21:20:07 +08:00
|
|
|
|
### bot api 调用钩子
|
|
|
|
|
|
|
|
|
|
这个钩子函数会在 `Bot` 调用 API 时运行。
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.adapters import Bot
|
|
|
|
|
|
|
|
|
|
@Bot.on_calling_api
|
|
|
|
|
async def handle_api_call(bot: Bot, api: str, data: Dict[str, Any]):
|
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2021-04-01 20:27:27 +08:00
|
|
|
|
### bot api 调用后钩子
|
|
|
|
|
|
|
|
|
|
这个钩子函数会在 `Bot` 调用 API 后运行。
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.adapters import Bot
|
|
|
|
|
|
|
|
|
|
@Bot.on_called_api
|
|
|
|
|
async def handle_api_result(bot: Bot, exception: Optional[Exception], api: str, data: Dict[str, Any], result: Any):
|
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2022-02-04 15:12:02 +08:00
|
|
|
|
## 事件钩子函数
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
2022-02-14 17:26:55 +08:00
|
|
|
|
这些钩子函数指的是影响 NoneBot2 进行**事件处理**的函数, 这些函数可以认为跟普通的事件处理函数一样,接受相应的参数。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
:::tip 提示
|
2022-02-05 19:46:33 +08:00
|
|
|
|
关于**事件处理**的流程,可以在[这里](./README.md)查阅。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
:::
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
:::warning
|
2021-03-24 20:04:48 +08:00
|
|
|
|
|
|
|
|
|
1.在事件处理钩子函数中,与 `matcher` 运行状态相关的函数将不可用,如 `matcher.finish()`
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
|
|
|
|
2.如果需要在事件处理钩子函数中打断整个对话的执行,请参考以下范例:
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.exception import IgnoredException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@event_preprocessor
|
2022-02-14 17:26:55 +08:00
|
|
|
|
async def do_something():
|
2021-02-08 10:26:39 +08:00
|
|
|
|
raise IgnoredException("reason")
|
|
|
|
|
```
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
2021-02-08 10:26:39 +08:00
|
|
|
|
:::
|
|
|
|
|
|
2021-03-24 19:52:47 +08:00
|
|
|
|
共分为四种函数:
|
|
|
|
|
|
|
|
|
|
### 事件预处理
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 `Event` 上报到 NoneBot2 时运行
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.message import event_preprocessor
|
|
|
|
|
|
|
|
|
|
@event_preprocessor
|
2022-02-14 17:26:55 +08:00
|
|
|
|
async def do_something():
|
2021-02-08 10:26:39 +08:00
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-24 19:52:47 +08:00
|
|
|
|
### 事件后处理
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 NoneBot2 处理 `Event` 后运行
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.message import event_postprocessor
|
|
|
|
|
|
|
|
|
|
@event_postprocessor
|
2022-02-14 17:26:55 +08:00
|
|
|
|
async def do_something():
|
2021-02-08 10:26:39 +08:00
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-24 19:52:47 +08:00
|
|
|
|
### 运行预处理
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 NoneBot2 运行 `matcher` 前运行。
|
2021-02-08 10:26:39 +08:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.message import run_preprocessor
|
|
|
|
|
|
|
|
|
|
@run_preprocessor
|
2022-02-14 17:26:55 +08:00
|
|
|
|
async def do_something():
|
2021-02-08 10:26:39 +08:00
|
|
|
|
pass
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-24 19:52:47 +08:00
|
|
|
|
### 运行后处理
|
|
|
|
|
|
2022-02-05 19:46:33 +08:00
|
|
|
|
这个钩子函数会在 NoneBot2 运行 `matcher` 后运行。
|
2021-03-24 19:52:47 +08:00
|
|
|
|
|
2021-02-08 10:26:39 +08:00
|
|
|
|
```python
|
|
|
|
|
from nonebot.message import run_postprocessor
|
|
|
|
|
|
|
|
|
|
@run_postprocessor
|
2022-02-14 17:26:55 +08:00
|
|
|
|
async def do_something():
|
2021-02-08 10:26:39 +08:00
|
|
|
|
pass
|
2021-03-24 19:52:47 +08:00
|
|
|
|
```
|