From 4a53161084ec11ddaaee0d5318d5ccd9895f8d58 Mon Sep 17 00:00:00 2001 From: issork Date: Sun, 3 Dec 2023 16:58:05 +0100 Subject: [PATCH] fixed eventsub --- addons/gift/api_connection.gd | 2 +- addons/gift/eventsub_connection.gd | 21 ++++++++++++++++----- example/Example.gd | 6 +++--- project.godot | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/addons/gift/api_connection.gd b/addons/gift/api_connection.gd index 84ffc4b..1ae0ac4 100644 --- a/addons/gift/api_connection.gd +++ b/addons/gift/api_connection.gd @@ -23,7 +23,7 @@ func poll() -> void: client_response.clear() func request(method : int, url : String, headers : PackedStringArray, body : String = "") -> Dictionary: - client.request(method, url, headers) + client.request(method, url, headers, body) var response = await(received_response) match (client.get_response_code()): 401: diff --git a/addons/gift/eventsub_connection.gd b/addons/gift/eventsub_connection.gd index 56b69e8..f988be5 100644 --- a/addons/gift/eventsub_connection.gd +++ b/addons/gift/eventsub_connection.gd @@ -1,7 +1,7 @@ class_name TwitchEventSubConnection extends RefCounted -const TEN_MINUTES_MS : int = 600000 +const TEN_MINUTES_S : int = 600 # The id has been received from the welcome message. signal session_id_received(id) @@ -66,12 +66,21 @@ func poll() -> void: connection_state = ConnectionState.DISCONNECTED print("Connection closed! [%s]: %s"%[websocket.get_close_code(), websocket.get_close_reason()]) websocket = null + var t : int = Time.get_ticks_msec() / 1000 - TEN_MINUTES_S + if (last_cleanup < t): + last_cleanup = Time.get_ticks_msec() / 1000 + var to_remove : Array = [] + for msg in eventsub_messages: + if (eventsub_messages[msg] < Time.get_unix_time_from_system() - TEN_MINUTES_S): + to_remove.append(msg) + for e in to_remove: + eventsub_messages.erase(e) func process_event(data : PackedByteArray) -> void: var msg : Dictionary = JSON.parse_string(data.get_string_from_utf8()) - if (eventsub_messages.has(msg["metadata"]["message_id"]) || msg["metadata"]["message_timestamp"]): + if (eventsub_messages.has(msg["metadata"]["message_id"]) || Time.get_unix_time_from_datetime_string(msg["metadata"]["message_timestamp"]) < Time.get_unix_time_from_system() - TEN_MINUTES_S): return - eventsub_messages[msg["metadata"]["message_id"]] = Time.get_ticks_msec() + eventsub_messages[msg["metadata"]["message_id"]] = Time.get_unix_time_from_datetime_string(msg["metadata"]["message_timestamp"]) var payload : Dictionary = msg["payload"] last_keepalive = Time.get_ticks_msec() match msg["metadata"]["message_type"]: @@ -93,10 +102,10 @@ func process_event(data : PackedByteArray) -> void: # Refer to https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/ for details on # which API versions are available and which conditions are required. -func subscribe_event(event_name : String, version : int, conditions : Dictionary) -> void: +func subscribe_event(event_name : String, version : String, conditions : Dictionary) -> void: var data : Dictionary = {} data["type"] = event_name - data["version"] = str(version) + data["version"] = version data["condition"] = conditions data["transport"] = { "method":"websocket", @@ -106,4 +115,6 @@ func subscribe_event(event_name : String, version : int, conditions : Dictionary if (response.has("error")): print("Subscription failed for event '%s'. Error %s (%s): %s" % [event_name, response["status"], response["error"], response["message"]]) return + elif (response.is_empty()): + return print("Now listening to '%s' events." % event_name) diff --git a/example/Example.gd b/example/Example.gd index a026a12..f0e9aa3 100644 --- a/example/Example.gd +++ b/example/Example.gd @@ -76,14 +76,14 @@ func _ready() -> void: # This part of the example only works if GIFT is logged in to your broadcaster account. # If you are, you can uncomment this to also try receiving follow events. + # Don't forget to also add the 'moderator:read:followers' scope to your token. # eventsub = TwitchEventSubConnection.new(api) -# eventsub.connect_to_eventsub() +# await(eventsub.connect_to_eventsub()) # eventsub.event.connect(on_event) # var user_ids : Dictionary = await(api.get_users_by_name([username])) -# print(user_ids) # if (user_ids.has("data") && user_ids["data"].size() > 0): # var user_id : String = user_ids["data"][0]["id"] -# eventsub.subscribe_event("channel.follow", 2, {"broadcaster_user_id": user_id, "moderator_user_id": user_id}) +# eventsub.subscribe_event("channel.follow", "2", {"broadcaster_user_id": user_id, "moderator_user_id": user_id}) func hello(cmd_info : CommandInfo) -> void: irc.chat("Hello World!") diff --git a/project.godot b/project.godot index 5ae4ad4..3152716 100644 --- a/project.godot +++ b/project.godot @@ -11,7 +11,7 @@ config_version=5 [application] config/name="GIFT" -run/main_scene="res://Example.tscn" +run/main_scene="res://example/Example.tscn" config/features=PackedStringArray("4.1") [debug]