Skip to content

Commit

Permalink
fix: return early if missing permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
eritislami committed Nov 6, 2020
1 parent d750df5 commit 4a066d0
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions commands/queue.js
Expand Up @@ -5,58 +5,70 @@ module.exports = {
aliases: ["q"],
description: "Show the music queue and now playing.",
async execute(message) {
const permissions = message.channel.permissionsFor(message.client.user);
if (!permissions.has(["MANAGE_MESSAGES", "ADD_REACTIONS"]))
return message.reply("Missing permission to manage messages or add reactions");

const serverQueue = message.client.queue.get(message.guild.id);
if (!serverQueue) return message.channel.send("❌ **Nothing playing in this server**");

let currentPage = 0;
const embeds = generateQueueEmbed(message, serverQueue.songs);

const queueEmbed = await message.channel.send(
`**Current Page - ${currentPage + 1}/${embeds.length}**`,
embeds[currentPage]
);

try {
let currentPage = 0;
const embeds = generateQueueEmbed(message, serverQueue.songs);
const queueEmbed = await message.channel.send(
`**Current Page - ${currentPage + 1}/${embeds.length}**`,
embeds[currentPage]
);
await queueEmbed.react("⬅️");
await queueEmbed.react("⏹");
await queueEmbed.react("➡️");
} catch (error) {
console.error(error);
message.channel.send(error.message).catch(console.error);
}

const filter = (reaction, user) =>
["⬅️", "⏹", "➡️"].includes(reaction.emoji.name) && message.author.id === user.id;
const collector = queueEmbed.createReactionCollector(filter, { time: 60000 });
const filter = (reaction, user) =>
["⬅️", "⏹", "➡️"].includes(reaction.emoji.name) && message.author.id === user.id;
const collector = queueEmbed.createReactionCollector(filter, { time: 60000 });

collector.on("collect", async (reaction, user) => {
try {
if (reaction.emoji.name === "➡️") {
if (currentPage < embeds.length - 1) {
currentPage++;
queueEmbed.edit(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
}
} else if (reaction.emoji.name === "⬅️") {
if (currentPage !== 0) {
--currentPage;
queueEmbed.edit(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
}
} else {
collector.stop();
reaction.message.reactions.removeAll();
collector.on("collect", async (reaction, user) => {
try {
if (reaction.emoji.name === "➡️") {
if (currentPage < embeds.length - 1) {
currentPage++;
queueEmbed.edit(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
}
} else if (reaction.emoji.name === "⬅️") {
if (currentPage !== 0) {
--currentPage;
queueEmbed.edit(`**Current Page - ${currentPage + 1}/${embeds.length}**`, embeds[currentPage]);
}
await reaction.users.remove(message.author.id);
} catch {
return message.channel.send("**Missing Permissions - [ADD_REACTIONS, MANAGE_MESSAGES]!**");
} else {
collector.stop();
reaction.message.reactions.removeAll();
}
});
} catch {
return message.channel.send("**Missing Permissions - [ADD_REACTIONS, MANAGE_MESSAGES]!**");
}
await reaction.users.remove(message.author.id);
} catch (error) {
console.error(error);
return message.channel.send(error.message).catch(console.error);
}
});
}
};

function generateQueueEmbed(message, queue) {
const embeds = [];
let embeds = [];
let k = 10;

for (let i = 0; i < queue.length; i += 10) {
const current = queue.slice(i, k);
let j = i;
k += 10;

const info = current.map((track) => `${++j} - [${track.title}](${track.url})`).join("\n");

const embed = new MessageEmbed()
.setTitle("Song Queue\n")
.setThumbnail(message.guild.iconURL())
Expand All @@ -65,5 +77,6 @@ function generateQueueEmbed(message, queue) {
.setTimestamp();
embeds.push(embed);
}

return embeds;
}

0 comments on commit 4a066d0

Please sign in to comment.