woomy/src/commands/queue.js

175 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-01-25 10:02:43 +00:00
'use strict';
const { getGuild, createTimestamp } = require('../modules/music')
const Discord = require('discord.js')
2020-01-25 10:02:43 +00:00
exports.run = (client, message, args) => {
var queue = getGuild(message.guild.id).queue
2020-01-25 10:02:43 +00:00
if (queue.length < 1) {
return message.channel.send('<:error:466995152976871434> Nothing is playing.')
2020-01-25 10:02:43 +00:00
}
const lists = []
2020-01-25 10:02:43 +00:00
function generateList (start, number) {
let list = ''
let timestamp
2020-01-25 10:02:43 +00:00
if (start === 1 && queue.length === 1) {
return ['There\'s nothing else waiting to be played!', 1]
2020-01-25 10:02:43 +00:00
}
if (number === 1 && queue.length + 1 < start) {
return false
}
2020-01-25 10:02:43 +00:00
const q = queue.slice(start)
2020-01-25 10:02:43 +00:00
let i = 0
2020-01-25 10:02:43 +00:00
for (i = 0; i < q.length; i++) {
const song = q[i]
2020-01-25 10:02:43 +00:00
timestamp = createTimestamp(song.video.lengthSeconds)
2020-01-25 10:02:43 +00:00
const aaa = list + `\`${(i + 1) + start - 1}:\` **[${song.video.title}](https://www.youtube.com/watch?v=${song.video.videoId})** added by ${song.requestedBy} \`[${timestamp}]\`\n`
2020-01-25 10:02:43 +00:00
if (aaa.length > 1024) {
return [list, start + i - 1]
} else {
list = aaa
}
2020-01-25 10:02:43 +00:00
// totalDuration = totalDuration + song.duration
}
2020-01-25 10:02:43 +00:00
return [list, start + i + 1]
}
2020-01-25 10:02:43 +00:00
const songsInQueue = queue.length - 1
let songsInQueueEnglish = 'song'
2020-01-25 10:02:43 +00:00
function generatePage (list, page) {
if (!list || list === '') {
return false
2020-01-25 10:02:43 +00:00
}
var embed = new Discord.MessageEmbed()
embed.setTitle(`Queue for: ${message.guild.name}`)
embed.setColor(client.embedColour(message))
2020-01-25 10:02:43 +00:00
var elapsedTime = getGuild(message.guild.id).dispatcher.streamTime / 1000
var totalDuration = queue[0].video.lengthSeconds - elapsedTime
2020-01-25 10:02:43 +00:00
let timeRemaining = ''
2020-01-25 10:02:43 +00:00
for (let i = 1; i < queue.length; i++) {
const b = queue[i]
if (b.video.lengthSeconds === 0) {
timeRemaining = '∞'
break
2020-01-25 10:02:43 +00:00
}
totalDuration += b.video.lengthSeconds
2020-01-25 10:02:43 +00:00
}
if (timeRemaining === '') {
const queueDuration = createTimestamp(totalDuration)
timeRemaining = queueDuration
2020-01-25 10:02:43 +00:00
}
let timestamp = `\`${createTimestamp(queue[0].video.lengthSeconds)}\``
2020-01-25 10:02:43 +00:00
if (timestamp !== '`[LIVE]`') {
timestamp = `\`[${createTimestamp(elapsedTime) + '/' + createTimestamp(queue[0].video.lengthSeconds)}]\``
}
embed.addField('Now playing:', `**[${queue[0].video.title}](https://www.youtube.com/watch?v=${queue[0].video.videoId})** added by ${queue[0].requestedBy} ${timestamp}`)
2020-01-25 10:02:43 +00:00
embed.addField('Up next:', list)
2020-01-25 10:02:43 +00:00
if (songsInQueue > 1 || songsInQueue === 0) {
songsInQueueEnglish = 'songs'
2020-01-25 10:02:43 +00:00
}
embed.setFooter(`Page ${page}/${lists.length} | ${songsInQueue + ' ' + songsInQueueEnglish} in queue | ${timeRemaining} time remaining`)
2020-01-25 10:02:43 +00:00
return embed
}
2020-01-25 10:02:43 +00:00
var myMessage = null
2020-01-25 10:02:43 +00:00
function displayPage (number) {
const page = generatePage(lists[number - 1], number)
2020-01-25 10:02:43 +00:00
if (page) {
if (myMessage) {
myMessage.edit(page)
2020-01-25 10:02:43 +00:00
} else {
myMessage = message.channel.send(page)
2020-01-25 10:02:43 +00:00
}
return true
2020-01-25 10:02:43 +00:00
} else {
return false
2020-01-25 10:02:43 +00:00
}
}
2020-01-25 10:02:43 +00:00
function aFunction (start) {
2020-01-25 10:02:43 +00:00
// start - index of song, which we should start with
// end - index of song, which we ended with
const [list, end] = generateList(start, lists.length + 1)
2020-01-25 10:02:43 +00:00
if (list && list !== '') {
lists.push(list)
if (queue[end + 1]) {
aFunction(end + 1)
2020-01-25 10:02:43 +00:00
}
}
}
2020-01-25 10:02:43 +00:00
aFunction(1)
2020-01-25 10:02:43 +00:00
let page = 1
2020-01-25 10:02:43 +00:00
if (args[0]) {
const userPage = Number(args[0])
2020-01-25 10:02:43 +00:00
if (userPage) {
page = userPage
2020-01-25 10:02:43 +00:00
} else {
return message.channel.send(
`<:error:466995152976871434> Invalid page number. Usage: \`${client.commands.get('queue').help.usage}\``
)
2020-01-25 10:02:43 +00:00
}
}
2020-01-25 10:02:43 +00:00
if (displayPage(page)) {
2020-01-25 10:02:43 +00:00
} else {
return message.channel.send(
`<:error:466995152976871434> Page ${page} doesn't exist!`
)
2020-01-25 10:02:43 +00:00
}
}
2020-01-25 10:02:43 +00:00
exports.conf = {
enabled: true,
guildOnly: true,
aliases: [],
permLevel: "User",
requiredPerms: []
};
exports.help = {
name: "queue",
category: "Music",
description: "Displays what songs are in the queue.",
usage: "queue <page>"
};