Skip to content

Commit

Permalink
Fix queue permissions
Browse files Browse the repository at this point in the history
Prevent members outside music channel from modifying the queue
  • Loading branch information
eritislami committed Jun 12, 2020
1 parent 2dccd98 commit 9700cff
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 48 deletions.
14 changes: 9 additions & 5 deletions commands/loop.js
@@ -1,15 +1,19 @@
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
name: "loop",
aliases: ['l'],
description: "Toggle music loop",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id);
if (!serverQueue) return message.reply("There is nothing playing.").catch(console.error);
if (!canModifyQueue(message.member)) return;

const queue = message.client.queue.get(message.guild.id);
if (!queue) return message.reply("There is nothing playing.").catch(console.error);

// toggle from false to true and reverse
serverQueue.loop = !serverQueue.loop;
return serverQueue.textChannel
.send(`Loop is now ${serverQueue.loop ? "**on**" : "**off**"}`)
queue.loop = !queue.loop;
return queue.textChannel
.send(`Loop is now ${queue.loop ? "**on**" : "**off**"}`)
.catch(console.error);
}
};
16 changes: 9 additions & 7 deletions commands/pause.js
@@ -1,16 +1,18 @@
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
name: "pause",
description: "Pause the currently playing music",
execute(message) {
if (!message.member.voice.channel)
return message.reply("You need to join a voice channel first!").catch(console.error);
if (!canModifyQueue(message.member)) return

const serverQueue = message.client.queue.get(message.guild.id);
if (serverQueue && serverQueue.playing) {
serverQueue.playing = false;
serverQueue.connection.dispatcher.pause(true);
return serverQueue.textChannel.send(`${message.author} ⏸ paused the music.`).catch(console.error);
const queue = message.client.queue.get(message.guild.id);
if (queue && queue.playing) {
queue.playing = false;
queue.connection.dispatcher.pause(true);
return queue.textChannel.send(`${message.author} ⏸ paused the music.`).catch(console.error);
}

return message.reply("There is nothing playing.").catch(console.error);
}
};
14 changes: 10 additions & 4 deletions commands/remove.js
@@ -1,12 +1,18 @@
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
name: "remove",
description: "Remove song from the queue",
execute(message, args) {
if (!canModifyQueue(message.member)) return;

if (!args.length) return message.reply(`Usage: ${message.client.prefix}remove <Queue Number>`);
const serverQueue = message.client.queue.get(message.guild.id);
if (!serverQueue) return message.channel.send("There is no queue.").catch(console.error);
if (isNaN(args[0])) return message.reply(`Usage: ${message.client.prefix}remove <Queue Number>`);

const queue = message.client.queue.get(message.guild.id);
if (!queue) return message.channel.send("There is no queue.").catch(console.error);

const song = serverQueue.songs.splice(args[0] - 1, 1);
serverQueue.textChannel.send(`${message.author} ❌ removed **${song[0].title}** from the queue.`);
const song = queue.songs.splice(args[0] - 1, 1);
queue.textChannel.send(`${message.author} ❌ removed **${song[0].title}** from the queue.`);
}
};
15 changes: 8 additions & 7 deletions commands/resume.js
@@ -1,17 +1,18 @@
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
name: "resume",
aliases: ['r'],
description: "Resume currently playing music",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id);
if (!canModifyQueue(message.member)) return;

if (!message.member.voice.channel)
return message.reply("You need to join a voice channel first!").catch(console.error);
const queue = message.client.queue.get(message.guild.id);

if (serverQueue && !serverQueue.playing) {
serverQueue.playing = true;
serverQueue.connection.dispatcher.resume();
return serverQueue.textChannel.send(`${message.author} ▶ resumed the music!`).catch(console.error);
if (queue && !queue.playing) {
queue.playing = true;
queue.connection.dispatcher.resume();
return queue.textChannel.send(`${message.author} ▶ resumed the music!`).catch(console.error);
}
return message.reply("There is nothing playing.").catch(console.error);
}
Expand Down
18 changes: 10 additions & 8 deletions commands/shuffle.js
@@ -1,21 +1,23 @@
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
name: "shuffle",
description: "Shuffle queue",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id);
if (!canModifyQueue(message.member)) return;

const queue = message.client.queue.get(message.guild.id);

if (!message.member.voice.channel)
return message.reply("You need to join a voice channel first!").catch(console.error);
if (!serverQueue)
if (!queue)
return message.channel.send("Playlist is empty.").catch(console.error);

