diff --git a/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd b/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd new file mode 100644 index 0000000..04b1732 --- /dev/null +++ b/classes/deck/nodes/twitch/twitch_add_eventsub_subscription.gd @@ -0,0 +1,76 @@ +# (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 + + +var subscription_data : Twitch_Connection.EventSub_Subscription + +func _init(): + name = "Twitch Add EventSub Subscription" + node_type = name.to_snake_case() + description = "Adds a subscription to a specific Twitch EventSub Event with the given dictionary 'condition' for the data needed." + + props_to_serialize = [] + + #Event Name + add_input_port(DeckType.Types.STRING, "Event Name", "field") + #Subscription Data + add_input_port(DeckType.Types.DICTIONARY, "Subscription Data") + #Trigger + add_input_port(DeckType.Types.ANY, "Add Subscription", "button") + + + + +func _receive(to_input_port, data: Variant, extra_data: Array = []): + + if to_input_port != 2: + + return + + + var input_data = await resolve_input_port_value_async(1) + + + if input_data == null or !"condition" in input_data.keys(): + DeckHolder.logger.log_node(name + ": Incorrect Subscription Data Connected, please supply a Dictionary with condition and if needed, version. Last supplied Data was: " + str(input_data), Logger.LogType.ERROR) + return + + + var sub_type = await resolve_input_port_value_async(0) + +# Creates an instance of Twitch_Connection.EventSub_Subscription to store the data with all the given inputs. + subscription_data = Twitch_Connection.EventSub_Subscription.new(sub_type, input_data.condition) +# Checks if the data has a version field, if so sets it on the EventSub_Subscription + if input_data.has("version"): + + subscription_data.version = input_data.version + + +# 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) + + + _: + + 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) + + + +