Skip to content

Commit

Permalink
feat: Add ability to open and close queue
Browse files Browse the repository at this point in the history
  • Loading branch information
opti21 committed Apr 27, 2022
1 parent 72f42dc commit a32b51e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
}
Expand Down
34 changes: 34 additions & 0 deletions src/redis/handlers/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Queue {
is_updating?: boolean;
being_updated_by?: string;
now_playing?: string;
is_open: boolean;
}

class Queue extends Entity {}
Expand All @@ -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",
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -160,6 +192,8 @@ export {
Queue,
getQueue,
createQueue,
openQueue,
closeQueue,
lockQueue,
unLockQueue,
addToQueue,
Expand Down

0 comments on commit a32b51e

Please sign in to comment.