let songs = serverQueue.songs;
let songs = queue.songs;
for (let i = songs.length - 1; i > 1; i--) {
let j = 1 + Math.floor(Math.random() * (i));
[songs[i], songs[j]] = [songs[j], songs[i]];
}
serverQueue.songs = songs;
message.client.queue.set(message.guild.id, serverQueue);
serverQueue.textChannel.send(`${message.author} 🔀 shuffled the queue`).catch(console.error);
queue.songs = songs;
message.client.queue.set(message.guild.id, queue);
queue.textChannel.send(`${message.author} 🔀 shuffled the queue`).catch(console.error);
}
};
13 changes: 7 additions & 6 deletions commands/skip.js
@@ -1,16 +1,17 @@
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
name: "skip",
aliases: ['s'],
description: "Skip the currently playing song",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id);
if (!canModifyQueue(message.member)) return;

if (!message.member.voice.channel)
return message.reply("You need to join a voice channel first!").catch(console.error);
if (!serverQueue)
const queue = message.client.queue.get(message.guild.id);
if (!queue)
return message.channel.send("There is nothing playing that I could skip for you.").catch(console.error);

serverQueue.connection.dispatcher.end();
serverQueue.textChannel.send(`${message.author} ⏭ skipped the song`).catch(console.error);
queue.connection.dispatcher.end();
queue.textChannel.send(`${message.author} ⏭ skipped the song`).catch(console.error);
}
};
16 changes: 9 additions & 7 deletions commands/stop.js
@@ -1,15 +1,17 @@
const { canModifyQueue } = require("../util/EvobotUtil");


module.exports = {
name: "stop",
description: "Stops the music",
execute(message) {
const serverQueue = message.client.queue.get(message.guild.id);
if (!canModifyQueue(message.member)) return;

if (!message.member.voice.channel)
return message.reply("You need to join a voice channel first!").catch(console.error);
if (!serverQueue) return message.reply("There is nothing playing.").catch(console.error);
const queue = message.client.queue.get(message.guild.id);
if (!queue) return message.reply("There is nothing playing.").catch(console.error);

serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
serverQueue.textChannel.send(`${message.author} ⏹ stopped the music!`).catch(console.error);
queue.songs = [];
queue.connection.dispatcher.end();
queue.textChannel.send(`${message.author} ⏹ stopped the music!`).catch(console.error);
}
};
16 changes: 12 additions & 4 deletions include/play.js
@@ -1,4 +1,5 @@
const ytdlDiscord = require("ytdl-core-discord");
const { canModifyQueue } = require("../util/EvobotUtil");

module.exports = {
async play(song, message) {
Expand Down Expand Up @@ -75,15 +76,20 @@ module.exports = {
collector.on("collect", (reaction, user) => {
// Stop if there is no queue on the server
if (!queue) return;
const member = message.guild.member(user);

switch (reaction.emoji.name) {
case "⏭":
reaction.users.remove(user).catch(console.error);
if (!canModifyQueue(member)) return
queue.connection.dispatcher.end();
queue.textChannel.send(`${user} ⏩ skipped the song`).catch(console.error);
collector.stop();
break;

case "⏯":
reaction.users.remove(user).catch(console.error);
if (!canModifyQueue(member)) return
if (queue.playing) {
queue.playing = !queue.playing;
queue.connection.dispatcher.pause();
Expand All @@ -93,16 +99,18 @@ module.exports = {
queue.connection.dispatcher.resume();
queue.textChannel.send(`${user} ▶ resumed the music!`).catch(console.error);
}
reaction.users.remove(user);
break;

case "🔁":
reaction.users.remove(user).catch(console.error);
if (!canModifyQueue(member)) return
queue.loop = !queue.loop;
queue.textChannel.send(`Loop is now ${queue.loop ? "**on**" : "**off**"}`).catch(console.error);
reaction.users.remove(user);
break;

case "⏹":
reaction.users.remove(user).catch(console.error);
if (!canModifyQueue(member)) return
queue.songs = [];
queue.textChannel.send(`${user} ⏹ stopped the music!`).catch(console.error);
try {
Expand All @@ -115,13 +123,13 @@ module.exports = {
break;

default:
reaction.users.remove(user);
reaction.users.remove(user).catch(console.error);
break;
}
});

collector.on("end", () => {
playingMessage.reactions.removeAll();
playingMessage.reactions.removeAll().catch(console.error);
});
}
};
13 changes: 13 additions & 0 deletions util/EvobotUtil.js
@@ -0,0 +1,13 @@
module.exports = {
canModifyQueue(member) {
const { channel } = member.voice;
const botChannel = member.guild.me.voice.channel;

if (channel !== botChannel) {
member.send("You need to join the voice channel first!").catch(console.error);
return false;
}

return true;
}
};

0 comments on commit 9700cff

Please sign in to comment.