curious.core.gateway

Websocket gateway code.

Functions

open_websocket(token, url, *[, shard_id, …]) Opens a new connection to Discord.

Classes

GatewayHandler(gw_state) Represents a gateway handler - something that is connected to Discord’s websocket and handles incoming events and parses them as appropriate.
GatewayOp Represents the opcode mapping for the gateway.
HeartbeatStats([heartbeats, heartbeat_acks, …]) Represents the statistics for the gateway’s heartbeat counters.
class curious.core.gateway.GatewayHandler(gw_state: curious.core.gateway._GatewayState)[source]

Bases: object

Represents a gateway handler - something that is connected to Discord’s websocket and handles incoming events and parses them as appropriate.

You don’t want to create this class directly; use open_gateway() instead.

async with open_gateway("wss://gateway.discord.gg", token="token",
                        shard_id=0, shard_count=1) as gateway:
    async for event in gateway.events():
        ...
await _start_heatbeat_events(heartbeat_interval)[source]

Starts heartbeating.

Parameters:heartbeat_interval (float) – The number of seconds between each heartbeat.
await _stop_heartbeat_events()[source]

Cancels any current heartbeat events.

Return type:None
await close(code=1000, reason='Client closed connection', *, reconnect=False, clear_session_id=True)[source]

Close the current websocket connection.

Parameters:
  • code (int) – The close code.
  • reason (str) – The close reason.
  • reconnect (bool) – If we should reconnect.
  • clear_session_id (bool) – If we should clear the session ID.
async for ... in events()[source]

Returns an async generator used to iterate over the events received by this websocket.

Return type:AsyncGenerator[None, Any]
async for ... in handle_data_event(evt)[source]

Handles a data event.

logger
Return type:Logger
Returns:The gateway-specific logger.
await open()[source]

Opens a new connection to Discord.

Warning

This only opens the websocket.

Return type:None
await send(data)[source]

Sends data down the websocket.

Return type:None
await send_guild_chunks(guild_ids)[source]

Sends GUILD_MEMBER_CHUNK packets to Discord.

Return type:None
await send_heartbeat()[source]

Sends a heartbeat to Discord.

Return type:None
await send_identify()[source]

Sends an IDENTIFY to Discord.

Return type:None
await send_resume()[source]

Sends a RESUME to Discord, attempting to resume the connection from where we left off.

Return type:None
await send_status(*, status=None, name=None, url=None, type_=None, afk=None)[source]

Sends a PRESENCE_UPDATE.

Parameters:
  • status (Optional[int]) – The int status to send.
  • name (Optional[str]) – The name of the status to send.
  • url (Optional[str]) – The URL to include if applicable.
  • type (Optional[int]) – The type of the status to send.
  • afk (Optional[bool]) – If the account is to be marked as AFK.
class curious.core.gateway.GatewayOp[source]

Bases: enum.IntEnum

Represents the opcode mapping for the gateway.

class curious.core.gateway.HeartbeatStats(heartbeats=0, heartbeat_acks=0, last_heartbeat_time=0, last_ack_time=0)[source]

Bases: object

Represents the statistics for the gateway’s heartbeat counters.

gw_time
Return type:float
Returns:The time the most recent heartbeat and heartbeat_ack.
class curious.core.gateway._GatewayState(token, gateway_url, shard_id, shard_count, session_id=None, sequence=0)[source]

Bases: object

Represents the gateway state for the current gateway.

async with curious.core.gateway.open_websocket(token, url, *, shard_id=0, shard_count=1)[source]

Opens a new connection to Discord.

This is an async context manager; for example, using Trio for nursery management:

async with trio.open_nursery() as nursery:
    async with open_websocket(token, url, task_group=nursery) as gateway:
        # example for changing presence
        nursery.start_soon(some_gw_handler, gateway)

        async for event in gateway.events():
            # handle events, etc.
            ...
Parameters:
  • token (str) – The token to connect to Discord with.
  • url (str) – The gateway URL to connect with.
  • shard_id (int) – The shard ID to connect with. Defaults to 0.
  • shard_count (int) – The number of shards to boot with.
Return type:

AsyncContextManager[GatewayHandler]

Returns:

An async context manager that yields a GatewayHandler.