extends HTTPRequest var token := "yourtokenhere" #make sure to actually replace this with your token! var client : WebSocketClient var heartbeat_interval : float var last_sequence : float func _ready() -> void: client = WebSocketClient.new() client.connect_to_url("wss://gateway.discord.gg/?v=6&encoding=json") client.connect("connection_established", self, "_connection_established") client.connect("data_received", self, "_data_received") func _process(delta : float) -> void: #check if the client is not disconnected, there's no point to poll it if it is if client.get_connection_status() != NetworkedMultiplayerPeer.CONNECTION_DISCONNECTED: client.poll() func _connection_established(protocol : String) -> void: print("We are connected!") func _data_received() -> void: var packet := client.get_peer(1).get_packet() var data := packet.get_string_from_utf8() var json_parsed := JSON.parse(data) var dict : Dictionary = json_parsed.result var op = str(dict["op"]) #convert it to string for easier checking match op: "0": #Opcode 0 (Events) print(dict["t"]) last_sequence = dict["s"] "10": #Opcode 10 Hello #Set our timer heartbeat_interval = dict["d"]["heartbeat_interval"] $Timer.wait_time = heartbeat_interval / 1000 $Timer.start() #Send Opcode 2 Identify to the Gateway var d = { "op" : 2, "d" : { "token": token, "properties": {} } } send_dictionary_as_packet(d) "11": #Opcode 11 Heartbeat ACK print("We've received a Heartbeat ACK from the gateway.") func _on_Timer_timeout() -> void: #Send Opcode 1 Heartbeat payloads every heartbeat_interval var d = {"op" : 1, "d" : last_sequence} send_dictionary_as_packet(d) print("We've send a Heartbeat to the gateway.") func send_dictionary_as_packet(d : Dictionary) -> void: var query = to_json(d) client.get_peer(1).put_packet(query.to_utf8())