From 161c8f4a4eaf6ac0486d1e4ff339a3a9710fddd0 Mon Sep 17 00:00:00 2001 From: Sarimoko Date: Sun, 13 Feb 2022 05:40:43 +0000 Subject: [PATCH] Add 'www/alert.js' --- www/alert.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 www/alert.js diff --git a/www/alert.js b/www/alert.js new file mode 100644 index 0000000..c4dc9dc --- /dev/null +++ b/www/alert.js @@ -0,0 +1,79 @@ +/* Config */ +const twitchTvHandle = "bdougieYO"; +const PAUSE_DURATION = 30 * 1000; // 30 seconds +const DISPLAY_DURATION = 10 * 1000; // 10 seconds + +/* DOM */ +const container = document.querySelector(".alerts"); +const img = new Image(); +const queue = new Queue(); + +/* Sound Effects */ +const pewAudio = new Audio("horn.wav"); +const magicChime = new Audio("Magic_Chime.mp3"); + +/* GIFs */ +const beyGif = "https://media.giphy.com/media/VxkNDa92gcsRq/giphy.gif"; +const welcomeGif = "https://media.giphy.com/media/l3V0doGbp2EDaLHJC/giphy.gif"; +const pizzaGif = "https://media.giphy.com/media/3o6nUXaNE4wdhq8Foc/giphy.gif"; + + +// Resolve promise after duration +const wait = async duration => { + return new Promise(resolve => setTimeout(resolve, duration)); +}; + +const pauseSpotify = () => { + fetch("https://serve.onegraph.com/graphql?app_id=cdf2ebe1-3ad3-408a-81c0-1ed675d76411", {body: '{"doc_id": "10fccd15-1a55-4a27-877a-a63106b4bd11"}', method: "POST"}) +} + +ComfyJS.Init(twitchTvHandle); +ComfyJS.onCommand = (user, command, message, flags, extra) => { + console.log(`!${command} was typed in chat`); + + if (command == "yo") { + new gifAlert(user, beyGif, pewAudio, command); + } + + if (command == "welcome") { + new gifAlert(message, welcomeGif, magicChime, command); + } + + if (flags.broadcaster && command == "pizza") { + new gifAlert(message, pizzaGif, magicChime, command); + } + + if (flags.broadcaster && command == "pause") { + // Clear GIF queue and pause for PAUSE_DURATION + queue.clear(); + queue.pause(PAUSE_DURATION); + } +}; + +ComfyJS.onChat = (user, message, flags, self, extra) => { + console.log(user + ":", message); +}; + +const generateTitle = { + yo: " is hype!", + welcome: " needs a welcome!", + pizza: " needed a pizza party!", +}; + +function gifAlert(user, gif, audio, type) { + queue.add(async () => { + audio.play(); + container.innerHTML = ` +

${user + generateTitle[type]}

+ + `; + container.style.opacity = 1; + + await wait(DISPLAY_DURATION); + + if (!queue.isLooping) { + container.style.opacity = 0; + } + + }); +} \ No newline at end of file