EmoteWall/addons/TwitchGod/twitch_god.gd

83 lines
1.8 KiB
GDScript3
Raw Normal View History

2024-10-22 02:14:11 +00:00
extends Node
@onready var http_client = $"HttpClient"
@onready var websocket_client = $"WebsocketClient"
@onready var http_server = $"HttpServer"
@onready var auth = $"Auth"
const API_URL = "https://api.twitch.tv/helix"
var access_token = null
# -- Built-in Methods
func _ready():
print("READY")
http_server.started.connect(_on_server_started)
http_server.received.connect(_on_server_received)
websocket_client.opened.connect(_on_websocket_opened)
http_client.request_completed.connect(_on_request_completed)
http_server.start()
# -- Private Methods
func _on_server_started():
websocket_client.open()
func _on_websocket_opened():
http_server.auth(
{
"response_type": "code",
"client_id": auth.CLIENT_ID,
"redirect_uri": "http://localhost:%d" % [auth.PORT],
"scope": auth.SCOPE
}
)
func _on_server_received(auth2):
http_client.request(
HttpClient.API_TYPE.AUTH,
{
"grant_type": "authorization_code",
"client_id": auth.CLIENT_ID,
"redirect_uri": "http://localhost:%d" % [auth.PORT],
"code": auth2,
"client_secret": auth.CLIENT_SECRET
}
)
func _on_request_completed(type: HttpClient.API_TYPE, data: Dictionary):
match type:
HttpClient.API_TYPE.AUTH:
http_client.request(
HttpClient.API_TYPE.VERIFY,
{
"token": data.access_token
}
)
HttpClient.API_TYPE.VERIFY:
if not websocket_client.id:
push_error("Tried to use nonexistent websocket id. Please report to Ategon.")
return
for sub in auth.SUBS:
var event = TwitchEvents.get_event_from_type(sub)
var sub_data = event.generate_sub({
"broadcaster": auth.BROADCASTER_ID,
"user": auth.USER_ID,
"websocket": websocket_client.id
})
http_client.request(
HttpClient.API_TYPE.CREATE_EVENTSUB,
{},
sub_data
)