mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-17 16:20:05 +08:00
commit
1304b94b61
52
README.md
52
README.md
@ -1,19 +1,46 @@
|
||||
<div align=center>
|
||||
<img src="./docs/.vuepress/public/logo.png" width="200" height="200">
|
||||
<p align="center">
|
||||
<a href="https://v2.nonebot.dev/"><img src="https://raw.githubusercontent.com/nonebot/nonebot2/master/docs/.vuepress/public/logo.png" width="200" height="200" alt="nonebot"></a>
|
||||
</p>
|
||||
|
||||
<div align="center">
|
||||
|
||||
# NoneBot
|
||||
|
||||
[![License](https://img.shields.io/github/license/nonebot/nonebot2.svg)](LICENSE)
|
||||
[![PyPI](https://img.shields.io/pypi/v/nonebot2.svg)](https://pypi.python.org/pypi/nonebot2)
|
||||
![Python Version](https://img.shields.io/badge/python-3.7+-blue.svg)
|
||||
![CQHTTP Version](https://img.shields.io/badge/cqhttp-11+-black.svg)
|
||||
[![QQ 群](https://img.shields.io/badge/qq%E7%BE%A4-768887710-orange.svg)](https://jq.qq.com/?_wv=1027&k=5OFifDh)
|
||||
[![Telegram](https://img.shields.io/badge/telegram-chat-blue.svg)](https://t.me/cqhttp)
|
||||
[![QQ 版本发布群](https://img.shields.io/badge/%E7%89%88%E6%9C%AC%E5%8F%91%E5%B8%83%E7%BE%A4-218529254-green.svg)](https://jq.qq.com/?_wv=1027&k=5Nl0zhE)
|
||||
[![Telegram 版本发布频道](https://img.shields.io/badge/%E7%89%88%E6%9C%AC%E5%8F%91%E5%B8%83%E9%A2%91%E9%81%93-join-green.svg)](https://t.me/cqhttp_release)
|
||||
_✨ Python 异步机器人框架 ✨_
|
||||
|
||||
</div>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://raw.githubusercontent.com/nonebot/nonebot2/master/LICENSE">
|
||||
<img src="https://img.shields.io/github/license/nonebot/nonebot2.svg" alt="license">
|
||||
</a>
|
||||
<a href="https://pypi.python.org/pypi/nonebot2">
|
||||
<img src="https://img.shields.io/pypi/v/nonebot2.svg" alt="pypi">
|
||||
</a>
|
||||
<img src="https://img.shields.io/badge/python-3.7+-blue.svg" alt="python">
|
||||
<img src="https://img.shields.io/badge/cqhttp-11+-black.svg" alt="cqhttp"><br />
|
||||
<a href="https://jq.qq.com/?_wv=1027&k=5OFifDh">
|
||||
<img src="https://img.shields.io/badge/qq%E7%BE%A4-768887710-orange.svg" alt="QQ Chat">
|
||||
</a>
|
||||
<a href="https://t.me/cqhttp">
|
||||
<img src="https://img.shields.io/badge/telegram-chat-blue.svg" alt="Telegram Chat">
|
||||
</a>
|
||||
<a href="https://jq.qq.com/?_wv=1027&k=5Nl0zhE">
|
||||
<img src="https://img.shields.io/badge/%E7%89%88%E6%9C%AC%E5%8F%91%E5%B8%83%E7%BE%A4-218529254-green.svg" alt="QQ Release">
|
||||
</a>
|
||||
<a href="https://t.me/cqhttp_release">
|
||||
<img src="https://img.shields.io/badge/版本发布频道-join-green.svg" alt="Telegram Release">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://v2.nonebot.dev/">文档</a>
|
||||
·
|
||||
<a href="https://v2.nonebot.dev/guide/installation.html">安装</a>
|
||||
·
|
||||
<a href="https://v2.nonebot.dev/guide/getting-started.html">开始使用</a>
|
||||
</p>
|
||||
|
||||
## 简介
|
||||
|
||||
NoneBot2 是一个可扩展的 Python 异步机器人框架,它会对机器人收到的消息进行解析和处理,并以插件化的形式,分发给消息所对应的命令处理器和自然语言处理器,来完成具体的功能。
|
||||
@ -26,6 +53,11 @@ NoneBot2 是一个可扩展的 Python 异步机器人框架,它会对机器人
|
||||
|
||||
需要注意的是,NoneBot 仅支持 Python 3.7+ 及 CQHTTP(OneBot) 插件 v11+。
|
||||
|
||||
此外,NoneBot2 还有可配套使用的额外脚手架/框架:
|
||||
|
||||
- [NB-CLI](https://github.com/nonebot/nb-cli)
|
||||
- [NoneBot-Test](https://github.com/nonebot/nonebot-test)
|
||||
|
||||
## 文档
|
||||
|
||||
文档目前尚未完成,「API」部分由 sphinx 自动生成,你可以在 [这里](https://v2.nonebot.dev/) 查看。
|
||||
|
@ -38,12 +38,12 @@ module.exports = context => ({
|
||||
}
|
||||
},
|
||||
|
||||
theme: "titanium",
|
||||
theme: "nonebot",
|
||||
themeConfig: {
|
||||
logo: "/logo.png",
|
||||
repo: "nonebot/nonebot",
|
||||
repo: "nonebot/nonebot2",
|
||||
docsDir: "docs",
|
||||
docsBranch: "dev2",
|
||||
docsBranch: "dev",
|
||||
docsDirVersioned: "archive",
|
||||
docsDirPages: "pages",
|
||||
editLinks: true,
|
||||
@ -92,6 +92,10 @@ module.exports = context => ({
|
||||
title: "nonebot.config 模块",
|
||||
path: "config"
|
||||
},
|
||||
{
|
||||
title: "nonebot.plugin 模块",
|
||||
path: "plugin"
|
||||
},
|
||||
{
|
||||
title: "nonebot.matcher 模块",
|
||||
path: "matcher"
|
||||
|
@ -10,6 +10,9 @@
|
||||
* [nonebot.config](config.html)
|
||||
|
||||
|
||||
* [nonebot.plugin](plugin.html)
|
||||
|
||||
|
||||
* [nonebot.matcher](matcher.html)
|
||||
|
||||
|
||||
|
@ -34,4 +34,213 @@ Driver 基类。将后端框架封装,以满足适配器使用。
|
||||
|
||||
### _abstract_ `__init__(env, config)`
|
||||
|
||||
Initialize self. See help(type(self)) for accurate signature.
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `env: Env`: 包含环境信息的 Env 对象
|
||||
|
||||
|
||||
* `config: Config`: 包含配置信息的 Config 对象
|
||||
|
||||
|
||||
|
||||
### `env`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`str`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
环境名称
|
||||
|
||||
|
||||
|
||||
### `config`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`Config`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
配置对象
|
||||
|
||||
|
||||
|
||||
### `_clients`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`Dict[str, Bot]`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
已连接的 Bot
|
||||
|
||||
|
||||
|
||||
### _classmethod_ `register_adapter(name, adapter)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个协议适配器
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `name: str`: 适配器名称,用于在连接时进行识别
|
||||
|
||||
|
||||
* `adapter: Type[Bot]`: 适配器 Class
|
||||
|
||||
|
||||
|
||||
### _abstract property_ `type`
|
||||
|
||||
驱动类型名称
|
||||
|
||||
|
||||
### _abstract property_ `server_app`
|
||||
|
||||
驱动 APP 对象
|
||||
|
||||
|
||||
### _abstract property_ `asgi`
|
||||
|
||||
驱动 ASGI 对象
|
||||
|
||||
|
||||
### _abstract property_ `logger`
|
||||
|
||||
驱动专属 logger 日志记录器
|
||||
|
||||
|
||||
### _property_ `bots`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`Dict[str, Bot]`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
获取当前所有已连接的 Bot
|
||||
|
||||
|
||||
|
||||
### _abstract_ `on_startup(func)`
|
||||
|
||||
注册一个在驱动启动时运行的函数
|
||||
|
||||
|
||||
### _abstract_ `on_shutdown(func)`
|
||||
|
||||
注册一个在驱动停止时运行的函数
|
||||
|
||||
|
||||
### _abstract_ `run(host=None, port=None, *args, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
启动驱动框架
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `host: Optional[str]`: 驱动绑定 IP
|
||||
|
||||
|
||||
* `post: Optional[int]`: 驱动绑定端口
|
||||
|
||||
|
||||
* `*args`
|
||||
|
||||
|
||||
* `**kwargs`
|
||||
|
||||
|
||||
|
||||
### _abstract async_ `_handle_http()`
|
||||
|
||||
用于处理 HTTP 类型请求的函数
|
||||
|
||||
|
||||
### _abstract async_ `_handle_ws_reverse()`
|
||||
|
||||
用于处理 WebSocket 类型请求的函数
|
||||
|
||||
|
||||
## _class_ `BaseWebSocket`
|
||||
|
||||
基类:`object`
|
||||
|
||||
WebSocket 连接封装,统一接口方便外部调用。
|
||||
|
||||
|
||||
### _abstract_ `__init__(websocket)`
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `websocket: Any`: WebSocket 连接对象
|
||||
|
||||
|
||||
|
||||
### _property_ `websocket`
|
||||
|
||||
WebSocket 连接对象
|
||||
|
||||
|
||||
### _abstract property_ `closed`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`bool`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
连接是否已经关闭
|
||||
|
||||
|
||||
|
||||
### _abstract async_ `accept()`
|
||||
|
||||
接受 WebSocket 连接请求
|
||||
|
||||
|
||||
### _abstract async_ `close(code)`
|
||||
|
||||
关闭 WebSocket 连接请求
|
||||
|
||||
|
||||
### _abstract async_ `receive()`
|
||||
|
||||
接收一条 WebSocket 信息
|
||||
|
||||
|
||||
### _abstract async_ `send(data)`
|
||||
|
||||
发送一条 WebSocket 信息
|
||||
|
@ -5,12 +5,121 @@ sidebarDepth: 0
|
||||
|
||||
# NoneBot.drivers.fastapi 模块
|
||||
|
||||
## FastAPI 驱动适配
|
||||
|
||||
后端使用方法请参考: [FastAPI 文档](https://fastapi.tiangolo.com/)
|
||||
|
||||
|
||||
## _class_ `Driver`
|
||||
|
||||
基类:[`nonebot.drivers.BaseDriver`](#None)
|
||||
|
||||
FastAPI 驱动框架
|
||||
|
||||
|
||||
### `__init__(env, config)`
|
||||
|
||||
Initialize self. See help(type(self)) for accurate signature.
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
|
||||
* `env: Env`: 包含环境信息的 Env 对象
|
||||
|
||||
|
||||
* `config: Config`: 包含配置信息的 Config 对象
|
||||
|
||||
|
||||
### _property_ `type`
|
||||
|
||||
驱动名称: `fastapi`
|
||||
|
||||
|
||||
### _property_ `server_app`
|
||||
|
||||
`FastAPI APP` 对象
|
||||
|
||||
|
||||
### _property_ `asgi`
|
||||
|
||||
`FastAPI APP` 对象
|
||||
|
||||
|
||||
### _property_ `logger`
|
||||
|
||||
fastapi 使用的 logger
|
||||
|
||||
|
||||
### `on_startup(func)`
|
||||
|
||||
参考文档: [Events](https://fastapi.tiangolo.com/advanced/events/#startup-event)
|
||||
|
||||
|
||||
### `on_shutdown(func)`
|
||||
|
||||
参考文档: [Events](https://fastapi.tiangolo.com/advanced/events/#startup-event)
|
||||
|
||||
|
||||
### `run(host=None, port=None, *, app=None, **kwargs)`
|
||||
|
||||
使用 `uvicorn` 启动 FastAPI
|
||||
|
||||
|
||||
### _async_ `_handle_http(adapter, data=Body(Ellipsis), x_self_id=Header(None), x_signature=Header(None), auth=Depends(get_auth_bearer))`
|
||||
|
||||
用于处理 HTTP 类型请求的函数
|
||||
|
||||
|
||||
### _async_ `_handle_ws_reverse(adapter, websocket, x_self_id=Header(None), auth=Depends(get_auth_bearer))`
|
||||
|
||||
用于处理 WebSocket 类型请求的函数
|
||||
|
||||
|
||||
## _class_ `WebSocket`
|
||||
|
||||
基类:[`nonebot.drivers.BaseWebSocket`](#None)
|
||||
|
||||
|
||||
### `__init__(websocket)`
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
|
||||
* `websocket: Any`: WebSocket 连接对象
|
||||
|
||||
|
||||
### _property_ `closed`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`bool`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
连接是否已经关闭
|
||||
|
||||
|
||||
|
||||
### _async_ `accept()`
|
||||
|
||||
接受 WebSocket 连接请求
|
||||
|
||||
|
||||
### _async_ `close(code=1000)`
|
||||
|
||||
关闭 WebSocket 连接请求
|
||||
|
||||
|
||||
### _async_ `receive()`
|
||||
|
||||
接收一条 WebSocket 信息
|
||||
|
||||
|
||||
### _async_ `send(data)`
|
||||
|
||||
发送一条 WebSocket 信息
|
||||
|
@ -377,7 +377,7 @@ sidebarDepth: 0
|
||||
|
||||
|
||||
|
||||
### _async classmethod_ `send(message)`
|
||||
### _async classmethod_ `send(message, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
@ -392,8 +392,11 @@ sidebarDepth: 0
|
||||
* `message: Union[str, Message, MessageSegment]`: 消息内容
|
||||
|
||||
|
||||
* `**kwargs`: 其他传递给 `bot.send` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
|
||||
### _async classmethod_ `finish(message=None)`
|
||||
|
||||
|
||||
### _async classmethod_ `finish(message=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
@ -408,8 +411,11 @@ sidebarDepth: 0
|
||||
* `message: Union[str, Message, MessageSegment]`: 消息内容
|
||||
|
||||
|
||||
* `**kwargs`: 其他传递给 `bot.send` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
|
||||
### _async classmethod_ `pause(prompt=None)`
|
||||
|
||||
|
||||
### _async classmethod_ `pause(prompt=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
@ -424,8 +430,11 @@ sidebarDepth: 0
|
||||
* `prompt: Union[str, Message, MessageSegment]`: 消息内容
|
||||
|
||||
|
||||
* `**kwargs`: 其他传递给 `bot.send` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
|
||||
### _async classmethod_ `reject(prompt=None)`
|
||||
|
||||
|
||||
### _async classmethod_ `reject(prompt=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
@ -440,6 +449,9 @@ sidebarDepth: 0
|
||||
* `prompt: Union[str, Message, MessageSegment]`: 消息内容
|
||||
|
||||
|
||||
* `**kwargs`: 其他传递给 `bot.send` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
|
||||
|
||||
|
||||
## _class_ `MatcherGroup`
|
||||
|
||||
|
591
docs/api/plugin.md
Normal file
591
docs/api/plugin.md
Normal file
@ -0,0 +1,591 @@
|
||||
---
|
||||
contentSidebar: true
|
||||
sidebarDepth: 0
|
||||
---
|
||||
|
||||
# NoneBot.plugin 模块
|
||||
|
||||
## 插件
|
||||
|
||||
为 NoneBot 插件开发提供便携的定义函数。
|
||||
|
||||
|
||||
## `plugins`
|
||||
|
||||
|
||||
* **类型**
|
||||
|
||||
`Dict[str, Plugin]`
|
||||
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
已加载的插件
|
||||
|
||||
|
||||
|
||||
## _class_ `Plugin`
|
||||
|
||||
基类:`object`
|
||||
|
||||
存储插件信息
|
||||
|
||||
|
||||
### `name`
|
||||
|
||||
|
||||
* **类型**: `str`
|
||||
|
||||
|
||||
* **说明**: 插件名称,使用 文件/文件夹 名称作为插件名
|
||||
|
||||
|
||||
### `module`
|
||||
|
||||
|
||||
* **类型**: `ModuleType`
|
||||
|
||||
|
||||
* **说明**: 插件模块对象
|
||||
|
||||
|
||||
### `matcher`
|
||||
|
||||
|
||||
* **类型**: `Set[Type[Matcher]]`
|
||||
|
||||
|
||||
* **说明**: 插件内定义的 `Matcher`
|
||||
|
||||
|
||||
## `on(type='', rule=None, permission=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个基础事件响应器,可自定义类型。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `type: str`: 事件响应器类型
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `permission: Optional[Permission]`: 事件响应权限
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_metaevent(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个元事件响应器。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_message(rule=None, permission=None, *, handlers=None, temp=False, priority=1, block=True, state=None)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个消息事件响应器。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `permission: Optional[Permission]`: 事件响应权限
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_notice(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个通知事件响应器。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_request(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个请求事件响应器。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_startswith(msg, rule=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个消息事件响应器,并且当消息的\*\*文本部分\*\*以指定内容开头时响应。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `msg: str`: 指定消息开头内容
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `permission: Optional[Permission]`: 事件响应权限
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_endswith(msg, rule=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个消息事件响应器,并且当消息的\*\*文本部分\*\*以指定内容结尾时响应。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `msg: str`: 指定消息结尾内容
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `permission: Optional[Permission]`: 事件响应权限
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## `on_command(cmd, rule=None, aliases=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个消息事件响应器,并且当消息以指定命令开头时响应。
|
||||
|
||||
命令匹配规则参考: [命令形式匹配](rule.html#command-command)
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `cmd: Union[str, Tuple[str, ...]]`: 指定命令内容
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `aliases: Optional[Set[Union[str, Tuple[str, ...]]]]`: 命令别名
|
||||
|
||||
|
||||
* `permission: Optional[Permission]`: 事件响应权限
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
* `MatcherGroup`
|
||||
|
||||
|
||||
|
||||
## `on_regex(pattern, flags=0, rule=None, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个消息事件响应器,并且当消息匹配正则表达式时响应。
|
||||
|
||||
命令匹配规则参考: [正则匹配](rule.html#regex-regex-flags-0)
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `pattern: str`: 正则表达式
|
||||
|
||||
|
||||
* `flags: Union[int, re.RegexFlag]`: 正则匹配标志
|
||||
|
||||
|
||||
* `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则
|
||||
|
||||
|
||||
* `permission: Optional[Permission]`: 事件响应权限
|
||||
|
||||
|
||||
* `handlers: Optional[List[Handler]]`: 事件处理函数列表
|
||||
|
||||
|
||||
* `temp: bool`: 是否为临时事件响应器(仅执行一次)
|
||||
|
||||
|
||||
* `priority: int`: 事件响应器优先级
|
||||
|
||||
|
||||
* `block: bool`: 是否阻止事件向更低优先级传递
|
||||
|
||||
|
||||
* `state: Optional[dict]`: 默认的 state
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
|
||||
## _class_ `CommandGroup`
|
||||
|
||||
基类:`object`
|
||||
|
||||
命令组,用于声明一组有相同名称前缀的命令。
|
||||
|
||||
|
||||
### `__init__(cmd, **kwargs)`
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `cmd: Union[str, Tuple[str, ...]]`: 命令前缀
|
||||
|
||||
|
||||
* `**kwargs`: 其他传递给 `on_command` 的参数默认值,参考 [on_command](#on-command-cmd-rule-none-aliases-none-kwargs)
|
||||
|
||||
|
||||
|
||||
### `basecmd`
|
||||
|
||||
|
||||
* **类型**: `Tuple[str, ...]`
|
||||
|
||||
|
||||
* **说明**: 命令前缀
|
||||
|
||||
|
||||
### `base_kwargs`
|
||||
|
||||
|
||||
* **类型**: `Dict[str, Any]`
|
||||
|
||||
|
||||
* **说明**: 其他传递给 `on_command` 的参数默认值
|
||||
|
||||
|
||||
### `command(cmd, **kwargs)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
注册一个新的命令。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `cmd: Union[str, Tuple[str, ...]]`: 命令前缀
|
||||
|
||||
|
||||
* `**kwargs`: 其他传递给 `on_command` 的参数,将会覆盖命令组默认值
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Type[Matcher]`
|
||||
|
||||
|
||||
* `MatcherGroup`
|
||||
|
||||
|
||||
|
||||
## `load_plugin(module_path)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
使用 `importlib` 加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `module_path: str`: 插件名称 `path.to.your.plugin`
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Optional[Plugin]`
|
||||
|
||||
|
||||
|
||||
## `load_plugins(*plugin_dir)`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
导入目录下多个插件,以 `_` 开头的插件不会被导入!
|
||||
|
||||
|
||||
|
||||
* **参数**
|
||||
|
||||
|
||||
* `*plugin_dir: str`: 插件路径
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Set[Plugin]`
|
||||
|
||||
|
||||
|
||||
## `load_builtin_plugins()`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
导入 NoneBot 内置插件
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Plugin`
|
||||
|
||||
|
||||
|
||||
## `get_loaded_plugins()`
|
||||
|
||||
|
||||
* **说明**
|
||||
|
||||
获取当前已导入的插件。
|
||||
|
||||
|
||||
|
||||
* **返回**
|
||||
|
||||
|
||||
* `Set[Plugin]`
|
@ -186,6 +186,10 @@ Rule(async_function, run_sync(sync_function))
|
||||
* `flags: Union[int, re.RegexFlag]`: 正则标志
|
||||
|
||||
|
||||
:::tip 提示
|
||||
正则表达式匹配使用 search 而非 match,如需从头匹配请使用 `r"^xxx"` 来确保匹配开头
|
||||
:::
|
||||
|
||||
|
||||
## `to_me()`
|
||||
|
||||
|
@ -20,6 +20,8 @@ NoneBot2 是一个可扩展的 Python 异步机器人框架,它会对机器人
|
||||
|
||||
## 它如何工作?
|
||||
|
||||
<!-- TODO: how to work -->
|
||||
|
||||
~~未填坑~~
|
||||
|
||||
## 特色
|
||||
|
@ -6,7 +6,10 @@
|
||||
请确保你的 Python 版本 >= 3.7。
|
||||
:::
|
||||
|
||||
请在安装 nonebot2 之前卸载 nonebot 1.x
|
||||
|
||||
```bash
|
||||
pip uninstall nonebot
|
||||
pip install nonebot2
|
||||
```
|
||||
|
||||
|
@ -4,6 +4,7 @@ NoneBot Api Reference
|
||||
:模块索引:
|
||||
- `nonebot <nonebot.html>`_
|
||||
- `nonebot.config <config.html>`_
|
||||
- `nonebot.plugin <plugin.html>`_
|
||||
- `nonebot.matcher <matcher.html>`_
|
||||
- `nonebot.rule <rule.html>`_
|
||||
- `nonebot.permission <permission.html>`_
|
||||
|
12
docs_build/plugin.rst
Normal file
12
docs_build/plugin.rst
Normal file
@ -0,0 +1,12 @@
|
||||
---
|
||||
contentSidebar: true
|
||||
sidebarDepth: 0
|
||||
---
|
||||
|
||||
NoneBot.plugin 模块
|
||||
====================
|
||||
|
||||
.. automodule:: nonebot.plugin
|
||||
:members:
|
||||
:show-inheritance:
|
||||
:special-members: __init__
|
@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
|
||||
from nonebot.config import Config
|
||||
from nonebot.adapters import BaseBot
|
||||
from nonebot.adapters import BaseBot, BaseEvent, BaseMessage, BaseMessageSegment
|
||||
from nonebot.typing import Any, Dict, List, Union, Driver, Optional, NoReturn, WebSocket, Iterable
|
||||
|
||||
|
||||
@ -750,7 +750,7 @@ class Bot(BaseBot):
|
||||
...
|
||||
|
||||
|
||||
class Event:
|
||||
class Event(BaseEvent):
|
||||
|
||||
def __init__(self, raw_event: dict):
|
||||
...
|
||||
@ -856,7 +856,7 @@ class Event:
|
||||
...
|
||||
|
||||
|
||||
class MessageSegment:
|
||||
class MessageSegment(BaseMessageSegment):
|
||||
|
||||
def __init__(self, type: str, data: Dict[str, Any]) -> None:
|
||||
...
|
||||
@ -982,7 +982,7 @@ class MessageSegment:
|
||||
...
|
||||
|
||||
|
||||
class Message:
|
||||
class Message(BaseMessage):
|
||||
|
||||
@staticmethod
|
||||
def _construct(msg: Union[str, dict, list]) -> Iterable[MessageSegment]:
|
||||
|
@ -27,12 +27,36 @@ class BaseDriver(abc.ABC):
|
||||
|
||||
@abc.abstractmethod
|
||||
def __init__(self, env: Env, config: Config):
|
||||
"""
|
||||
:参数:
|
||||
* ``env: Env``: 包含环境信息的 Env 对象
|
||||
* ``config: Config``: 包含配置信息的 Config 对象
|
||||
"""
|
||||
self.env = env.environment
|
||||
"""
|
||||
:类型: ``str``
|
||||
:说明: 环境名称
|
||||
"""
|
||||
self.config = config
|
||||
"""
|
||||
:类型: ``Config``
|
||||
:说明: 配置对象
|
||||
"""
|
||||
self._clients: Dict[str, Bot] = {}
|
||||
"""
|
||||
:类型: ``Dict[str, Bot]``
|
||||
:说明: 已连接的 Bot
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def register_adapter(cls, name: str, adapter: Type[Bot]):
|
||||
"""
|
||||
:说明:
|
||||
注册一个协议适配器
|
||||
:参数:
|
||||
* ``name: str``: 适配器名称,用于在连接时进行识别
|
||||
* ``adapter: Type[Bot]``: 适配器 Class
|
||||
"""
|
||||
cls._adapters[name] = adapter
|
||||
logger.opt(
|
||||
colors=True).debug(f'Succeeded to load adapter "<y>{name}</y>"')
|
||||
@ -40,33 +64,43 @@ class BaseDriver(abc.ABC):
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def type(self):
|
||||
"""驱动类型名称"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def server_app(self):
|
||||
"""驱动 APP 对象"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def asgi(self):
|
||||
"""驱动 ASGI 对象"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def logger(self):
|
||||
"""驱动专属 logger 日志记录器"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def bots(self) -> Dict[str, Bot]:
|
||||
"""
|
||||
:类型: ``Dict[str, Bot]``
|
||||
:说明: 获取当前所有已连接的 Bot
|
||||
"""
|
||||
return self._clients
|
||||
|
||||
@abc.abstractmethod
|
||||
def on_startup(self, func: Callable) -> Callable:
|
||||
"""注册一个在驱动启动时运行的函数"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def on_shutdown(self, func: Callable) -> Callable:
|
||||
"""注册一个在驱动停止时运行的函数"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
@ -75,44 +109,69 @@ class BaseDriver(abc.ABC):
|
||||
port: Optional[int] = None,
|
||||
*args,
|
||||
**kwargs):
|
||||
"""
|
||||
:说明:
|
||||
启动驱动框架
|
||||
:参数:
|
||||
* ``host: Optional[str]``: 驱动绑定 IP
|
||||
* ``post: Optional[int]``: 驱动绑定端口
|
||||
* ``*args``
|
||||
* ``**kwargs``
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _handle_http(self):
|
||||
"""用于处理 HTTP 类型请求的函数"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def _handle_ws_reverse(self):
|
||||
"""用于处理 WebSocket 类型请求的函数"""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class BaseWebSocket(object):
|
||||
"""WebSocket 连接封装,统一接口方便外部调用。"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def __init__(self, websocket):
|
||||
"""
|
||||
:参数:
|
||||
* ``websocket: Any``: WebSocket 连接对象
|
||||
"""
|
||||
self._websocket = websocket
|
||||
|
||||
@property
|
||||
def websocket(self):
|
||||
"""WebSocket 连接对象"""
|
||||
return self._websocket
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def closed(self):
|
||||
"""
|
||||
:类型: ``bool``
|
||||
:说明: 连接是否已经关闭
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def accept(self):
|
||||
"""接受 WebSocket 连接请求"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def close(self, code: int):
|
||||
"""关闭 WebSocket 连接请求"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def receive(self) -> dict:
|
||||
"""接收一条 WebSocket 信息"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
async def send(self, data: dict):
|
||||
"""发送一条 WebSocket 信息"""
|
||||
raise NotImplementedError
|
||||
|
@ -1,5 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
FastAPI 驱动适配
|
||||
================
|
||||
|
||||
后端使用方法请参考: `FastAPI 文档`_
|
||||
|
||||
.. _FastAPI 文档:
|
||||
https://fastapi.tiangolo.com/
|
||||
"""
|
||||
|
||||
import hmac
|
||||
import json
|
||||
@ -31,6 +40,7 @@ def get_auth_bearer(access_token: Optional[str] = Header(
|
||||
|
||||
|
||||
class Driver(BaseDriver):
|
||||
"""FastAPI 驱动框架"""
|
||||
|
||||
def __init__(self, env: Env, config: Config):
|
||||
super().__init__(env, config)
|
||||
@ -50,29 +60,35 @@ class Driver(BaseDriver):
|
||||
@property
|
||||
@overrides(BaseDriver)
|
||||
def type(self) -> str:
|
||||
"""驱动名称: ``fastapi``"""
|
||||
return "fastapi"
|
||||
|
||||
@property
|
||||
@overrides(BaseDriver)
|
||||
def server_app(self) -> FastAPI:
|
||||
"""``FastAPI APP`` 对象"""
|
||||
return self._server_app
|
||||
|
||||
@property
|
||||
@overrides(BaseDriver)
|
||||
def asgi(self):
|
||||
"""``FastAPI APP`` 对象"""
|
||||
return self._server_app
|
||||
|
||||
@property
|
||||
@overrides(BaseDriver)
|
||||
def logger(self) -> logging.Logger:
|
||||
"""fastapi 使用的 logger"""
|
||||
return logging.getLogger("fastapi")
|
||||
|
||||
@overrides(BaseDriver)
|
||||
def on_startup(self, func: Callable) -> Callable:
|
||||
"""参考文档: `Events <https://fastapi.tiangolo.com/advanced/events/#startup-event>`_"""
|
||||
return self.server_app.on_event("startup")(func)
|
||||
|
||||
@overrides(BaseDriver)
|
||||
def on_shutdown(self, func: Callable) -> Callable:
|
||||
"""参考文档: `Events <https://fastapi.tiangolo.com/advanced/events/#startup-event>`_"""
|
||||
return self.server_app.on_event("shutdown")(func)
|
||||
|
||||
@overrides(BaseDriver)
|
||||
@ -82,6 +98,7 @@ class Driver(BaseDriver):
|
||||
*,
|
||||
app: Optional[str] = None,
|
||||
**kwargs):
|
||||
"""使用 ``uvicorn`` 启动 FastAPI"""
|
||||
LOGGING_CONFIG = {
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
|
@ -298,66 +298,70 @@ class Matcher(metaclass=MatcherMeta):
|
||||
return _decorator
|
||||
|
||||
@classmethod
|
||||
async def send(cls, message: Union[str, Message, MessageSegment]):
|
||||
async def send(cls, message: Union[str, Message, MessageSegment], **kwargs):
|
||||
"""
|
||||
:说明:
|
||||
发送一条消息给当前交互用户
|
||||
:参数:
|
||||
* ``message: Union[str, Message, MessageSegment]``: 消息内容
|
||||
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
"""
|
||||
bot = current_bot.get()
|
||||
event = current_event.get()
|
||||
await bot.send(event=event, message=message)
|
||||
await bot.send(event=event, message=message, **kwargs)
|
||||
|
||||
@classmethod
|
||||
async def finish(
|
||||
cls,
|
||||
message: Optional[Union[str, Message,
|
||||
MessageSegment]] = None) -> NoReturn:
|
||||
async def finish(cls,
|
||||
message: Optional[Union[str, Message,
|
||||
MessageSegment]] = None,
|
||||
**kwargs) -> NoReturn:
|
||||
"""
|
||||
:说明:
|
||||
发送一条消息给当前交互用户并结束当前事件响应器
|
||||
:参数:
|
||||
* ``message: Union[str, Message, MessageSegment]``: 消息内容
|
||||
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
"""
|
||||
bot = current_bot.get()
|
||||
event = current_event.get()
|
||||
if message:
|
||||
await bot.send(event=event, message=message)
|
||||
await bot.send(event=event, message=message, **kwargs)
|
||||
raise FinishedException
|
||||
|
||||
@classmethod
|
||||
async def pause(
|
||||
cls,
|
||||
prompt: Optional[Union[str, Message,
|
||||
MessageSegment]] = None) -> NoReturn:
|
||||
async def pause(cls,
|
||||
prompt: Optional[Union[str, Message,
|
||||
MessageSegment]] = None,
|
||||
**kwargs) -> NoReturn:
|
||||
"""
|
||||
:说明:
|
||||
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后继续下一个处理函数
|
||||
:参数:
|
||||
* ``prompt: Union[str, Message, MessageSegment]``: 消息内容
|
||||
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
"""
|
||||
bot = current_bot.get()
|
||||
event = current_event.get()
|
||||
if prompt:
|
||||
await bot.send(event=event, message=prompt)
|
||||
await bot.send(event=event, message=prompt, **kwargs)
|
||||
raise PausedException
|
||||
|
||||
@classmethod
|
||||
async def reject(
|
||||
cls,
|
||||
prompt: Optional[Union[str, Message,
|
||||
MessageSegment]] = None) -> NoReturn:
|
||||
async def reject(cls,
|
||||
prompt: Optional[Union[str, Message,
|
||||
MessageSegment]] = None,
|
||||
**kwargs) -> NoReturn:
|
||||
"""
|
||||
:说明:
|
||||
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后重新运行当前处理函数
|
||||
:参数:
|
||||
* ``prompt: Union[str, Message, MessageSegment]``: 消息内容
|
||||
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
||||
"""
|
||||
bot = current_bot.get()
|
||||
event = current_event.get()
|
||||
if prompt:
|
||||
await bot.send(event=event, message=prompt)
|
||||
await bot.send(event=event, message=prompt, **kwargs)
|
||||
raise RejectedException
|
||||
|
||||
# 运行handlers
|
||||
|
@ -1,5 +1,11 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
插件
|
||||
====
|
||||
|
||||
为 NoneBot 插件开发提供便携的定义函数。
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
@ -13,21 +19,39 @@ from nonebot.permission import Permission
|
||||
from nonebot.typing import Handler, RuleChecker
|
||||
from nonebot.matcher import Matcher, MatcherGroup
|
||||
from nonebot.rule import Rule, startswith, endswith, command, regex
|
||||
from nonebot.typing import Set, List, Dict, Type, Tuple, Union, Optional, ModuleType
|
||||
from nonebot.typing import Any, Set, List, Dict, Type, Tuple, Union, Optional, ModuleType
|
||||
|
||||
plugins: Dict[str, "Plugin"] = {}
|
||||
"""
|
||||
:类型: ``Dict[str, Plugin]``
|
||||
:说明: 已加载的插件
|
||||
"""
|
||||
|
||||
_tmp_matchers: Set[Type[Matcher]] = set()
|
||||
|
||||
|
||||
@dataclass(eq=False)
|
||||
class Plugin(object):
|
||||
"""存储插件信息"""
|
||||
name: str
|
||||
"""
|
||||
- **类型**: ``str``
|
||||
- **说明**: 插件名称,使用 文件/文件夹 名称作为插件名
|
||||
"""
|
||||
module: ModuleType
|
||||
"""
|
||||
- **类型**: ``ModuleType``
|
||||
- **说明**: 插件模块对象
|
||||
"""
|
||||
matcher: Set[Type[Matcher]]
|
||||
"""
|
||||
- **类型**: ``Set[Type[Matcher]]``
|
||||
- **说明**: 插件内定义的 ``Matcher``
|
||||
"""
|
||||
|
||||
|
||||
def on(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
def on(type: str = "",
|
||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
permission: Optional[Permission] = None,
|
||||
*,
|
||||
handlers: Optional[List[Handler]] = None,
|
||||
@ -35,7 +59,22 @@ def on(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
priority: int = 1,
|
||||
block: bool = False,
|
||||
state: Optional[dict] = None) -> Type[Matcher]:
|
||||
matcher = Matcher.new("",
|
||||
"""
|
||||
:说明:
|
||||
注册一个基础事件响应器,可自定义类型。
|
||||
:参数:
|
||||
* ``type: str``: 事件响应器类型
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``permission: Optional[Permission]``: 事件响应权限
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
matcher = Matcher.new(type,
|
||||
Rule() & rule,
|
||||
permission or Permission(),
|
||||
temp=temp,
|
||||
@ -54,6 +93,19 @@ def on_metaevent(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
priority: int = 1,
|
||||
block: bool = False,
|
||||
state: Optional[dict] = None) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个元事件响应器。
|
||||
:参数:
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
matcher = Matcher.new("meta_event",
|
||||
Rule() & rule,
|
||||
Permission(),
|
||||
@ -74,6 +126,20 @@ def on_message(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
priority: int = 1,
|
||||
block: bool = True,
|
||||
state: Optional[dict] = None) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个消息事件响应器。
|
||||
:参数:
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``permission: Optional[Permission]``: 事件响应权限
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
matcher = Matcher.new("message",
|
||||
Rule() & rule,
|
||||
permission or Permission(),
|
||||
@ -93,6 +159,19 @@ def on_notice(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
priority: int = 1,
|
||||
block: bool = False,
|
||||
state: Optional[dict] = None) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个通知事件响应器。
|
||||
:参数:
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
matcher = Matcher.new("notice",
|
||||
Rule() & rule,
|
||||
Permission(),
|
||||
@ -112,6 +191,19 @@ def on_request(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
priority: int = 1,
|
||||
block: bool = False,
|
||||
state: Optional[dict] = None) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个请求事件响应器。
|
||||
:参数:
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
matcher = Matcher.new("request",
|
||||
Rule() & rule,
|
||||
Permission(),
|
||||
@ -127,6 +219,21 @@ def on_request(rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
def on_startswith(msg: str,
|
||||
rule: Optional[Optional[Union[Rule, RuleChecker]]] = None,
|
||||
**kwargs) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。
|
||||
:参数:
|
||||
* ``msg: str``: 指定消息开头内容
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``permission: Optional[Permission]``: 事件响应权限
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
return on_message(startswith(msg) & rule, **kwargs) if rule else on_message(
|
||||
startswith(msg), **kwargs)
|
||||
|
||||
@ -134,6 +241,21 @@ def on_startswith(msg: str,
|
||||
def on_endswith(msg: str,
|
||||
rule: Optional[Optional[Union[Rule, RuleChecker]]] = None,
|
||||
**kwargs) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。
|
||||
:参数:
|
||||
* ``msg: str``: 指定消息结尾内容
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``permission: Optional[Permission]``: 事件响应权限
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
return on_message(endswith(msg) & rule, **kwargs) if rule else on_message(
|
||||
startswith(msg), **kwargs)
|
||||
|
||||
@ -142,6 +264,25 @@ def on_command(cmd: Union[str, Tuple[str, ...]],
|
||||
rule: Optional[Union[Rule, RuleChecker]] = None,
|
||||
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None,
|
||||
**kwargs) -> Union[Type[Matcher], MatcherGroup]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个消息事件响应器,并且当消息以指定命令开头时响应。
|
||||
|
||||
命令匹配规则参考: `命令形式匹配 <rule.html#command-command>`_
|
||||
:参数:
|
||||
* ``cmd: Union[str, Tuple[str, ...]]``: 指定命令内容
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``aliases: Optional[Set[Union[str, Tuple[str, ...]]]]``: 命令别名
|
||||
* ``permission: Optional[Permission]``: 事件响应权限
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
- ``MatcherGroup``
|
||||
"""
|
||||
if isinstance(cmd, str):
|
||||
cmd = (cmd,)
|
||||
|
||||
@ -172,12 +313,80 @@ def on_regex(pattern: str,
|
||||
flags: Union[int, re.RegexFlag] = 0,
|
||||
rule: Optional[Rule] = None,
|
||||
**kwargs) -> Type[Matcher]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个消息事件响应器,并且当消息匹配正则表达式时响应。
|
||||
|
||||
命令匹配规则参考: `正则匹配 <rule.html#regex-regex-flags-0>`_
|
||||
:参数:
|
||||
* ``pattern: str``: 正则表达式
|
||||
* ``flags: Union[int, re.RegexFlag]``: 正则匹配标志
|
||||
* ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则
|
||||
* ``permission: Optional[Permission]``: 事件响应权限
|
||||
* ``handlers: Optional[List[Handler]]``: 事件处理函数列表
|
||||
* ``temp: bool``: 是否为临时事件响应器(仅执行一次)
|
||||
* ``priority: int``: 事件响应器优先级
|
||||
* ``block: bool``: 是否阻止事件向更低优先级传递
|
||||
* ``state: Optional[dict]``: 默认的 state
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
"""
|
||||
return on_message(regex(pattern, flags) &
|
||||
rule, **kwargs) if rule else on_message(
|
||||
regex(pattern, flags), **kwargs)
|
||||
|
||||
|
||||
class CommandGroup:
|
||||
"""命令组,用于声明一组有相同名称前缀的命令。"""
|
||||
|
||||
def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs):
|
||||
"""
|
||||
:参数:
|
||||
* ``cmd: Union[str, Tuple[str, ...]]``: 命令前缀
|
||||
* ``**kwargs``: 其他传递给 ``on_command`` 的参数默认值,参考 `on_command <#on-command-cmd-rule-none-aliases-none-kwargs>`_
|
||||
"""
|
||||
self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
|
||||
"""
|
||||
- **类型**: ``Tuple[str, ...]``
|
||||
- **说明**: 命令前缀
|
||||
"""
|
||||
if "aliases" in kwargs:
|
||||
del kwargs["aliases"]
|
||||
self.base_kwargs: Dict[str, Any] = kwargs
|
||||
"""
|
||||
- **类型**: ``Dict[str, Any]``
|
||||
- **说明**: 其他传递给 ``on_command`` 的参数默认值
|
||||
"""
|
||||
|
||||
def command(self, cmd: Union[str, Tuple[str, ...]],
|
||||
**kwargs) -> Union[Type[Matcher], MatcherGroup]:
|
||||
"""
|
||||
:说明:
|
||||
注册一个新的命令。
|
||||
:参数:
|
||||
* ``cmd: Union[str, Tuple[str, ...]]``: 命令前缀
|
||||
* ``**kwargs``: 其他传递给 ``on_command`` 的参数,将会覆盖命令组默认值
|
||||
:返回:
|
||||
- ``Type[Matcher]``
|
||||
- ``MatcherGroup``
|
||||
"""
|
||||
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
|
||||
cmd = self.basecmd + sub_cmd
|
||||
|
||||
final_kwargs = self.base_kwargs.copy()
|
||||
final_kwargs.update(kwargs)
|
||||
return on_command(cmd, **final_kwargs)
|
||||
|
||||
|
||||
def load_plugin(module_path: str) -> Optional[Plugin]:
|
||||
"""
|
||||
:说明:
|
||||
使用 ``importlib`` 加载单个插件,可以是本地插件或是通过 ``pip`` 安装的插件。
|
||||
:参数:
|
||||
* ``module_path: str``: 插件名称 ``path.to.your.plugin``
|
||||
:返回:
|
||||
- ``Optional[Plugin]``
|
||||
"""
|
||||
try:
|
||||
_tmp_matchers.clear()
|
||||
if module_path in plugins:
|
||||
@ -202,6 +411,14 @@ def load_plugin(module_path: str) -> Optional[Plugin]:
|
||||
|
||||
|
||||
def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
||||
"""
|
||||
:说明:
|
||||
导入目录下多个插件,以 ``_`` 开头的插件不会被导入!
|
||||
:参数:
|
||||
- ``*plugin_dir: str``: 插件路径
|
||||
:返回:
|
||||
- ``Set[Plugin]``
|
||||
"""
|
||||
loaded_plugins = set()
|
||||
for module_info in pkgutil.iter_modules(plugin_dir):
|
||||
_tmp_matchers.clear()
|
||||
@ -209,7 +426,7 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
||||
if name.startswith("_"):
|
||||
continue
|
||||
|
||||
spec = module_info.module_finder.find_spec(name)
|
||||
spec = module_info.module_finder.find_spec(name, None)
|
||||
if spec.name in plugins:
|
||||
continue
|
||||
elif spec.name in sys.modules:
|
||||
@ -232,27 +449,21 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
||||
return loaded_plugins
|
||||
|
||||
|
||||
def load_builtin_plugins():
|
||||
def load_builtin_plugins() -> Optional[Plugin]:
|
||||
"""
|
||||
:说明:
|
||||
导入 NoneBot 内置插件
|
||||
:返回:
|
||||
- ``Plugin``
|
||||
"""
|
||||
return load_plugin("nonebot.plugins.base")
|
||||
|
||||
|
||||
def get_loaded_plugins() -> Set[Plugin]:
|
||||
"""
|
||||
:说明:
|
||||
获取当前已导入的插件。
|
||||
:返回:
|
||||
- ``Set[Plugin]``
|
||||
"""
|
||||
return set(plugins.values())
|
||||
|
||||
|
||||
class CommandGroup:
|
||||
|
||||
def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs):
|
||||
self.basecmd = (cmd,) if isinstance(cmd, str) else cmd
|
||||
if "aliases" in kwargs:
|
||||
del kwargs["aliases"]
|
||||
self.base_kwargs = kwargs
|
||||
|
||||
def command(self, cmd: Union[str, Tuple[str, ...]],
|
||||
**kwargs) -> Union[Type[Matcher], MatcherGroup]:
|
||||
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
|
||||
cmd = self.basecmd + sub_cmd
|
||||
|
||||
final_kwargs = self.base_kwargs.copy()
|
||||
final_kwargs.update(kwargs)
|
||||
return on_command(cmd, **final_kwargs)
|
||||
|
@ -18,7 +18,8 @@ class Plugin(object):
|
||||
matcher: Set[Type[Matcher]]
|
||||
|
||||
|
||||
def on(rule: Optional[Union[Rule, RuleChecker]] = ...,
|
||||
def on(type: str = ...,
|
||||
rule: Optional[Union[Rule, RuleChecker]] = ...,
|
||||
permission: Optional[Permission] = ...,
|
||||
*,
|
||||
handlers: Optional[List[Handler]] = ...,
|
||||
|
@ -238,6 +238,10 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
|
||||
:参数:
|
||||
* ``regex: str``: 正则表达式
|
||||
* ``flags: Union[int, re.RegexFlag]``: 正则标志
|
||||
|
||||
\:\:\:tip 提示
|
||||
正则表达式匹配使用 search 而非 match,如需从头匹配请使用 ``r"^xxx"`` 来确保匹配开头
|
||||
\:\:\:
|
||||
"""
|
||||
|
||||
pattern = re.compile(regex, flags)
|
||||
|
1349
package-lock.json
generated
1349
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@ -20,14 +20,14 @@
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@vuepress/plugin-back-to-top": "^1.5.4",
|
||||
"@vuepress/plugin-medium-zoom": "^1.5.4",
|
||||
"vuepress": "^1.5.4",
|
||||
"vuepress-plugin-versioning": "^4.5.0",
|
||||
"vuepress-theme-titanium": "^4.5.1"
|
||||
"@vuepress/plugin-back-to-top": "^1.7.1",
|
||||
"@vuepress/plugin-medium-zoom": "^1.7.1",
|
||||
"vuepress": "^1.7.1",
|
||||
"vuepress-plugin-versioning": "git+https://github.com/nonebot/vuepress-plugin-versioning.git",
|
||||
"vuepress-theme-nonebot": "git+https://github.com/nonebot/vuepress-theme-nonebot.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"vuetify": "^2.3.10",
|
||||
"vuetify": "^2.3.14",
|
||||
"wowjs": "^1.1.3"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user