Skip to content

Commit

Permalink
Merge pull request #67 from opti21/add-pause-command
Browse files Browse the repository at this point in the history
feat: pause command
  • Loading branch information
opti21 authored Aug 27, 2024
2 parents c6a1037 + ed98096 commit 77e2415
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pepega-bot",
"version": "1.3.1",
"version": "1.4.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 26 additions & 8 deletions src/commands/songRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@ const handleSongRequest = async (
) => {
const queue = await getQueue();
if (!queue.is_open) {
twitch.say(channel, `@${tags.username} The queue is currently closed`);
twitch.say(
channel,
`@${tags.username} KEKWait The suggestion list is currently closed`
);
return;
}

if (!queue.is_paused) {
twitch.say(
channel,
`@${tags.username} KEKWait The suggestion list is currently paused, please wait for it to be resumed`
);
return;
}

// Check if valid youtube link then parse
const parsed = urlParser.parse(args[0]);

Expand Down Expand Up @@ -47,15 +59,15 @@ const handleSongRequest = async (
if (userAlreadyRequested) {
twitch.say(
channel,
`@${tags.username} looks like you already have a song in the queue, you can replace it by doing '!replace newurl', or once your request has been played or removed with !remove you can request another`
`@${tags.username} KEKWait looks like you already have a song in the queue, you can replace it by doing '!replace newurl', or once your request has been played or removed with !remove you can request another`
);
return;
}

if (videoAlreadyRequested) {
twitch.say(
channel,
`@${tags.username} this song has already been requested, please try another song`
`@${tags.username} this song has already been requested, please try another song peepoShy`
);
return;
}
Expand All @@ -82,11 +94,17 @@ const handleSongRequest = async (
const addedToQueue = await addToQueue(createdRequest?.id.toString());

if (!addedToQueue) {
twitch.say(channel, `Error adding to queue`);
twitch.say(
channel,
`Error adding to Suggestion List DinkDank @opti_21`
);
return;
}

twitch.say(channel, `@${tags.username} your request has been added`);
twitch.say(
channel,
`@${tags.username} your request has been added POGGIES`
);
}
return;
}
Expand All @@ -108,19 +126,19 @@ const handleSongRequest = async (
);

if (!createRequest) {
twitch.say(channel, `Error creating request`);
twitch.say(channel, `Error creating request DinkDank @opti_21`);
return;
}

const addedToQueue = await addToQueue(createdRequest?.id.toString());

if (!addedToQueue) {
twitch.say(channel, `Error adding to queue`);
twitch.say(channel, `Error adding to Suggestion List DinkDank @opti_21`);
}

twitch.say(
channel,
`@${tags.username} your song has been added to the suggestion list`
`@${tags.username} your song has been added to the suggestion list POGGIES`
);

return;
Expand Down
57 changes: 52 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import tmi from "tmi.js";
import "js-video-url-parser/lib/provider/youtube";
import { closeQueue, openQueue } from "./redis/handlers/Queue";
import {
closeQueue,
openQueue,
pauseQueue,
resumeQueue,
} from "./redis/handlers/Queue";
import express from "express";
import Pusher from "pusher";
import handleSongRequest from "./commands/songRequest";
Expand Down Expand Up @@ -61,14 +66,14 @@ twitch.on("message", async (channel, tags, message, self) => {
) {
await openQueue().catch((err) => {
console.error(err);
twitch.say(channel, "Error opening queue");
twitch.say(channel, "Error opening Sugestion List DinkDank @opti_21");
});
pusher.trigger(
process.env.NEXT_PUBLIC_PUSHER_CHANNEL!,
"update-queue",
{}
);
twitch.say(channel, `@${tags.username} Queue is now open`);
twitch.say(channel, `@${tags.username} Suggestion List is now open`);
return;
}

Expand All @@ -81,14 +86,56 @@ twitch.on("message", async (channel, tags, message, self) => {
) {
await closeQueue().catch((err) => {
console.error(err);
twitch.say(channel, "Error opening queue");
twitch.say(channel, "Error opening Suggestion list DinkDank @opti_21");
});
pusher.trigger(
process.env.NEXT_PUBLIC_PUSHER_CHANNEL!,
"update-queue",
{}
);
twitch.say(channel, `@${tags.username} Queue is now closed`);
twitch.say(channel, `@${tags.username} Suggestion list is now closed 🛑`);
return;
}
if (
command === "pause" &&
(tags.mod ||
tags.username === "opti_21" ||
// brodacaster
channel.replace("#", "") === tags.username)
) {
await pauseQueue().catch((err) => {
console.error(err);
twitch.say(channel, "Error pausing Suggestion List DinkDank @opti_21");
});
pusher.trigger(
process.env.NEXT_PUBLIC_PUSHER_CHANNEL!,
"update-queue",
{}
);
twitch.say(channel, `@${tags.username} Suggestion list is now paused ⏸️`);
return;
}

if (
command === "resume" &&
(tags.mod ||
tags.username === "opti_21" ||
// brodacaster
channel.replace("#", "") === tags.username)
) {
await resumeQueue().catch((err) => {
console.error(err);
twitch.say(channel, "Error resuming Suggestion List DinkDank @opti_21");
});
pusher.trigger(
process.env.NEXT_PUBLIC_PUSHER_CHANNEL!,
"update-queue",
{}
);
twitch.say(
channel,
`@${tags.username} Suggestion list has been resumed! Get your suggestions in! `
);
return;
}

Expand Down
36 changes: 36 additions & 0 deletions src/redis/handlers/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const QUEUE_ID = process.env.QUEUE_ID ? process.env.QUEUE_ID : "";
interface Queue {
order?: string[];
is_updating?: boolean;
is_paused?: boolean;
being_updated_by?: string;
now_playing?: string;
is_open: boolean;
Expand All @@ -20,6 +21,7 @@ const queueSchema = new Schema(
{
order: { type: "string[]" },
is_updating: { type: "boolean" },
is_paused: { type: "boolean" },
being_updated_by: { type: "string" },
now_playing: { type: "string" },
is_open: { type: "boolean" },
Expand Down Expand Up @@ -128,6 +130,38 @@ async function unLockQueue() {
return queue;
}

async function pauseQueue() {
await connect();

pusher.trigger(process.env.NEXT_PUBLIC_PUSHER_CHANNEL!, "pause-queue", {});

const repository = client.fetchRepository(queueSchema);

const queue = await repository.fetch(QUEUE_ID);

queue.is_paused = true;

repository.save(queue);

return queue;
}

async function resumeQueue() {
await connect();

pusher.trigger(process.env.NEXT_PUBLIC_PUSHER_CHANNEL!, "resume-queue", {});

const repository = client.fetchRepository(queueSchema);

const queue = await repository.fetch(QUEUE_ID);

queue.is_paused = false;

repository.save(queue);

return queue;
}

async function addToQueue(
requestID: string | undefined
): Promise<boolean | undefined> {
Expand Down Expand Up @@ -300,6 +334,8 @@ export {
closeQueue,
lockQueue,
unLockQueue,
pauseQueue,
resumeQueue,
addToQueue,
removeFromOrder,
updateOrder,
Expand Down

0 comments on commit 77e2415

Please sign in to comment.