nonebot2/docs/api/adapters/cqhttp.md

508 lines
7.1 KiB
Markdown
Raw Normal View History

2020-09-29 14:33:08 +00:00
---
contentSidebar: true
sidebarDepth: 0
---
# NoneBot.adapters.cqhttp 模块
2020-12-03 08:04:14 +00:00
## `escape(s, *, escape_comma=True)`
* **说明**
对字符串进行 CQ 码转义。
* **参数**
* `s: str`: 需要转义的字符串
* `escape_comma: bool`: 是否转义逗号(`,`)。
## `unescape(s)`
* **说明**
对字符串进行 CQ 码去转义。
* **参数**
* `s: str`: 需要转义的字符串
## _exception_ `CQHTTPAdapterException`
基类:[`nonebot.exception.AdapterException`](../exception.md#nonebot.exception.AdapterException)
## _exception_ `ActionFailed`
基类:[`nonebot.exception.ActionFailed`](../exception.md#nonebot.exception.ActionFailed), `nonebot.adapters.cqhttp.exception.CQHTTPAdapterException`
* **说明**
API 请求返回错误信息。
* **参数**
* `retcode: Optional[int]`: 错误码
## _exception_ `NetworkError`
基类:[`nonebot.exception.NetworkError`](../exception.md#nonebot.exception.NetworkError), `nonebot.adapters.cqhttp.exception.CQHTTPAdapterException`
* **说明**
网络错误。
* **参数**
* `retcode: Optional[int]`: 错误码
## _exception_ `ApiNotAvailable`
基类:[`nonebot.exception.ApiNotAvailable`](../exception.md#nonebot.exception.ApiNotAvailable), `nonebot.adapters.cqhttp.exception.CQHTTPAdapterException`
## _async_ `_check_reply(bot, event)`
* **说明**
检查消息中存在的回复,去除并赋值 `event.reply`, `event.to_me`
* **参数**
* `bot: Bot`: Bot 对象
* `event: Event`: Event 对象
## `_check_at_me(bot, event)`
* **说明**
检查消息开头或结尾是否存在 @机器人,去除并赋值 `event.to_me`
* **参数**
* `bot: Bot`: Bot 对象
* `event: Event`: Event 对象
## `_check_nickname(bot, event)`
* **说明**
检查消息开头是否存在,去除并赋值 `event.to_me`
* **参数**
* `bot: Bot`: Bot 对象
* `event: Event`: Event 对象
## `_handle_api_result(result)`
* **说明**
处理 API 请求返回值。
* **参数**
* `result: Optional[Dict[str, Any]]`: API 返回数据
* **返回**
* `Any`: API 调用返回数据
* **异常**
* `ActionFailed`: API 调用失败
## _class_ `Bot`
2020-12-06 16:52:26 +00:00
基类:[`nonebot.adapters.Bot`](README.md#nonebot.adapters.Bot)
2020-12-03 08:04:14 +00:00
CQHTTP 协议 Bot 适配。继承属性参考 [BaseBot](./#class-basebot) 。
### _property_ `type`
* 返回: `"cqhttp"`
### _async classmethod_ `check_permission(driver, connection_type, headers, body)`
* **说明**
CQHTTP (OneBot) 协议鉴权。参考 [鉴权](https://github.com/howmanybots/onebot/blob/master/v11/specs/communication/authorization.md)
### _async_ `handle_message(message)`
* **说明**
调用 [_check_reply](#async-check-reply-bot-event), [_check_at_me](#check-at-me-bot-event), [_check_nickname](#check-nickname-bot-event) 处理事件并转换为 [Event](#class-event)
### _async_ `call_api(api, **data)`
* **说明**
调用 CQHTTP 协议 API
* **参数**
* `api: str`: API 名称
* `**data: Any`: API 参数
* **返回**
* `Any`: API 调用返回数据
* **异常**
* `NetworkError`: 网络错误
* `ActionFailed`: API 调用失败
### _async_ `send(event, message, at_sender=False, **kwargs)`
* **说明**
根据 `event` 向触发事件的主体发送消息。
* **参数**
* `event: Event`: Event 对象
* `message: Union[str, Message, MessageSegment]`: 要发送的消息
* `at_sender: bool`: 是否 @ 事件主体
* `**kwargs`: 覆盖默认参数
* **返回**
* `Any`: API 调用返回数据
* **异常**
* `ValueError`: 缺少 `user_id`, `group_id`
* `NetworkError`: 网络错误
* `ActionFailed`: API 调用失败
## _class_ `Event`
2020-12-06 16:52:26 +00:00
基类:[`nonebot.adapters.Event`](README.md#nonebot.adapters.Event)
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
CQHTTP 协议事件。各事件字段未列出部分参考 [CQHTTP 文档](https://github.com/howmanybots/onebot/blob/master/README.md)
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `MessageEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.Event`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
消息事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
### `to_me`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
* **说明**
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
消息是否与机器人有关
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
* **类型**
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
`bool`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
### `reply`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
* **说明**
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
消息中提取的回复消息,内容为 `get_msg` API 返回结果
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
* **类型**
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
`Optional[Reply]`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `PrivateMessageEvent`
基类:`nonebot.adapters.cqhttp.event.MessageEvent`
私聊消息
## _class_ `GroupMessageEvent`
基类:`nonebot.adapters.cqhttp.event.MessageEvent`
群消息
## _class_ `NoticeEvent`
基类:`nonebot.adapters.cqhttp.event.Event`
通知事件
## _class_ `GroupUploadNoticeEvent`
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
群文件上传事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `GroupAdminNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群管理员变动
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `GroupDecreaseNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群成员减少事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `GroupIncreaseNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群成员增加事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `GroupBanNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群禁言事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `FriendAddNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
好友添加事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `GroupRecallNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群消息撤回事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `FriendRecallNoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
好友消息撤回事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `NotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NoticeEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
提醒事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `PokeNotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
戳一戳提醒事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `LuckyKingNotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群红包运气王提醒事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `HonorNotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.NotifyEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
群荣誉变更提醒事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `RequestEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.Event`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
请求事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `FriendRequestEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.RequestEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
加好友请求事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `GroupRequestEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.RequestEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
加群请求/邀请事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `MetaEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.Event`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
元事件
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
## _class_ `LifecycleMetaEvent`
2020-12-03 08:04:14 +00:00
2020-12-13 04:53:33 +00:00
基类:`nonebot.adapters.cqhttp.event.MetaEvent`
生命周期元事件
## _class_ `HeartbeatMetaEvent`
基类:`nonebot.adapters.cqhttp.event.MetaEvent`
心跳元事件
## `get_event_model(event_name)`
* **说明**
根据事件名获取对应 `Event Model``FallBack Event Model` 列表
* **返回**
* `List[Type[Event]]`
2020-12-03 08:04:14 +00:00
## _class_ `MessageSegment`
2020-12-06 16:52:26 +00:00
基类:[`nonebot.adapters.MessageSegment`](README.md#nonebot.adapters.MessageSegment)
2020-12-03 08:04:14 +00:00
CQHTTP 协议 MessageSegment 适配。具体方法参考协议消息段类型或源码。
## _class_ `Message`
2020-12-06 16:52:26 +00:00
基类:[`nonebot.adapters.Message`](README.md#nonebot.adapters.Message)
2020-12-03 08:04:14 +00:00
CQHTTP 协议 Message 适配。