2021-04-06 17:58:46 +05:00
|
|
|
import { Message, Snowflake, VoiceConnection } from 'discord.js';
|
|
|
|
import AudioFilters from '../utils/AudioFilters';
|
2021-04-06 20:38:17 +05:00
|
|
|
import { Player } from '../Player';
|
2021-04-06 17:58:46 +05:00
|
|
|
import { EventEmitter } from 'events';
|
2021-04-06 20:38:17 +05:00
|
|
|
import { Track } from './Track';
|
2021-04-06 17:58:46 +05:00
|
|
|
import { QueueFilters } from '../types/types';
|
2021-04-04 22:36:40 +05:00
|
|
|
|
2021-04-06 20:38:17 +05:00
|
|
|
export class Queue extends EventEmitter {
|
2021-04-09 17:59:14 +05:00
|
|
|
/**
|
|
|
|
* The player that instantiated this Queue
|
|
|
|
*/
|
2021-04-04 22:36:40 +05:00
|
|
|
public player!: Player;
|
2021-04-06 17:55:29 +05:00
|
|
|
public guildID: Snowflake;
|
|
|
|
public voiceConnection?: VoiceConnection;
|
|
|
|
public stream?: any;
|
|
|
|
public tracks: Track[];
|
|
|
|
public previousTracks: Track[];
|
|
|
|
public stopped: boolean;
|
|
|
|
public lastSkipped: boolean;
|
|
|
|
public volume: number;
|
|
|
|
public paused: boolean;
|
|
|
|
public repeatMode: boolean;
|
|
|
|
public loopMode: boolean;
|
|
|
|
public filters: QueueFilters;
|
|
|
|
public additionalStreamTime: number;
|
|
|
|
public firstMessage: Message;
|
2021-04-11 18:27:48 +05:00
|
|
|
public autoPlay = false;
|
2021-04-06 17:55:29 +05:00
|
|
|
|
2021-04-11 17:51:50 +05:00
|
|
|
constructor(player: Player, message: Message) {
|
2021-04-06 17:55:29 +05:00
|
|
|
super();
|
2021-04-04 22:36:40 +05:00
|
|
|
|
2021-04-06 17:58:46 +05:00
|
|
|
Object.defineProperty(this, 'player', { value: player, enumerable: false });
|
2021-04-06 17:55:29 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ID of the guild assigned to this queue
|
|
|
|
*/
|
|
|
|
this.guildID = message.guild.id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The voice connection of this queue
|
|
|
|
*/
|
|
|
|
this.voiceConnection = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks of this queue
|
|
|
|
*/
|
|
|
|
this.tracks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Previous tracks of this queue
|
|
|
|
*/
|
|
|
|
this.previousTracks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the player of this queue is stopped
|
|
|
|
*/
|
|
|
|
this.stopped = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If last track was skipped
|
|
|
|
*/
|
|
|
|
this.lastSkipped = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue volume
|
|
|
|
*/
|
|
|
|
this.volume = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the player of this queue is paused
|
|
|
|
*/
|
|
|
|
this.paused = Boolean(this.voiceConnection?.dispatcher?.paused);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If repeat mode is enabled in this queue
|
|
|
|
*/
|
|
|
|
this.repeatMode = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If loop mode is enabled in this queue
|
|
|
|
*/
|
|
|
|
this.loopMode = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The additional calculated stream time
|
|
|
|
*/
|
|
|
|
this.additionalStreamTime = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial message object
|
|
|
|
*/
|
|
|
|
this.firstMessage = message;
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
this.filters = {};
|
|
|
|
|
2021-04-06 17:58:46 +05:00
|
|
|
Object.keys(AudioFilters).forEach((fn) => {
|
2021-04-06 17:55:29 +05:00
|
|
|
// @ts-ignore
|
|
|
|
this.filters[fn] = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently playing track
|
|
|
|
*/
|
|
|
|
get playing() {
|
|
|
|
return this.tracks[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculated volume of this queue
|
|
|
|
*/
|
|
|
|
get calculatedVolume() {
|
2021-04-11 19:15:58 +05:00
|
|
|
return this.filters.normalizer ? this.volume + 70 : this.volume;
|
2021-04-06 17:55:29 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Total duration
|
|
|
|
*/
|
|
|
|
get totalTime() {
|
|
|
|
return this.tracks.length > 0 ? this.tracks.map((t) => t.durationMS).reduce((p, c) => p + c) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current stream time
|
|
|
|
*/
|
|
|
|
get currentStreamTime() {
|
|
|
|
return this.voiceConnection?.dispatcher?.streamTime + this.additionalStreamTime || 0;
|
2021-04-04 22:36:40 +05:00
|
|
|
}
|
2021-04-17 19:28:12 +05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets audio filters in this player
|
|
|
|
* @param filters Audio filters to set
|
|
|
|
*/
|
|
|
|
setFilters(filters: QueueFilters) {
|
|
|
|
return this.player.setFilters(this.firstMessage, filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns array of all enabled filters
|
|
|
|
*/
|
|
|
|
getFiltersEnabled() {
|
|
|
|
const filters: string[] = [];
|
|
|
|
|
|
|
|
for (const filter in this.filters) {
|
|
|
|
if (this.filters[filter as keyof QueueFilters] !== false) filters.push(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all disabled filters
|
|
|
|
*/
|
|
|
|
getFiltersDisabled() {
|
|
|
|
const enabled = this.getFiltersEnabled();
|
|
|
|
|
|
|
|
return Object.keys(this.filters).filter((f) => !enabled.includes(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `<Queue ${this.guildID}>`;
|
|
|
|
}
|
2021-04-04 22:44:45 +05:00
|
|
|
}
|
2021-04-06 20:38:17 +05:00
|
|
|
|
|
|
|
export default Queue;
|