Skip to content

Gateway

Internal API

These classes are for internal use only.

Users should interact with Client and other public API classes instead.


GatewayClient

Initialize this websocket.

Parameters:

  • gateway_url (str) –

    gateway URL provided by GET /gateway/bot endpoint

  • shard_id (int) –

    assigned shard ID

  • total_shards (int) –

    total shard count provided by GET /gateway/bot endpoint

shard_id = shard_id instance-attribute

total_shards = total_shards instance-attribute

ws = None instance-attribute

seq = None instance-attribute

session_id = None instance-attribute

allow_resume = False instance-attribute

reconnect_immediately = False instance-attribute

_ws_closed = False instance-attribute

backoff = MIN_BACKOFF instance-attribute

metrics = GatewayMetrics(0.0, 0.0) instance-attribute

heartbeat_task = None instance-attribute

heartbeat_interval = None instance-attribute

event_queue = asyncio.Queue() instance-attribute

base_url = gateway_url instance-attribute

url_params = '?v=10&encoding=json' instance-attribute

wait_reconnect async

Sleep for exponentially increasing time between reconnects.

start async

Start this websocket's connection.

Parameters:

  • token (str) –

    the bot's token

  • intents (int) –

    the bot's intents

connect_ws async

Connect to Discord's Gateway (websocket).

send async

Send data through the websocket.

Parameters:

  • data (dict) –

    data to send

receive async

Receive data through the websocket.

Returns:

  • dict

    websocket data

heartbeat async

Heartbeat task to keep connection alive.

identify async

Send an IDENTIFY payload to handshake for bot.

Parameters:

  • token (str) –

    the bot's token

  • intents (int) –

    the bot's intents

Raises:

  • ConnectionError

    invalid intents

resume async

Send a RESUME payload to resume a connection.

Parameters:

  • token (str) –

    the bot's token

_listen async

Listen for events and queue them to be consumed by Client.

Raises:

  • ConnectionError

    an error occurred

close_ws async

Close the websocket connection if one is still open and cancels heartbeat.