8.3 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	sidebar_position, description
| sidebar_position | description | 
|---|---|
| 0 | nonebot.drivers 模块 | 
nonebot.drivers
本模块定义了驱动适配器基类。
各驱动请继承以下基类。
abstract class Driver(env, config)
instance-var env
- 
类型: str
 - 
说明: 环境名称
 
instance-var config
- 
类型: Config
 - 
说明: 全局配置对象
 
property bots
- 
类型: dict[str, Bot]
 - 
说明: 获取当前所有已连接的 Bot
 
method register_adapter(adapter, **kwargs)
- 
说明: 注册一个协议适配器
 - 
参数
- 
adapter(type[Adapter]): 适配器类 - 
**kwargs: 其他传递给适配器的参数 
 - 
 - 
返回
- None
 
 
abstract property type
- 
类型: str
 - 
说明: 驱动类型名称
 
abstract property logger
- 
类型:
 - 
说明: 驱动专属 logger 日志记录器
 
abstract method run(*args, **kwargs)
- 
说明: 启动驱动框架
 - 
参数
- 
*args - 
**kwargs 
 - 
 - 
返回
- untyped
 
 
abstract method on_startup(func)
- 
说明: 注册一个在驱动器启动时执行的函数
 - 
参数
func(Callable)
 - 
返回
- Callable
 
 
abstract method on_shutdown(func)
- 
说明: 注册一个在驱动器停止时执行的函数
 - 
参数
func(Callable)
 - 
返回
- Callable
 
 
classmethod on_bot_connect(func)
- 
说明
装饰一个函数使他在 bot 连接成功时执行。
钩子函数参数:
- bot: 当前连接上的 Bot 对象
 
 - 
参数
func(T_BotConnectionHook)
 - 
返回
 
classmethod on_bot_disconnect(func)
- 
说明
装饰一个函数使他在 bot 连接断开时执行。
钩子函数参数:
- bot: 当前连接上的 Bot 对象
 
 - 
参数
func(T_BotDisconnectionHook)
 - 
返回
 
class Cookies(cookies=None)
- 
参数
cookies(CookieTypes)
 
method set(name, value, domain="", path="/")
- 
参数
- 
name(str) - 
value(str) - 
domain(str) - 
path(str) 
 - 
 - 
返回
- None
 
 
method get(name, default=None, domain=None, path=None)
- 
参数
- 
name(str) - 
default(str | None) - 
domain(str | None) - 
path(str | None) 
 - 
 - 
返回
- str | None
 
 
method delete(name, domain=None, path=None)
- 
参数
- 
name(str) - 
domain(str | None) - 
path(str | None) 
 - 
 - 
返回
- None
 
 
method clear(domain=None, path=None)
- 
参数
- 
domain(str | None) - 
path(str | None) 
 - 
 - 
返回
- None
 
 
method update(cookies=None)
- 
参数
cookies(CookieTypes)
 - 
返回
- None
 
 
method as_header(request)
- 
参数
request(Request)
 - 
返回
- dict[str, str]
 
 
class Request(method, url, *, params=None, headers=None, cookies=None, content=None, data=None, json=None, files=None, version=HTTPVersion.H11, timeout=None, proxy=None)
- 
参数
- 
method(str | bytes) - 
url(URL | str | RawURL) - 
params(QueryTypes) - 
headers(HeaderTypes) - 
cookies(CookieTypes) - 
content(ContentTypes) - 
data(DataTypes) - 
json(Any) - 
files(FilesTypes) - 
version(str | HTTPVersion) - 
timeout(float | None) - 
proxy(str | None) 
 - 
 
class Response(status_code, *, headers=None, content=None, request=None)
- 
参数
- 
status_code(int) - 
headers(HeaderTypes) - 
content(ContentTypes) - 
request(Request | None) 
 - 
 
abstract class WebSocket(*, request)
- 
参数
request(Request)
 
abstract property closed
- 
类型: bool
 - 
说明: 连接是否已经关闭
 
abstract async method accept()
- 
说明: 接受 WebSocket 连接请求
 - 
参数
empty
 - 
返回
- None
 
 
abstract async method close(code=1000, reason="")
- 
说明: 关闭 WebSocket 连接请求
 - 
参数
- 
code(int) - 
reason(str) 
 - 
 - 
返回
- None
 
 
abstract async method receive()
- 
说明: 接收一条 WebSocket text/bytes 信息
 - 
参数
empty
 - 
返回
- str | bytes
 
 
abstract async method receive_text()
- 
说明: 接收一条 WebSocket text 信息
 - 
参数
empty
 - 
返回
- str
 
 
abstract async method receive_bytes()
- 
说明: 接收一条 WebSocket binary 信息
 - 
参数
empty
 - 
返回
- bytes
 
 
async method send(data)
- 
说明: 发送一条 WebSocket text/bytes 信息
 - 
参数
data(str | bytes)
 - 
返回
- None
 
 
abstract async method send_text(data)
- 
说明: 发送一条 WebSocket text 信息
 - 
参数
data(str)
 - 
返回
- None
 
 
abstract async method send_bytes(data)
- 
说明: 发送一条 WebSocket binary 信息
 - 
参数
data(bytes)
 - 
返回
- None
 
 
enum HTTPVersion
- 
说明: An enumeration.
 - 
参数
auto
- 
H10: '1.0' - 
H11: '1.1' - 
H2: '2' 
 - 
 
abstract class ForwardMixin(<auto>)
- 
说明: 客户端混入基类。
 - 
参数
auto
 
abstract property type
- 
类型: str
 - 
说明: 客户端驱动类型名称
 
abstract async method request(setup)
abstract method websocket(setup)
abstract class ForwardDriver(env, config)
abstract class ReverseDriver(env, config)
abstract property server_app
- 
类型: Any
 - 
说明: 驱动 APP 对象
 
abstract property asgi
- 
类型: Any
 - 
说明: 驱动 ASGI 对象
 
abstract method setup_http_server(setup)
- 
说明: 设置一个 HTTP 服务器路由配置
 - 
参数
setup(HTTPServerSetup)
 - 
返回
- None
 
 
abstract method setup_websocket_server(setup)
- 
说明: 设置一个 WebSocket 服务器路由配置
 - 
参数
setup(WebSocketServerSetup)
 - 
返回
- None
 
 
def combine_driver(driver, *mixins)
- 
说明: 将一个驱动器和多个混入类合并。
 - 
参数
- 
driver(type[Driver]) - 
*mixins(type[ForwardMixin]) 
 - 
 - 
返回
- type[Driver]
 
 
class HTTPServerSetup(<auto>)
- 
说明: HTTP 服务器路由配置。
 - 
参数
auto
 
class WebSocketServerSetup(<auto>)
- 
说明: WebSocket 服务器路由配置。
 - 
参数
auto