From 47adaa9a252f82688eabbf6f18d9ddd786e1cb70 Mon Sep 17 00:00:00 2001 From: Joe Rayhawk Date: Mon, 21 Nov 2022 22:57:17 -0800 Subject: crystal: crystal-obs-websocket API updates --- crystal/eventsub.cr | 65 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 20 deletions(-) (limited to 'crystal/eventsub.cr') diff --git a/crystal/eventsub.cr b/crystal/eventsub.cr index 6014ebf..b472b4b 100644 --- a/crystal/eventsub.cr +++ b/crystal/eventsub.cr @@ -15,17 +15,28 @@ settings = Hash(String, String).new end end -# enable explosions? -explosions = Hash( String, String ).new -if File.exists?( settings["configdir"] + "/explosionlist.txt" ) - File.each_line( settings["configdir"] + "/explosionlist.txt" ) do |line| - explosions[ line.downcase ] = line - end +settings["configdir"] = "/home/jrayhawk/.config/bungmobott/" + +if ENV["LOCALAPPDATA"]? + settings["configdir"] = Path.windows( ENV["LOCALAPPDATA"] + "\\bungmobott\\" ).to_s + settings["statedir"] = Path.windows( ENV["LOCALAPPDATA"] + "\\bungmobott\\state\\" ).to_s end -obs = OBSWebSocket::Server.new( "ws://127.0.0.1:4455/" ) +ENV["XDG_CONFIG_HOME"]? && ( settings["configdir"] = ENV["XDG_CONFIG_HOME"] + "/bungmobott/" ) -def obstemporarymediacreate( obs : OBSWebSocket::Server, sname : String, iname, path : String ) +Dir.mkdir_p( settings["configdir"] ) + +# enable effects? +effects = Hash( String, String ).new +if File.exists?( settings["configdir"] + "/effects.txt" ) + File.each_line( settings["configdir"] + "/effects.txt" ) do |line| + effects[ line.downcase ] = line + end +end +explosioncount = 0 +youdied = false + +def obstemporarymediacreate( obs : OBS::WebSocket, sname : String, iname, path : String ) iname = "media-temporary-explosion-#{iname}" isettings = Hash( String, String | Bool | Int64 | Float64 ){ "advanced" => true, @@ -39,6 +50,15 @@ def obstemporarymediacreate( obs : OBSWebSocket::Server, sname : String, iname, response = obs.scenes[sname].createinput( iname, "ffmpeg_source", isettings ) end +def obsrandommediaenable( obs : OBS::WebSocket, siname : String ) + if ( Random.rand(3) < 2 ) + obs.scenes.current.metascene[siname][0].enable! + else + randsiname = obs.scenes.current.metascene.keys.select( /^#{siname}/ ).sample( 1 )[0] + obs.scenes.current.metascene[randsiname][0].enable! + end +end + def ircmsg( channel : String, msg : String) if msg !~ /(\r|\n)/ sock = Socket.unix @@ -75,7 +95,6 @@ File.open("/tmp/eventsub.#{Process.pid}.txt", "w", 0o644 ) do |file| digest = "sha256=" + OpenSSL::HMAC.hexdigest( :sha256, settings["eventsub_secret"], ENV["HTTP_TWITCH_EVENTSUB_MESSAGE_ID"] + ENV["HTTP_TWITCH_EVENTSUB_MESSAGE_TIMESTAMP"] + input ) if json["challenge"]? && ( ENV["HTTP_TWITCH_EVENTSUB_MESSAGE_SIGNATURE"] == digest ) - STDOUT.puts( json["challenge"] ) file.puts( json["challenge"] ) elsif json["event"]? @@ -91,6 +110,7 @@ File.open("/tmp/eventsub.#{Process.pid}.txt", "w", 0o644 ) do |file| ircmsg( settings["channel"], "#{json["event"]["user_login"]} is now following!" ) when "channel.ban" ircmsg( settings["channel"], "#{json["event"]["user_login"]} is now banned!" ) + youdied = true when "channel.unban" ircmsg( settings["channel"], "#{json["event"]["user_login"]} is now unbanned!" ) when "channel.raid" @@ -101,13 +121,8 @@ File.open("/tmp/eventsub.#{Process.pid}.txt", "w", 0o644 ) do |file| sub_broadcaster = ( json["event"]["broadcaster_user_login"]?.as_s? || "unknown" ) sub_plan = ( json["event"]["tier"]?.as_s? || "unknown" ) #sub_months = ( json["event"]["cumulative_months"]?.as_i? || 0 ) - unless json["event"]["is_gift"].as_bool - effectsmsg( "overlay gltext 10 " + sub_recver + " subscribed!" ) - spawn do - explosions.values.sample( 5 ).each do | explosion | - obstemporarymediacreate( obs, "meta-foreground", explosion, "C:/cygwin64/home/user/effects/explosions/#{explosion}.webm" ) - end - end + unless json["event"]["is_gift"].as_bool # If gift, let count get size-limited in channel.subscription.gift below instead + explosioncount += 5 end ircmsg( settings["channel"], sub_recver.to_s + " has subscribed at sub level #" + sub_plan.to_s + " bungmoBlobDance bungmoBlobDance bungmoBlobDance bungmoBlobDance" ) when "channel.subscription.gift" @@ -121,11 +136,9 @@ File.open("/tmp/eventsub.#{Process.pid}.txt", "w", 0o644 ) do |file| spawn do count = sub_total * 5 ( count > 50 ) && ( count = 50 ) - explosions.values.sample( count ).each do | explosion | - obstemporarymediacreate( obs, "meta-foreground", explosion, "C:/cygwin64/home/user/effects/explosions/#{explosion}.webm" ) - end + explosioncount += count end - when "channel.subscription.message" + when "channel.subscription.message" sub_sender = ( json["event"]["user_login"]?.as_s? || "anonymous" ) sub_plan = ( json["event"]["tier"]?.as_s? || "unknown" ) sub_months = ( json["event"]["cumulative_months"]?.as_i? || 0 ) @@ -150,6 +163,18 @@ File.open("/tmp/eventsub.#{Process.pid}.txt", "w", 0o644 ) do |file| end +if youdied + obs = OBS::WebSocket.new( "ws://127.0.0.1:4455/", retry: false ) + obsrandommediaenable( obs, "media-youdied" ) +end + +if ( explosioncount > 0 ) + obs = OBS::WebSocket.new( "ws://127.0.0.1:4455/", retry: false ) + effects.values.select(/^explosions/).sample( explosioncount ).each do | effect | + obstemporarymediacreate( obs, "meta-foreground", effect.gsub(/[\/ ]/,"_"), "C:/cygwin64/home/user/effects/#{effect}") + end +end +exit #{"subscription" => # {"id" => "3c1c3a9c-748e-4374-8e61-2c0210755af3", -- cgit v1.2.3