Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: 钩子函数代码片段补充 #2173

Merged
merged 3 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions website/docs/advanced/runtime-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ options:
这个钩子函数会在 NoneBot 启动时运行。很多时候,我们并不希望在模块被导入时就执行一些耗时操作,如:连接数据库,这时候我们可以在这个钩子函数中进行这些操作。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_startup
async def do_something():
pass
Expand All @@ -35,6 +39,10 @@ async def do_something():
这个钩子函数会在 NoneBot 终止时运行。我们可以在这个钩子函数中进行一些清理工作,如:关闭数据库连接。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_shutdown
async def do_something():
pass
Expand All @@ -45,6 +53,10 @@ async def do_something():
这个钩子函数会在任何协议适配器连接 `Bot` 对象至 NoneBot 时运行。支持依赖注入,可以直接注入 `Bot` 对象。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_bot_connect
async def do_something(bot: Bot):
pass
Expand All @@ -55,6 +67,10 @@ async def do_something(bot: Bot):
这个钩子函数会在 `Bot` 断开与 NoneBot 的连接时运行。支持依赖注入,可以直接注入 `Bot` 对象。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_bot_disconnect
async def do_something(bot: Bot):
pass
Expand Down
16 changes: 16 additions & 0 deletions website/versioned_docs/version-2.0.0/advanced/runtime-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ options:
这个钩子函数会在 NoneBot 启动时运行。很多时候,我们并不希望在模块被导入时就执行一些耗时操作,如:连接数据库,这时候我们可以在这个钩子函数中进行这些操作。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_startup
async def do_something():
pass
Expand All @@ -35,6 +39,10 @@ async def do_something():
这个钩子函数会在 NoneBot 终止时运行。我们可以在这个钩子函数中进行一些清理工作,如:关闭数据库连接。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_shutdown
async def do_something():
pass
Expand All @@ -45,6 +53,10 @@ async def do_something():
这个钩子函数会在任何协议适配器连接 `Bot` 对象至 NoneBot 时运行。支持依赖注入,可以直接注入 `Bot` 对象。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_bot_connect
async def do_something(bot: Bot):
pass
Expand All @@ -55,6 +67,10 @@ async def do_something(bot: Bot):
这个钩子函数会在 `Bot` 断开与 NoneBot 的连接时运行。支持依赖注入,可以直接注入 `Bot` 对象。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_bot_disconnect
async def do_something(bot: Bot):
pass
Expand Down
16 changes: 16 additions & 0 deletions website/versioned_docs/version-2.0.0rc4/advanced/runtime-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ options:
这个钩子函数会在 NoneBot 启动时运行。很多时候,我们并不希望在模块被导入时就执行一些耗时操作,如:连接数据库,这时候我们可以在这个钩子函数中进行这些操作。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_startup
async def do_something():
pass
Expand All @@ -35,6 +39,10 @@ async def do_something():
这个钩子函数会在 NoneBot 终止时运行。我们可以在这个钩子函数中进行一些清理工作,如:关闭数据库连接。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_shutdown
async def do_something():
pass
Expand All @@ -45,6 +53,10 @@ async def do_something():
这个钩子函数会在任何协议适配器连接 `Bot` 对象至 NoneBot 时运行。支持依赖注入,可以直接注入 `Bot` 对象。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_bot_connect
async def do_something(bot: Bot):
pass
Expand All @@ -55,6 +67,10 @@ async def do_something(bot: Bot):
这个钩子函数会在 `Bot` 断开与 NoneBot 的连接时运行。支持依赖注入,可以直接注入 `Bot` 对象。

```python
from nonebot import get_driver

driver = get_driver()

@driver.on_bot_disconnect
async def do_something(bot: Bot):
pass
Expand Down