Skip to main content
Version: 2.2.1

nonebot.drivers

本模块定义了驱动适配器基类。

各驱动请继承以下基类。

abstract class Mixin(<auto>)

  • 说明: 可与其他驱动器共用的混入基类。

  • 参数

    auto

abstract property type

  • 类型: str

  • 说明: 混入驱动类型名称

abstract class Driver(env, config)

  • 说明

    驱动器基类。

    驱动器控制框架的启动和停止,适配器的注册,以及机器人生命周期管理。

  • 参数

    • env (Env): 包含环境信息的 Env 对象

    • config (Config): 包含配置信息的 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

  • 类型: untyped

  • 说明: 驱动专属 logger 日志记录器

abstract method run(*args, **kwargs)

  • 说明: 启动驱动框架

  • 参数

    • *args

    • **kwargs

  • 返回

    • untyped

method on_startup(func)

  • 说明: 注册一个启动时执行的函数

  • 参数

    • func (LIFESPAN_FUNC)
  • 返回

    • LIFESPAN_FUNC

method on_shutdown(func)

  • 说明: 注册一个停止时执行的函数

  • 参数

    • func (LIFESPAN_FUNC)
  • 返回

    • LIFESPAN_FUNC

classmethod on_bot_connect(func)

classmethod on_bot_disconnect(func)

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 ASGIMixin(<auto>)

  • 说明

    ASGI 服务端基类。

    将后端框架封装,以满足适配器使用。

  • 参数

    auto

abstract property server_app

  • 类型: Any

  • 说明: 驱动 APP 对象

abstract property asgi

  • 类型: Any

  • 说明: 驱动 ASGI 对象

abstract method setup_http_server(setup)

  • 说明: 设置一个 HTTP 服务器路由配置

  • 参数

  • 返回

    • None

abstract method setup_websocket_server(setup)

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 class ReverseMixin(<auto>)

  • 说明: 服务端混入基类。

  • 参数

    auto

var ForwardDriver

  • 类型: ForwardMixin

  • 说明

    支持客户端请求的驱动器。

    Deprecated,请使用 ForwardMixin 或其子类代替。

var ReverseDriver

  • 类型: ReverseMixin

  • 说明

    支持服务端请求的驱动器。

    Deprecated,请使用 ReverseMixin 或其子类代替。

def combine_driver(driver, *mixins)

  • 说明: 将一个驱动器和多个混入类合并。

  • 重载

    1. (driver) -> type[D]

    • 参数

      • driver (type[D])
    • 返回

      • type[D]

    2. (driver, *mixins) -> type[CombinedDriver]

    • 参数

      • driver (type[D])

      • *mixins (type[Mixin])

    • 返回

      • type[CombinedDriver]

abstract class HTTPClientMixin(<auto>)

  • 说明: HTTP 客户端混入基类。

  • 参数

    auto

abstract async method request(setup)

class HTTPServerSetup(<auto>)

  • 说明: HTTP 服务器路由配置。

  • 参数

    auto

abstract class WebSocketClientMixin(<auto>)

  • 说明: WebSocket 客户端混入基类。

  • 参数

    auto

abstract method websocket(setup)

  • 说明: 发起一个 WebSocket 连接

  • 参数

  • 返回

class WebSocketServerSetup(<auto>)

  • 说明: WebSocket 服务器路由配置。

  • 参数

    auto