Skip to content

Commit

Permalink
Pageable Queue (#265)
Browse files Browse the repository at this point in the history
* Pageable Queue

A simple pageable queue which can be controlled with reactions instead of sending separate embeds. Edits the single embed which is sent as the queue. Great for large queues and does not flood the queue channel if the queue is too big

* Update queue.js

* style: format with prettier

* fix: add max time to collector

Co-authored-by: Erit Islami <eritislami@gmail.com>
  • Loading branch information
badRyuk and eritislami committed Oct 27, 2020
1 parent 9589606 commit 202697e
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions commands/queue.js
@@ -1,30 +1,69 @@
const { MessageEmbed, splitMessage, escapeMarkdown } = require("discord.js");
const { MessageEmbed } = require("discord.js");

module.exports = {
name: "queue",
aliases: ["q"],
description: "Show the music queue and now playing.",
execute(message) {
const queue = message.client.queue.get(message.guild.id);
if (!queue) return message.reply("There is nothing playing.").catch(console.error);
async execute(message) {
const serverQueue = message.client.queue.get(message.guild.id);
if (!serverQueue) return message.channel.send("❌ **Nothing playing in this server**");
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("➡️");

const description = queue.songs.map((song, index) => `${index + 1}. ${escapeMarkdown(song.title)}`);
const filter = (reaction, user) =>
["⬅️", "⏹", "➡️"].includes(reaction.emoji.name) && message.author.id === user.id;
const collector = queueEmbed.createReactionCollector(filter, { time: 60000 });

let queueEmbed = new MessageEmbed()
.setTitle("EvoBot Music Queue")
.setDescription(description)
.setColor("#F8AA2A");

const splitDescription = splitMessage(description, {
maxLength: 2048,
char: "\n",
prepend: "",
append: ""
});

splitDescription.forEach(async (m) => {
queueEmbed.setDescription(m);
message.channel.send(queueEmbed);
});
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();
}
await reaction.users.remove(message.author.id);
} catch {
return message.channel.send("**Missing Permissions - [ADD_REACTIONS, MANAGE_MESSAGES]!**");
}
});
} catch {
return message.channel.send("**Missing Permissions - [ADD_REACTIONS, MANAGE_MESSAGES]!**");
}
}
};

function generateQueueEmbed(message, queue) {
const 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())
.setColor("#F8AA2A")
.setDescription(`**Current Song - [${queue[0].title}](${queue[0].url})**\n\n${info}`)
.setTimestamp();
embeds.push(embed);
}
return embeds;
}

0 comments on commit 202697e

Please sign in to comment.