commit ef9418f8fe9abf3e167f4782ef55b0e7f1242458 Author: MennoMax Date: Fri Oct 25 21:09:38 2019 +0200 initial commit diff --git a/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.md5 b/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.md5 new file mode 100644 index 0000000..f75f542 --- /dev/null +++ b/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.md5 @@ -0,0 +1,3 @@ +source_md5="8dd9ff1eebf38898a54579d8c01b0a88" +dest_md5="da70afec3c66d4e872db67f808e12edb" + diff --git a/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex b/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex new file mode 100644 index 0000000..46e6d2a Binary files /dev/null and b/.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex differ diff --git a/.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.md5 b/.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.md5 new file mode 100644 index 0000000..221c624 --- /dev/null +++ b/.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.md5 @@ -0,0 +1,3 @@ +source_md5="a968f7207fa0b577199e591bee3d650b" +dest_md5="d352d63ef80c05346da08dcf324711fd" + diff --git a/.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.stex b/.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.stex new file mode 100644 index 0000000..a5bf3f8 Binary files /dev/null and b/.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.stex differ diff --git a/Button.gd b/Button.gd new file mode 100644 index 0000000..b2aba84 --- /dev/null +++ b/Button.gd @@ -0,0 +1,5 @@ +extends Button + +func _pressed(): + $"../Gift".chat($"../LineEdit".text) + $"../LineEdit".text = "" \ No newline at end of file diff --git a/Gift.gd b/Gift.gd new file mode 100644 index 0000000..c8271d2 --- /dev/null +++ b/Gift.gd @@ -0,0 +1,64 @@ +extends Gift + +func _ready() -> void: + connect("cmd_no_permission", self, "no_permission") + connect_to_twitch() + yield(self, "twitch_connected") + authenticate_oauth(, ) + join_channel() + + # Adds a command with a specified permission flag. Defaults to PermissionFlag.EVERYONE + # All implementation must take at least one arg for the command data. + # This command can only be executed by VIPS/MODS/SUBS/STREAMER + add_command("test", self, "command_test", PermissionFlag.NON_REGULAR) + # This command can be executed by everyone + add_command("helloworld", self, "hello_world", PermissionFlag.EVERYONE) + # This command can only be executed by the streamer + add_command("streamer_only", self, "streamer_only", PermissionFlag.STREAMER) + # Command that requires exactly 1 arg. + add_command("greet", self, "greet", PermissionFlag.EVERYONE, 1, 1) + # Command that prints every arg seperated by a comma (infinite args allowed), at least 2 required + add_command("list", self, "list", PermissionFlag.EVERYONE, -1, 2) + + # Adds a command alias + add_alias("test","test1") + add_alias("test","test2") + add_alias("test","test3") + + # Remove a single command + remove_command("test2") + # Now only knows commands "test", "test1" and "test3" + remove_command("test") + # Now only knows commands "test1" and "test3" + + # Remove all commands that call the same function as the specified command + purge_command("test1") + # Now no "test" command is known + + # Send a chat message to the only connected channel ("mennomax") + # Fails, if connected to more than one channel. + chat("TEST") + # Send a chat message to channel "mennomax" + chat("TEST", "mennomax") + # Send a whisper to user "mennomax" + whisper("TEST", "mennomax") + +# The cmd_data array contains [, , +func command_test(cmd_data): + print("A") + +# The cmd_data array contains [, , +func hello_world(cmd_data): + chat("HELLO WORLD!") + +func streamer_only(cmd_data): + chat("Streamer command executed") + +func no_permission(cmd_data): + chat("NO PERMISSION!") + +func greet(cmd_data, arg_ary): + chat("Greetings, " + arg_ary[0]) + +func list(cmd_data, arg_ary): + chat(arg_ary.join(", ")) \ No newline at end of file diff --git a/Node.tscn b/Node.tscn new file mode 100644 index 0000000..7de9d04 --- /dev/null +++ b/Node.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://Gift.gd" type="Script" id=1] +[ext_resource path="res://addons/gift/icon.png" type="Texture" id=2] +[ext_resource path="res://Button.gd" type="Script" id=3] + +[node name="Node" type="Control"] + +[node name="Gift" type="Node" parent="."] +script = ExtResource( 1 ) +__meta__ = { +"_editor_icon": ExtResource( 2 ) +} + +[node name="LineEdit" type="LineEdit" parent="."] +margin_left = 403.0 +margin_top = 278.0 +margin_right = 531.0 +margin_bottom = 302.0 + +[node name="Button" type="Button" parent="."] +margin_left = 539.0 +margin_top = 278.0 +margin_right = 586.0 +margin_bottom = 302.0 +text = "Send" +script = ExtResource( 3 ) diff --git a/addons/gift/gift.gd b/addons/gift/gift.gd new file mode 100644 index 0000000..9296ecc --- /dev/null +++ b/addons/gift/gift.gd @@ -0,0 +1,8 @@ +tool +extends EditorPlugin + +func _enter_tree() -> void: + add_custom_type("Gift", "Node", preload("gift_node.gd"), preload("icon.png")) + +func _exit_tree() -> void: + remove_custom_type("Gift") diff --git a/addons/gift/gift_node.gd b/addons/gift/gift_node.gd new file mode 100644 index 0000000..04124c5 --- /dev/null +++ b/addons/gift/gift_node.gd @@ -0,0 +1,254 @@ +extends Node +class_name Gift + +# The underlying websocket sucessfully connected to twitch. +signal twitch_connected +# The connection has been closed. +signal twitch_disconnected +# The connection to twitch failed. +signal twitch_unavailable +# The client tried to login. Returns true if successful, else false. +signal login_attempt(success) +# User sent a message in chat. +signal chat_message(sender_data, message, channel) +# User sent a whisper message. +signal whisper_message(sender_data, message, channel) +# Unhandled data passed through +signal unhandled_message(message, tags) +# A command has been called with invalid arg count +signal cmd_invalid_argcount(cmd_name, sender_data, cmd_data, arg_ary) +# A command has been called with insufficient permissions +signal cmd_no_permission(cmd_name, sender_data, cmd_data, arg_ary) +# Twitch's ping is about to be answered with a pong. +signal pong + +# Messages starting with one of these symbols are handled. '/' will be ignored, reserved by Twitch. +export(Array, String) var command_prefixes = ["!"] + +# Mapping of channels to their channel info, like currently connected users. +var channels : Dictionary = {} +var commands : Dictionary = {} +var websocket : WebSocketClient = WebSocketClient.new() +var user_regex = RegEx.new() + +# Required permission to execute the command +enum PermissionFlag { + EVERYONE = 0, + VIP = 1, + SUB = 2, + MOD = 4, + STREAMER = 8, + # Mods and the streamer + MOD_STREAMER = 12, + # Everyone but regular viewers + NON_REGULAR = 15 +} + +# Where the command should be accepted +enum WhereFlag { + CHAT = 1, + WHISPER = 2 +} + +func _init(): + websocket.verify_ssl = true + user_regex.compile("(?<=!)[\\w]*(?=Q)") + +func _ready() -> void: + assert(websocket.connect("data_received", self, "data_received") == OK) + assert(websocket.connect("connection_established", self, "connection_established") == OK) + assert(websocket.connect("connection_closed", self, "connection_closed") == OK) + assert(websocket.connect("server_close_request", self, "sever_close_request") == OK) + assert(websocket.connect("connection_error", self, "connection_error") == OK) + +func connect_to_twitch() -> void: + if(websocket.connect_to_url("wss://irc-ws.chat.twitch.tv:443") != OK): + print_debug("Could not connect to Twitch.") + emit_signal("twitch_unavailable") + +func _process(delta : float) -> void: + if(websocket.get_connection_status() != NetworkedMultiplayerPeer.CONNECTION_DISCONNECTED): + websocket.poll() + +# Login using a oauth token. +# You will have to either get a oauth token yourself or use +# https://twitchapps.com/tokengen/ +# to generate a token with custom scopes. +func authenticate_oauth(nick : String, token : String) -> void: + websocket.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT) + send("PASS " + ("" if token.begins_with("oauth:") else "oauth:") + token, true) + send("NICK " + nick.to_lower()) + request_caps() + +func request_caps(caps : Array = [":twitch.tv/commands", ":twitch.tv/tags", ":twitch.tv/membership"]) -> void: + for cap in caps: + send("CAP REQ " + cap) + +# Sends a String to Twitch. +func send(text : String, token : bool = false) -> void: + assert(websocket.get_peer(1).put_packet(text.to_utf8()) == OK) + if(OS.is_debug_build()): + if(!token): + print("< " + text.strip_edges(false)) + else: + print("< PASS oauth:******************************") + +# Sends a chat message to a channel. Defaults to the only connected channel. +func chat(message : String, channel : String = ""): + var keys : Array = channels.keys() + if(channel != ""): + send("PRIVMSG " + ("" if channel.begins_with("#") else "#") + channel + " :" + message + "\r\n") + elif(keys.size() == 1): + send("PRIVMSG #" + channels.keys()[0] + " :" + message + "\r\n") + else: + print_debug("No channel specified.") + +func whisper(message : String, target : String): + chat("/w " + target + " " + message) + +func data_received() -> void: + var messages : PoolStringArray = websocket.get_peer(1).get_packet().get_string_from_utf8().strip_edges(false).split("\r\n") + var tags = {} + for message in messages: + if(message.begins_with("@")): + var msg : PoolStringArray = message.split(" ", false, 1) + message = msg[1] + for tag in msg[0].split(";"): + var pair = tag.split("=") + tags[pair[0]] = Array(pair[1].split(",")) + if(OS.is_debug_build()): + print("> " + message) + handle_message(message, tags) + +# Registers a command on an object with a func to call, similar to connect(signal, instance, func). +func add_command(cmd_name : String, instance : Object, instance_func : String, permission_level : int = PermissionFlag.EVERYONE, max_args : int = 0, min_args : int = 0, where : int = WhereFlag.CHAT) -> void: + var func_ref = FuncRef.new() + func_ref.set_instance(instance) + func_ref.set_function(instance_func) + commands[cmd_name] = [func_ref, permission_level, max_args, min_args, where] + +# Removes a single command or alias. +func remove_command(cmd_name : String) -> void: + commands.erase(cmd_name) + +# Removes a command and all associated aliases. +func purge_command(cmd_name : String) -> void: + var to_remove = commands.get(cmd_name) + if(to_remove): + var remove_queue = [] + for command in commands.keys(): + if(commands[command][0] == to_remove[0]): + remove_queue.append(command) + for queued in remove_queue: + commands.erase(queued) + +func add_alias(cmd_name : String, alias : String) -> void: + if(commands.has(cmd_name)): + commands[alias] = commands.get(cmd_name) + +func add_aliases(cmd_name : String, aliases : Array) -> void: + for alias in aliases: + add_alias(cmd_name, alias) + +func handle_message(message : String, tags : Dictionary) -> void: + if(message == ":tmi.twitch.tv NOTICE * :Login authentication failed"): + print_debug("Authentication failed.") + emit_signal("login_attempt", false) + return + if(message == "PING :tmi.twitch.tv"): + send("PONG :tmi.twitch.tv") + emit_signal("pong") + return + var msg : PoolStringArray = message.split(" ", true, 4) + match msg[1]: + "001": + print_debug("Authentication successful.") + emit_signal("login_attempt", true) + "PRIVMSG": + var sender_data : Array = [user_regex.search(msg[0]), msg[2], tags] + handle_command(sender_data, msg) + emit_signal("chat_message", sender_data, msg[3].right(1)) + "WHISPER": + var sender_data : Array = [user_regex.search(msg[0]), msg[2], tags] + handle_command(sender_data, msg, true) + emit_signal("whisper_message", sender_data, msg[3].right(1)) + _: + emit_signal("unhandled_message", message, tags) + +func handle_command(sender_data : Array, msg : PoolStringArray, whisper : bool = false) -> void: + if(command_prefixes.has(msg[3].substr(1, 1))): + var command : String = msg[3].right(2) + var cmd_data = commands.get(command) + if(cmd_data): + var args = "" if msg.size() < 5 else msg[4] + var arg_ary : PoolStringArray = PoolStringArray() if args == "" else args.split(" ") + if(arg_ary.size() > cmd_data[2] && cmd_data[2] != -1): + emit_signal("cmd_invalid_argcount", command, sender_data, cmd_data, arg_ary) + print_debug("Invalid argcount!") + return + if(arg_ary.size() < cmd_data[3]): + emit_signal("cmd_invalid_argcount", command, sender_data, cmd_data, arg_ary) + print_debug("Invalid argcount!") + return + if(cmd_data[1] != 0): + var user_perm_flags = get_perm_flag_from_tags(sender_data[2]) + print(user_perm_flags & cmd_data[1]) + print(cmd_data[1]) + if(user_perm_flags & cmd_data[1] != cmd_data[1]): + emit_signal("cmd_no_permission", command, sender_data, cmd_data, arg_ary) + print_debug("No Permission for command!") + return + if(arg_ary.size() == 0): + cmd_data[0].call_func([sender_data, command, whisper]) + else: + cmd_data[0].call_func([sender_data, command, whisper], arg_ary) + +func get_perm_flag_from_tags(tags : Dictionary) -> int: + print(tags) + var flag = 0 + var entry = tags.get("badges") + if(entry): + if(entry.has("vip/1")): + flag += PermissionFlag.VIP + if(entry.has("broadcaster/1")): + flag += PermissionFlag.STREAMER + entry = tags.get("mod") + if(entry): + if(entry[0] == "1"): + flag += PermissionFlag.MOD + entry = tags.get("subscriber") + if(entry): + if(entry[0] == "1"): + flag += PermissionFlag.SUB + return flag + +func join_channel(channel : String) -> void: + var lower_channel : String = channel.to_lower() + send("JOIN #" + lower_channel) + channels[lower_channel] = {} + +func leave_channel(channel : String) -> void: + var lower_channel : String = channel.to_lower() + send("PART #" + lower_channel) + channels.erase(lower_channel) + +func connection_established(protocol : String) -> void: + print_debug("Connected to Twitch.") + emit_signal("twitch_connected") + +func connection_closed(was_clean_close : bool) -> void: + print_debug("Disconnected from Twitch.") + emit_signal("twitch_disconnected") + +func connection_error() -> void: + print_debug("Twitch is unavailable.") + emit_signal("twitch_unavailable") + +func server_close_request(code : int, reason : String) -> void: + pass + +func _enter_tree() -> void: + pass + +func _exit_tree() -> void: + pass \ No newline at end of file diff --git a/addons/gift/icon.png b/addons/gift/icon.png new file mode 100644 index 0000000..b72c82d Binary files /dev/null and b/addons/gift/icon.png differ diff --git a/addons/gift/icon.png.import b/addons/gift/icon.png.import new file mode 100644 index 0000000..ecd1a3a --- /dev/null +++ b/addons/gift/icon.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/gift/icon.png" +dest_files=[ "res://.import/icon.png-b5cf707f4ba91fefa5df60a746e02900.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/addons/gift/plugin.cfg b/addons/gift/plugin.cfg new file mode 100644 index 0000000..e12e110 --- /dev/null +++ b/addons/gift/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="Godot IRC For Twitch" +description="Godot websocket implementation for Twitch IRC." +author="MennoMax" +version="0.0.1" +script="gift.gd" \ No newline at end of file diff --git a/default_env.tres b/default_env.tres new file mode 100644 index 0000000..20207a4 --- /dev/null +++ b/default_env.tres @@ -0,0 +1,7 @@ +[gd_resource type="Environment" load_steps=2 format=2] + +[sub_resource type="ProceduralSky" id=1] + +[resource] +background_mode = 2 +background_sky = SubResource( 1 ) diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..2b65815 Binary files /dev/null and b/icon.png differ diff --git a/icon.png.import b/icon.png.import new file mode 100644 index 0000000..96cbf46 --- /dev/null +++ b/icon.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.png" +dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..b4c657a --- /dev/null +++ b/project.godot @@ -0,0 +1,41 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +_global_script_classes=[ { +"base": "Node", +"class": "Gift", +"language": "GDScript", +"path": "res://addons/gift/gift_node.gd" +} ] +_global_script_class_icons={ +"Gift": "" +} + +[application] + +config/name="GIFT" +run/main_scene="res://Node.tscn" +config/icon="res://icon.png" + +[debug] + +gdscript/warnings/unused_argument=false +gdscript/warnings/return_value_discarded=false + +[editor_plugins] + +enabled=PoolStringArray( "gift" ) + +[rendering] + +quality/driver/driver_name="GLES2" +vram_compression/import_etc=true +vram_compression/import_etc2=false +environment/default_environment="res://default_env.tres"