Raxol.Gateway.Adapter.Discord.Protocol (RaxolGateway v0.2.0)

Copy Markdown View Source

Pure codec for Discord Gateway v10 JSON frames.

Stateless: encodes the three client payloads the socket sends (heartbeat, identify, resume) and classifies inbound frames into tagged tuples. All connection state (sequence numbers, session ids, timers) lives in Raxol.Gateway.Adapter.Discord.GatewaySocket.

Dispatch frames (op 0) are returned whole and string-keyed, exactly as decoded, so the socket can hand them to its :on_event sink unmodified.

Summary

Functions

Classify one inbound text frame.

The default intent bitfield: GUILDS, GUILD_MESSAGES, DIRECT_MESSAGES, and MESSAGE_CONTENT.

Encode a heartbeat (op 1) carrying the last seen sequence (or nil).

Encode an identify (op 2) with token and intents.

Encode a resume (op 6) for an interrupted session.

Types

decoded()

@type decoded() ::
  {:dispatch, map()}
  | {:hello, pos_integer()}
  | :heartbeat_request
  | :heartbeat_ack
  | :reconnect
  | {:invalid_session, resumable? :: boolean()}
  | {:unknown, term()}
  | {:error, term()}

Functions

decode(payload)

@spec decode(String.t()) :: decoded()

Classify one inbound text frame.

default_intents()

@spec default_intents() :: pos_integer()

The default intent bitfield: GUILDS, GUILD_MESSAGES, DIRECT_MESSAGES, and MESSAGE_CONTENT.

MESSAGE_CONTENT is privileged: it must also be enabled for the bot in the Discord developer portal, or guild message content arrives empty.

encode_heartbeat(seq)

@spec encode_heartbeat(integer() | nil) :: String.t()

Encode a heartbeat (op 1) carrying the last seen sequence (or nil).

encode_identify(token, intents)

@spec encode_identify(String.t(), pos_integer()) :: String.t()

Encode an identify (op 2) with token and intents.

encode_resume(token, session_id, seq)

@spec encode_resume(String.t(), String.t(), integer()) :: String.t()

Encode a resume (op 6) for an interrupted session.