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

Handles real-time Websocket (WS) connection to Discord's Gateway. Connects to Discord's WS, handles identify/resume logic, and maintains heartbeat.

token = token instance-attribute

The bot's token.

_logger = logger instance-attribute

Logger instance to log events.

ws = None instance-attribute

Websocket instance.

heartbeat = None instance-attribute

Heartbeat task instance.

sequence = None instance-attribute

Discord-generated sequence number for this websocket connection.

session_id = None instance-attribute

Discord-generated session ID for this websocket connection.

intents = intents instance-attribute

User-defined bot intents (for identify).

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

Discord WS query params.

connect_url = f'wss://gateway.discord.gg/' instance-attribute

URL to connect to Discord's gateway.

connect async

Established websocket connection to Discord.

receive async

Receives and logs messages from the gateway.

Returns:

  • dict

    parsed JSON data

send async

Sends a JSON-encoded message to the gateway.

Parameters:

  • message (dict) –

    the message to send

send_heartbeat_loop async

Background task that sends heartbeat payloads in regular intervals. Retries until cancelled.

identify async

Sends the IDENIFY payload (token, intents, connection properties). Must be sent after connecting to the WS.

start_heartbeat async

Waits for initial HELLO event, hydrates the HelloEvent class, and begins the heartbeat.

reconnect async

Sends RESUME payload to reconnect with the same session ID and sequence number as provided by Discord.

close async

Cancels heart beat and cleanly closes WS with error handling.


HelloEvent dataclass

Bases: DataModel

Heartbeat interval event.

heartbeat_interval: int instance-attribute

Heartbeat interval in milliseconds.