2024-02-09 12:43:17 +01:00
# (c) 2023-present Eroax
# (c) 2023-present Yagich
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
extends DeckNode
2024-02-26 12:36:19 +01:00
enum inputs {
event_name ,
sub_data ,
add
}
2024-02-09 12:43:17 +01:00
var subscription_data : Twitch_Connection . EventSub_Subscription
func _init ( ) :
name = " Twitch Add EventSub Subscription "
node_type = name . to_snake_case ( )
2024-02-11 20:08:11 +01:00
description = " Subscribes to a Twitch EventSub Event with the given dictionary ' condition ' for the data needed. "
2024-02-09 12:43:17 +01:00
props_to_serialize = [ ]
#Event Name
2024-02-26 12:36:19 +01:00
add_input_port (
DeckType . Types . STRING ,
" Event Name " , " field "
2024-03-06 08:50:23 +01:00
)
2024-02-09 12:43:17 +01:00
#Subscription Data
2024-02-26 12:36:19 +01:00
add_input_port (
DeckType . Types . DICTIONARY ,
" Subscription Data "
2024-03-06 08:50:23 +01:00
)
2024-02-09 12:43:17 +01:00
#Trigger
2024-02-26 12:36:19 +01:00
add_input_port (
DeckType . Types . ANY ,
" Add Subscription " , " button " ,
Port . UsageType . TRIGGER
2024-03-06 08:50:23 +01:00
) . button_pressed . connect ( _receive . bind ( inputs . add , null ) )
2024-02-09 12:43:17 +01:00
2024-02-26 12:36:19 +01:00
func _receive ( to_input_port , data : Variant ) - > void :
2024-02-09 12:43:17 +01:00
2024-02-26 12:36:19 +01:00
var event
var sub_data
2024-02-09 12:43:17 +01:00
2024-02-26 12:36:19 +01:00
match to_input_port :
inputs . event_name :
event = str ( data )
sub_data = await resolve_input_port_value_async ( 1 )
inputs . sub_data :
if data == null or not data is Dictionary :
DeckHolder . logger . log_node ( " %s : Port %d : Incorrect Subscription Data Connected, please supply a Dictionary with condition and if needed, version. Last supplied Data was: " + str ( data ) , Logger . LogType . ERROR )
return
sub_data = data
event = await resolve_input_port_value_async ( 0 )
inputs . add :
event = await resolve_input_port_value_async ( 0 )
sub_data = await resolve_input_port_value_async ( 1 )
2024-02-09 12:43:17 +01:00
# Creates an instance of Twitch_Connection.EventSub_Subscription to store the data with all the given inputs.
2024-02-26 12:36:19 +01:00
subscription_data = Twitch_Connection . EventSub_Subscription . new ( event , sub_data )
2024-02-09 12:43:17 +01:00
# Checks if the data has a version field, if so sets it on the EventSub_Subscription
2024-02-26 12:36:19 +01:00
if sub_data . has ( " version " ) :
2024-02-09 12:43:17 +01:00
2024-02-26 12:36:19 +01:00
subscription_data . version = sub_data . version
2024-02-09 12:43:17 +01:00
# Calls the connection to add the Subscription
var req = await Connections . twitch . add_eventsub_subscription ( subscription_data )
req . response_received . connect ( eventsub_subscription_response )
## Handles checking the [Twitch_Connection.HTTPResponse] returned by [method Twitch_Connection.add_eventsub_subscription] to ensure that it succeeded.
func eventsub_subscription_response ( data ) :
match data . code :
202 :
var succ_string = name + " : EventSub Subscription Added for " + subscription_data . subscription_type + " successfully "
DeckHolder . logger . log_node ( succ_string , Logger . LogType . INFO )
Connections . twitch . eventsub_socket . notif_received . connect ( Connections . _twitch_eventsub_event_received )
_ :
2024-02-21 07:34:17 +01:00
@ warning_ignore ( " shadowed_global_identifier " )
2024-02-09 12:43:17 +01:00
var error_string = name + " : Error " + data . code + " Received from Twitch when Subscribing to " + subscription_data . sub_type + " with " + str ( subscription_data . return_request_dictionary )
DeckHolder . logger . log_node ( error_string , Logger . LogType . ERROR )