Class: Async::Discord::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/async/discord/gateway.rb

Overview

WebSocket client for the Discord Gateway (v10).

Connects to Discord’s WebSocket gateway, authenticates with a bot token, manages heartbeating, and dispatches incoming events to registered handlers.

Supports session resumption on reconnect.

gateway = Async::Discord::Gateway.new(token: "MTk...", intents: 0x30001)
gateway.on("MESSAGE_CREATE") { |data| puts data["content"] }
gateway.on("READY") { |data| puts "Connected as #{data["user"]["username"]}" }
gateway.run  # blocks, runs inside Async reactor

Constant Summary collapse

GATEWAY_URL =
"wss://gateway.discord.gg/?v=10&encoding=json"
DISPATCH =

Discord Gateway opcodes

0
HEARTBEAT =
1
IDENTIFY =
2
PRESENCE_UPDATE =
3
VOICE_STATE_UPDATE =
4
RESUME =
6
RECONNECT =
7
REQUEST_GUILD_MEMBERS =
8
INVALID_SESSION =
9
HELLO =
10
HEARTBEAT_ACK =
11

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, intents:, gateway_url: GATEWAY_URL) ⇒ Gateway

Returns a new instance of Gateway.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/async/discord/gateway.rb', line 46

def initialize(token:, intents:, gateway_url: GATEWAY_URL)
  @token       = token
  @intents     = intents
  @gateway_url = gateway_url
  @handlers    = Hash.new { |h, k| h[k] = [] }
  @seq         = nil
  @session_id  = nil
  @resume_url  = nil
  @heartbeat_interval = nil
  @heartbeat_acked    = true
  @connection  = nil
  @running     = false
end

Instance Attribute Details

#seqObject (readonly)

Returns the value of attribute seq.



44
45
46
# File 'lib/async/discord/gateway.rb', line 44

def seq
  @seq
end

#session_idObject (readonly)

Returns the value of attribute session_id.



44
45
46
# File 'lib/async/discord/gateway.rb', line 44

def session_id
  @session_id
end

Instance Method Details

#on(event_type, &block) ⇒ Object

Register a handler for a Discord event type.

gateway.on("MESSAGE_CREATE") { |data| ... }
gateway.on("READY") { |data| ... }


65
66
67
# File 'lib/async/discord/gateway.rb', line 65

def on(event_type, &block)
  @handlers[event_type] << block
end

#runObject

Connect to the gateway and run the event loop. Blocks until the connection is closed or an unrecoverable error occurs. Must be called inside an Async reactor.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/async/discord/gateway.rb', line 72

def run
  @running = true

  while @running
    begin
      connect_and_run
    rescue => e
      Console.error(self) { "Gateway error: #{e.class}: #{e.message}" }
      break unless @running
      sleep(5)
    end
  end
end

#send_payload(op:, d: nil) ⇒ Object

Send a raw payload to the gateway.



93
94
95
96
97
98
99
# File 'lib/async/discord/gateway.rb', line 93

def send_payload(op:, d: nil)
  return unless @connection

  payload = {op: op, d: d}
  @connection.write(payload.to_json)
  @connection.flush
end

#stopObject

Gracefully stop the gateway.



87
88
89
90
# File 'lib/async/discord/gateway.rb', line 87

def stop
  @running = false
  @connection&.close
end