From a32b51e2822dc71955449cf0912819424ed7ec4f Mon Sep 17 00:00:00 2001 From: opti21 <40129778+opti21@users.noreply.github.com> Date: Wed, 27 Apr 2022 14:15:02 -0500 Subject: [PATCH] feat: Add ability to open and close queue --- src/index.ts | 56 ++++++++++++++++++++++++++++++++++++- src/redis/handlers/Queue.ts | 34 ++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 8ae4b3d..cc3eba2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,12 @@ import axios from "axios"; import urlParser from "js-video-url-parser/lib/base"; import "js-video-url-parser/lib/provider/youtube"; import { YTApiResponse } from "./types"; -import { addToQueue, getQueue, removeFromOrder } from "./redis/handlers/Queue"; +import { + addToQueue, + getQueue, + openQueue, + removeFromOrder, +} from "./redis/handlers/Queue"; import { parseYTDuration } from "./utils"; import express from "express"; import Pusher from "pusher"; @@ -84,7 +89,39 @@ twitch.on("message", async (channel, tags, message, self) => { const args = message.slice(1).split(" "); const command = args.shift()?.toLowerCase(); + if ( + command === "open" && + (tags.mod || + tags.username === "opti_21" || + // brodacaster + channel.replace("#", "") == tags.username) + ) { + await openQueue().catch((err) => { + console.error(err); + twitch.say(channel, "Error opening queue"); + }); + twitch.say(channel, `@${tags.username} Queue is now open`); + } + + if ( + command === "close" && + (tags.mod || + tags.username === "opti_21" || + // brodacaster + channel.replace("#", "") == tags.username) + ) { + await openQueue().catch((err) => { + console.error(err); + twitch.say(channel, "Error opening queue"); + }); + twitch.say(channel, `@${tags.username} Queue is now closed`); + } + if (command === "sr") { + const queue = await getQueue(); + if (!queue.is_open) { + twitch.say(channel, `@${tags.username} Queue is closed`); + } // Check if valid youtube link then parse const parsed = urlParser.parse(args[0]); @@ -214,6 +251,11 @@ twitch.on("message", async (channel, tags, message, self) => { } if (command === "replace") { + const queue = await getQueue(); + if (!queue.is_open) { + twitch.say(channel, `@${tags.username} Queue is closed`); + } + const parsed = urlParser.parse(args[0]); if (!parsed) { @@ -323,6 +365,10 @@ twitch.on("message", async (channel, tags, message, self) => { } if (command === "wrongsong" || command === "remove") { + const queue = await getQueue(); + if (!queue.is_open) { + twitch.say(channel, `@${tags.username} Queue is closed`); + } const userHasRequest = await prisma.request.findFirst({ where: { requested_by_id: tags["user-id"], @@ -366,6 +412,10 @@ twitch.on("message", async (channel, tags, message, self) => { if (command === "song" || command === "cs" || command === "currentsong") { const queue = await getQueue(); + if (!queue.is_open) { + twitch.say(channel, `@${tags.username} Queue is closed`); + } + if (!queue) { if (growthbook.isOn("bot-talk")) { twitch.say(channel, "Error getting queue"); @@ -406,6 +456,10 @@ twitch.on("message", async (channel, tags, message, self) => { } if (command === "save") { + const queue = await getQueue(); + if (!queue.is_open) { + twitch.say(channel, `@${tags.username} Queue is closed`); + } if (growthbook.isOn("bot-talk")) { twitch.say(channel, "Coming Soon... PauseChamp"); } diff --git a/src/redis/handlers/Queue.ts b/src/redis/handlers/Queue.ts index 46cd981..be3552d 100644 --- a/src/redis/handlers/Queue.ts +++ b/src/redis/handlers/Queue.ts @@ -9,6 +9,7 @@ interface Queue { is_updating?: boolean; being_updated_by?: string; now_playing?: string; + is_open: boolean; } class Queue extends Entity {} @@ -20,6 +21,7 @@ const queueSchema = new Schema( is_updating: { type: "boolean" }, being_updated_by: { type: "string" }, now_playing: { type: "string" }, + is_open: { type: "boolean" }, }, { dataStructure: "JSON", @@ -56,6 +58,36 @@ async function createQueue(data: EntityCreationData) { return id; } +async function openQueue() { + await connect(); + + pusher.trigger(process.env.NEXT_PUBLIC_PUSHER_CHANNEL!, "open-queue", {}); + + const repository = client.fetchRepository(queueSchema); + + const queue = await repository.fetch(QUEUE_ID); + + queue.is_open = true; + + repository.save(queue); + + return queue; +} + +async function closeQueue() { + await connect(); + + pusher.trigger(process.env.NEXT_PUBLIC_PUSHER_CHANNEL!, "close-queue", {}); + + const repository = client.fetchRepository(queueSchema); + + const queue = await repository.fetch(QUEUE_ID); + + queue.is_open = false; + + repository.save(queue); +} + async function lockQueue() { await connect(); @@ -160,6 +192,8 @@ export { Queue, getQueue, createQueue, + openQueue, + closeQueue, lockQueue, unLockQueue, addToQueue,