miggor-StreamGraph/addons/no_twitch/twitch_connection.gd

407 lines
11 KiB
GDScript3
Raw Permalink Normal View History

extends Node
class_name No_Twitch
## Handler for a Connection to a Single Twitch Account
##
## Used for getting authentication and handling websocket connections.
signal token_received(token : String)
signal twitch_error(error : String)
signal invalid_auth()
signal chat_received(chat_dict)
signal chat_received_rich(chat_dict)
@export var client_id := "qyjg1mtby1ycs5scm1pvctos7yvyc1"
@export var redirect_uri := "http://localhost"
2024-01-26 11:26:22 +01:00
## Port that the redirect_uri will head to on your local system. Defaults to 80 for most cases.
## Linux tends to prefer 8000 or possibly 1338
@export var redirect_port := "8000"
var auth_url := "https://id.twitch.tv/oauth2/authorize?response_type=token&"
var auth_scopes : Array[String] = ["chat:read", "chat:edit", "channel:manage:redemptions", "bits:read", "channel:read:goals", "channel:manage:polls", "channel:read:hype_train", "channel:manage:predictions", "moderator:read:followers", "user:read:chat", "channel:read:subscriptions", "channel:moderate", "channel:read:charity", "moderator:read:shield_mode", "moderator:manage:shoutouts"]
var auth_server : TCPServer
const chat_socket_class = preload("res://addons/no_twitch/chat_socket.gd")
const eventsub_socket_class = preload("res://addons/no_twitch/eventsub_socket.gd")
## [Websocket_Client] used for receiving all data from Twitch pertaining to chat (Ex. IRC Chat Messages)
var chat_socket : Websocket_Client
## [Websocket_Client] handles all the data from Twitch pertaining to EventSub (Ex. General Alerts)
var eventsub_socket : Websocket_Client
var state := make_state()
var token : String
## The User data of the account that the authorized [member token] is connected to.
var user_info : Dictionary
var responses : Array
func _ready():
2024-01-26 11:26:22 +01:00
token_received.connect(cache_user_data.unbind(1))
2024-01-26 11:26:22 +01:00
## Function that handles Caching the data of the Account tied to the Connections Token
func cache_user_data():
if token.is_empty():
return
var resp = request_user_info()
resp.response_received.connect(func(data):
# Error handling here.
if data.has("error") and not data.error.is_empty(): # If we have an error.
invalid_auth.emit()
# Return out.
return
user_info = data.data
print("User Info Cached")
)
2024-01-26 11:26:22 +01:00
## Handles the basic Twitch Authentication process to request and then later receive a Token
## (using [method check_auth_peer]). Returns the authentication URL.
func authenticate_with_twitch(client_id = client_id, scopes : Array[String] = auth_scopes) -> String:
auth_server = TCPServer.new()
auth_scopes = scopes
var url := create_auth_url()
auth_server.listen(int(redirect_port))
return url
2024-01-26 11:26:22 +01:00
## Sets up the chat connection. Joining 1 room with [param token] as it's "PASS" specifying what
## account it's on. And the optional [param default_chat] specifying the default room to join.
## While [param nick] specifies the "nickname" (Not the username on Twitch)
func setup_chat_connection(default_chat : String = "", token : String = token, request_twitch_info = true, nick = "terribletwitch"):
2024-01-26 11:26:22 +01:00
chat_socket = chat_socket_class.new(self)
2024-01-26 11:26:22 +01:00
# Connects to the Twitch IRC server.
chat_socket.connect_to_chat(token, request_twitch_info)
await chat_socket.socket_open
2024-01-26 11:26:22 +01:00
if !default_chat.is_empty():
2024-01-26 11:26:22 +01:00
chat_socket.join_chat(default_chat)
2024-01-26 11:26:22 +01:00
## Joins the given [param channel] over IRC. Essentially just sending JOIN #[param channel]
func join_channel(channel : String):
2024-01-26 11:26:22 +01:00
chat_socket.join_chat(channel)
2024-01-26 11:26:22 +01:00
## Leaves or "PART"s the given [param channel] over IRC. (Sends PART #[param channel])
func leave_channel(channel : String):
chat_socket.leave_chat(channel)
func send_chat(msg : String, channel : String = ""):
2024-01-26 11:26:22 +01:00
chat_socket.send_chat(msg, channel)
2024-01-26 11:26:22 +01:00
## Sets up an EventSub connection to allow subscribing to EventSub events. Ex. Alerts, Channel Point
## Redemptions etc.
func setup_eventsub_connection(events : Array[EventSub_Subscription] = [], timeout_duration : int = 20):
2024-01-26 11:26:22 +01:00
eventsub_socket = eventsub_socket_class.new(self, timeout_duration)
var ret = await eventsub_socket.connect_to_eventsub(events)
return ret
2024-01-26 11:26:22 +01:00
## Adds a new subscription to a given EventSub Event using a [No_Twitch.EventSub_Subscription]
2024-01-26 11:26:22 +01:00
## to handle the data.
func add_eventsub_subscription(sub_type : EventSub_Subscription):
2024-01-26 11:26:22 +01:00
if eventsub_socket == null:
return await setup_eventsub_connection([sub_type])
if sub_type.session_id == "":
sub_type.session_id = eventsub_socket.session_id
return twitch_request("https://api.twitch.tv/helix/eventsub/subscriptions", HTTPClient.METHOD_POST, str(sub_type.return_request_dictionary()))
2024-01-26 11:26:22 +01:00
func subscribe_to_channel_points(user_id : String):
2024-01-26 11:26:22 +01:00
if !eventsub_socket.socket_open:
2024-01-26 11:26:22 +01:00
push_error("No_Twitch Error: No EventSub Connection, please use setup_eventsub_connection first")
2024-01-26 11:26:22 +01:00
return
return await add_eventsub_subscription(EventSub_Subscription.new("channel.channel_points_custom_reward_redemption.add", {"broadcaster_user_id" : user_id}, eventsub_socket.session_id))
func _process(delta):
2024-01-26 11:26:22 +01:00
if auth_server and auth_server.is_listening() and auth_server.is_connection_available():
2024-01-26 11:26:22 +01:00
check_auth_peer(auth_server.take_connection())
2024-01-26 11:26:22 +01:00
if chat_socket:
2024-01-26 11:26:22 +01:00
chat_socket.poll_socket()
2024-01-26 11:26:22 +01:00
if eventsub_socket:
2024-01-26 11:26:22 +01:00
eventsub_socket.poll_socket()
2024-01-26 11:26:22 +01:00
## Utility function for creating a Twitch Authentication URL with the given
## [param scopes], Twitch Client ID ([param id]) and [param redirect_uri].
## [param id] defaults to [member client_id] and both [param redirect] and
## [param redirect_port]
func create_auth_url(port := redirect_port, id : String = client_id, redirect : String = redirect_uri) -> String:
2024-01-26 11:26:22 +01:00
var str_scopes : String
2024-01-26 11:26:22 +01:00
for all in auth_scopes:
2024-01-26 11:26:22 +01:00
str_scopes += " " + all
str_scopes = str_scopes.strip_edges()
2024-01-26 11:26:22 +01:00
var full_redirect_uri := redirect
if !port.is_empty():
full_redirect_uri += ":" + port
2024-01-26 11:26:22 +01:00
var url := auth_url + "client_id=" + id + "&redirect_uri=" + full_redirect_uri + "&scope=" + str_scopes + "&state=" + str(state)
2024-01-26 11:26:22 +01:00
return url
## Utility Function for making a generic HTTP Request to Twitch
func twitch_request(url : String, method : HTTPClient.Method = HTTPClient.METHOD_GET, body := "") -> HTTPResponse:
2024-01-26 11:26:22 +01:00
var headers : Array = ["Authorization: Bearer " + token, "Client-Id: " + client_id]
2024-01-26 11:26:22 +01:00
# Adds the Content type to the headers if we're actually sending some data along
if method != HTTPClient.METHOD_GET:
2024-01-26 11:26:22 +01:00
headers.append("Content-Type: application/json")
2024-01-26 11:26:22 +01:00
var http := HTTPRequest.new()
2024-01-26 11:26:22 +01:00
var resp = HTTPResponse.new(http, responses)
add_child(http)
http.request(url, headers, method, body)
# Generic error signal handling to ensure the signal is emitted.
resp.response_received.connect(func(data):
# If there's no error return.
if !data.has("error") or data.error.is_empty():
return
twitch_error.emit(data.error, data.message)
)
return resp
2024-01-26 11:26:22 +01:00
## Wrapper function around [method twitch_request] to handle doing a Twitch "Get Users" request.
func request_user_info(users : Array[String] = []):
2024-01-26 11:26:22 +01:00
var req_url := "https://api.twitch.tv/helix/users"
2024-01-26 11:26:22 +01:00
if users.is_empty():
2024-01-26 11:26:22 +01:00
return twitch_request(req_url)
2024-01-26 11:26:22 +01:00
var user_list : String = "?login=" + users[0]
for all in users:
2024-01-26 11:26:22 +01:00
user_list += "&login=" + all
2024-01-26 11:26:22 +01:00
return twitch_request(req_url + user_list)
2024-01-26 11:26:22 +01:00
## Wrapper function around [method twitch_request] to grab information about a set of channels based
## off an Array of Channel IDs Ex. stream title, current category, current tags, etc.
func request_channel_info(channels : Array[String] = []):
2024-01-26 11:26:22 +01:00
var req_url := "https://api.twitch.tv/helix/channels?broadcaster_id="
2024-01-26 11:26:22 +01:00
if channels.is_empty():
2024-01-26 11:26:22 +01:00
if user_info.is_empty():
2024-01-26 11:26:22 +01:00
return
2024-01-26 11:26:22 +01:00
channels.append(user_info.id)
2024-01-26 11:26:22 +01:00
var id_string := channels[0]
for all in channels:
2024-01-26 11:26:22 +01:00
id_string += "&broadcaster_id=" + all
2024-01-26 11:26:22 +01:00
return twitch_request(req_url + id_string)
2024-01-26 11:26:22 +01:00
## Utility function for creating a "state" used for different requests for some extra security as a
## semi password.
func make_state(len : int = 16) -> String:
2024-01-26 11:26:22 +01:00
var crypto = Crypto.new()
var state = crypto.generate_random_bytes(len).hex_encode()
2024-01-26 11:26:22 +01:00
return state
2024-01-26 11:26:22 +01:00
func check_auth_peer(peer : StreamPeerTCP):
2024-01-26 11:26:22 +01:00
var info = peer.get_utf8_string(peer.get_available_bytes())
printraw(info)
2024-01-26 11:26:22 +01:00
var root := redirect_uri
if !redirect_port.is_empty():
root += ":" + redirect_port
root += "/"
2024-01-26 11:26:22 +01:00
var script = "<script>fetch('" + root + "' + window.location.hash.substr(1))</script>"
2024-01-26 11:26:22 +01:00
peer.put_data(str("HTTP/1.1 200\n\n" + script).to_utf8_buffer())
2024-01-26 11:26:22 +01:00
var resp_state = info.split("&state=")
2024-01-26 11:26:22 +01:00
# Ensures that the received state is correct.
if !resp_state.size() > 1 or resp_state[0] == state:
2024-01-26 11:26:22 +01:00
return
2024-01-26 11:26:22 +01:00
var token = info.split("access_token=")[1].split("&scope=")[0].strip_edges()
printraw("Token: ", token, "\n")
self.token = token
token_received.emit(token)
2024-01-26 11:26:22 +01:00
func check_chat_socket(dict, rich = false):
2024-01-26 11:26:22 +01:00
if rich:
2024-01-26 11:26:22 +01:00
chat_received_rich.emit(dict)
return
2024-01-26 11:26:22 +01:00
chat_received.emit(dict)
2024-01-26 11:26:22 +01:00
## "Promise" Class used for relaying the info received from the supplied [HTTPRequest] node.
class HTTPResponse:
2024-01-26 11:26:22 +01:00
signal response_received
2024-01-26 11:26:22 +01:00
## The infuriatingly needed data variable. Because you can't await AND get signal data.
2024-01-26 11:26:22 +01:00
var inf_data
func _init(http : HTTPRequest, storage : Array):
2024-01-26 11:26:22 +01:00
storage.append(self)
http.request_completed.connect(request_complete.bind(http, storage))
2024-01-26 11:26:22 +01:00
func request_complete(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray, http, storage):
2024-01-26 11:26:22 +01:00
var info : Dictionary
var str = body.get_string_from_utf8()
if !body.is_empty() and !JSON.parse_string(str) == null:
2024-01-26 11:26:22 +01:00
info = JSON.parse_string(body.get_string_from_utf8())
2024-01-26 11:26:22 +01:00
info["result"] = result
info["code"] = response_code
info["headers"] = headers
2024-01-26 11:26:22 +01:00
if info.has("error"):
emit_response(info, http, storage)
return
if info.data is Array and info.data.size() == 1:
info.data = info.data[0]
# Revisit later cause Twitch weirdness
#info["data"] = info.get("data", [[]])[0]
2024-01-26 11:26:22 +01:00
print("Response Received")
emit_response(info, http, storage)
func emit_response(info, http, storage):
inf_data = info
response_received.emit(info)
http.queue_free()
storage.erase(self)
2024-01-26 11:26:22 +01:00
## Data handler class for making it easier to send the data used for EventSub Subscriptions over
## [member eventsub_socket]
class EventSub_Subscription:
2024-01-26 11:26:22 +01:00
## Specifies the type of subscription this is representing (Ex. "Channel Update" is channel.update)
var subscription_type : String
## Used for setting the "version" the subscription that we're currently using.
var version : String
2024-01-26 11:26:22 +01:00
## Stores the "condition", AKA the condition for being able to subscribe to this Event.
## (Ex. The User ID of the channel you're listening to)
var condition : Dictionary
2024-01-26 11:26:22 +01:00
## Holds the "session" ID for this EventSub Subscription. Which is held in
## [member eventsub_socket.session_id]
var session_id : String
2024-01-26 11:26:22 +01:00
## Stores the subscription ID returned from the EventSub Subscription API Request
var sub_id
var method = "websocket"
2024-01-26 11:26:22 +01:00
func _init(sub_type : String, cond : Dictionary, sess_id : String = "", vers : String = "1"):
2024-01-26 11:26:22 +01:00
subscription_type = sub_type
version = vers
2024-01-26 11:26:22 +01:00
condition = cond
session_id = sess_id
2024-01-26 11:26:22 +01:00
## Returns back the information pertaining to the Subscription in the format needed for twitch.
func return_request_dictionary():
2024-01-26 11:26:22 +01:00
var dict = {"type" : subscription_type, "version" : version, "condition" : condition, "transport" : {"method" : method, "session_id" : session_id}}
2024-01-26 11:26:22 +01:00
return dict
2024-01-26 11:26:22 +01:00