discord-player-play-dl/src/Structures/Queue.ts

109 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-05-01 12:59:42 +05:00
// @ts-nocheck
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-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-21 10:08:33 +05:00
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
this.guildID = message.guild.id;
this.voiceConnection = null;
this.tracks = [];
this.previousTracks = [];
this.stopped = false;
this.lastSkipped = false;
this.volume = 100;
this.paused = Boolean(this.voiceConnection?.dispatcher?.paused);
this.repeatMode = false;
this.loopMode = false;
this.additionalStreamTime = 0;
this.firstMessage = message;
this.filters = {};
2021-04-06 17:58:46 +05:00
Object.keys(AudioFilters).forEach((fn) => {
2021-04-19 18:32:10 +05:00
this.filters[fn as keyof QueueFilters] = false;
2021-04-06 17:55:29 +05:00
});
}
2021-05-01 13:05:12 +05:00
2021-04-19 18:32:10 +05:00
get playing(): Track {
2021-04-06 17:55:29 +05:00
return this.tracks[0];
}
2021-05-01 13:05:12 +05:00
2021-04-19 18:32:10 +05:00
get calculatedVolume(): number {
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
}
2021-04-19 18:32:10 +05:00
get totalTime(): number {
2021-04-06 17:55:29 +05:00
return this.tracks.length > 0 ? this.tracks.map((t) => t.durationMS).reduce((p, c) => p + c) : 0;
}
2021-04-19 18:32:10 +05:00
get currentStreamTime(): number {
2021-04-06 17:55:29 +05:00
return this.voiceConnection?.dispatcher?.streamTime + this.additionalStreamTime || 0;
2021-04-04 22:36:40 +05:00
}
2021-04-17 19:28:12 +05:00
2021-04-19 18:32:10 +05:00
setFilters(filters: QueueFilters): Promise<void> {
2021-04-17 19:28:12 +05:00
return this.player.setFilters(this.firstMessage, filters);
}
2021-04-19 18:32:10 +05:00
getFiltersEnabled(): string[] {
2021-04-17 19:28:12 +05:00
const filters: string[] = [];
for (const filter in this.filters) {
if (this.filters[filter as keyof QueueFilters] !== false) filters.push(filter);
}
return filters;
}
2021-04-19 18:32:10 +05:00
getFiltersDisabled(): string[] {
2021-04-17 19:28:12 +05:00
const enabled = this.getFiltersEnabled();
return Object.keys(this.filters).filter((f) => !enabled.includes(f));
}
2021-04-19 18:32:10 +05:00
toString(): string {
2021-04-17 19:28:12 +05:00
return `<Queue ${this.guildID}>`;
}
2021-04-04 22:44:45 +05:00
}
2021-04-06 20:38:17 +05:00
export default Queue;