Skip to content

Commit

Permalink
fixed eventsub
Browse files Browse the repository at this point in the history
  • Loading branch information
issork committed Dec 3, 2023
1 parent e7ce70d commit 4a53161
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion addons/gift/api_connection.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 16 additions & 5 deletions addons/gift/eventsub_connection.gd
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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"]:
Expand All @@ -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",
Expand All @@ -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)
6 changes: 3 additions & 3 deletions example/Example.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4a53161

Please sign in to comment.