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.



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

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.



42
43
44
# File 'lib/async/discord/gateway.rb', line 42

def seq
  @seq
end

#session_idObject (readonly)

Returns the value of attribute session_id.



42
43
44
# File 'lib/async/discord/gateway.rb', line 42

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| ... }


63
64
65
# File 'lib/async/discord/gateway.rb', line 63

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.



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

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.



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

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.



85
86
87
88
# File 'lib/async/discord/gateway.rb', line 85

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