2020-07-04 18:26:47 +05:00
|
|
|
const Discord = require('discord.js')
|
|
|
|
const { EventEmitter } = require('events')
|
|
|
|
const Track = require('./Track')
|
2020-08-13 18:21:42 +05:00
|
|
|
const Player = require('./Player')
|
2020-01-12 00:01:18 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a guild queue.
|
|
|
|
*/
|
|
|
|
class Queue extends EventEmitter {
|
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* @param {Discord.Snowflake} guildID ID of the guild this queue is for.
|
2020-08-13 18:21:42 +05:00
|
|
|
* @param {Discord.Message} message Message that initialized the queue
|
|
|
|
* @param {import('./Player').Filters[]} filters Filters the queue should be initialized with.
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-08-13 18:21:42 +05:00
|
|
|
constructor (guildID, message, filters) {
|
2020-07-04 18:26:47 +05:00
|
|
|
super()
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* ID of the guild this queue is for.
|
|
|
|
* @type {Discord.Snowflake}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.guildID = guildID
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* The voice connection of this queue.
|
|
|
|
* @type {Discord.VoiceConnection}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.voiceConnection = null
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* The tracks of this queue. The first one is currenlty playing and the others are going to be played.
|
|
|
|
* @type {Track[]}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.tracks = []
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
|
|
|
* Whether the stream is currently stopped.
|
2020-07-04 18:26:47 +05:00
|
|
|
* @type {boolean}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.stopped = false
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* Whether the last track was skipped.
|
|
|
|
* @type {boolean}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.lastSkipped = false
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* The stream volume of this queue. (0-100)
|
|
|
|
* @type {number}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.volume = 100
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* Whether the stream is currently paused.
|
|
|
|
* @type {boolean}
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.paused = true
|
2020-01-18 15:03:22 +05:00
|
|
|
/**
|
|
|
|
* Whether the repeat mode is enabled.
|
2020-07-04 18:26:47 +05:00
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.repeatMode = false
|
|
|
|
/**
|
|
|
|
* Filters status
|
|
|
|
* @type {Filters}
|
|
|
|
*/
|
|
|
|
this.filters = {}
|
2020-08-13 18:21:42 +05:00
|
|
|
Object.keys(filters).forEach((f) => {
|
|
|
|
this.filters[f] = false
|
|
|
|
})
|
2020-07-04 18:26:47 +05:00
|
|
|
/**
|
|
|
|
* Additional stream time
|
|
|
|
* @type {Number}
|
2020-01-18 15:03:22 +05:00
|
|
|
*/
|
2020-07-04 18:26:47 +05:00
|
|
|
this.additionalStreamTime = 0
|
2020-08-13 18:21:42 +05:00
|
|
|
/**
|
|
|
|
* Message that initialized the queue
|
|
|
|
* @type {Discord.Message}
|
|
|
|
*/
|
|
|
|
this.firstMessage = message
|
|
|
|
}
|
|
|
|
|
|
|
|
get playing () {
|
|
|
|
return this.tracks[0]
|
2020-01-12 00:01:18 +05:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:26:47 +05:00
|
|
|
get calculatedVolume () {
|
|
|
|
return this.filters.bassboost ? this.volume + 50 : this.volume
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Queue
|
2020-01-12 00:01:18 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emitted when the queue is empty.
|
|
|
|
* @event Queue#end
|
2020-07-04 18:26:47 +05:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* client.on('message', (message) => {
|
|
|
|
*
|
|
|
|
* const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
|
|
|
|
* const command = args.shift().toLowerCase();
|
|
|
|
*
|
|
|
|
* if(command === 'play'){
|
|
|
|
*
|
|
|
|
* let track = await client.player.play(message.member.voice.channel, args[0]);
|
|
|
|
*
|
|
|
|
* track.queue.on('end', () => {
|
|
|
|
* message.channel.send('The queue is empty, please add new tracks!');
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* });
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|
|
|
|
|
2020-01-12 22:55:56 +05:00
|
|
|
/**
|
|
|
|
* Emitted when the voice channel is empty.
|
|
|
|
* @event Queue#channelEmpty
|
|
|
|
*/
|
|
|
|
|
2020-01-12 00:01:18 +05:00
|
|
|
/**
|
2020-07-04 18:26:47 +05:00
|
|
|
* Emitted when the track changes.
|
|
|
|
* @event Queue#trackChanged
|
|
|
|
* @param {Track} oldTrack The old track (playing before)
|
|
|
|
* @param {Track} newTrack The new track (currently playing)
|
2020-01-12 00:01:18 +05:00
|
|
|
* @param {Boolean} skipped Whether the change is due to the skip() function
|
2020-07-04 18:26:47 +05:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* client.on('message', (message) => {
|
|
|
|
*
|
|
|
|
* const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
|
|
|
|
* const command = args.shift().toLowerCase();
|
|
|
|
*
|
|
|
|
* if(command === 'play'){
|
|
|
|
*
|
|
|
|
* let track = await client.player.play(message.member.voice.channel, args[0]);
|
|
|
|
*
|
|
|
|
* track.queue.on('trackChanged', (oldTrack, newTrack, skipped, repeatMode) => {
|
|
|
|
* if(repeatMode){
|
|
|
|
* message.channel.send(`Playing ${newTrack} again...`);
|
|
|
|
* } else {
|
|
|
|
* message.channel.send(`Now playing ${newTrack}...`);
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* });
|
2020-01-12 00:01:18 +05:00
|
|
|
*/
|