handle destroyed
This commit is contained in:
parent
2a6d9a74f4
commit
6db3cdb7b2
1 changed files with 22 additions and 12 deletions
|
@ -23,7 +23,7 @@ class Queue<T = unknown> {
|
||||||
public _cooldownsTimeout = new Collection<string, NodeJS.Timeout>();
|
public _cooldownsTimeout = new Collection<string, NodeJS.Timeout>();
|
||||||
private _activeFilters: any[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any
|
private _activeFilters: any[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||||
private _filtersUpdate = false;
|
private _filtersUpdate = false;
|
||||||
public destroyed = false;
|
#destroyed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue constructor
|
* Queue constructor
|
||||||
|
@ -52,12 +52,6 @@ class Queue<T = unknown> {
|
||||||
*/
|
*/
|
||||||
this.options = {};
|
this.options = {};
|
||||||
|
|
||||||
/**
|
|
||||||
* If this queue is destroyed
|
|
||||||
* @type {boolean}
|
|
||||||
* @name Queue#destroyed
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue repeat mode
|
* Queue repeat mode
|
||||||
* @type {QueueRepeatMode}
|
* @type {QueueRepeatMode}
|
||||||
|
@ -112,6 +106,14 @@ class Queue<T = unknown> {
|
||||||
return this.connection.audioResource?.metadata ?? this.tracks[0];
|
return this.connection.audioResource?.metadata ?? this.tracks[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this queue is destroyed
|
||||||
|
* @type {boolean}
|
||||||
|
*/
|
||||||
|
get destroyed() {
|
||||||
|
return this.#destroyed;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns current track
|
* Returns current track
|
||||||
* @returns {Track}
|
* @returns {Track}
|
||||||
|
@ -182,11 +184,12 @@ class Queue<T = unknown> {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
destroy(disconnect = this.options.leaveOnStop) {
|
destroy(disconnect = this.options.leaveOnStop) {
|
||||||
|
this.#watchDestroyed();
|
||||||
this.connection.end();
|
this.connection.end();
|
||||||
if (disconnect) this.connection.disconnect();
|
if (disconnect) this.connection.disconnect();
|
||||||
this.player.queues.delete(this.guild.id);
|
this.player.queues.delete(this.guild.id);
|
||||||
this.player.voiceUtils.cache.delete(this.guild.id);
|
this.player.voiceUtils.cache.delete(this.guild.id);
|
||||||
this.destroyed = true;
|
this.#destroyed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -408,6 +411,7 @@ class Queue<T = unknown> {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
stop() {
|
stop() {
|
||||||
|
this.#watchDestroyed();
|
||||||
return this.destroy();
|
return this.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,6 +420,7 @@ class Queue<T = unknown> {
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
shuffle() {
|
shuffle() {
|
||||||
|
this.#watchDestroyed();
|
||||||
if (!this.tracks.length || this.tracks.length < 3) return false;
|
if (!this.tracks.length || this.tracks.length < 3) return false;
|
||||||
const currentTrack = this.tracks.shift();
|
const currentTrack = this.tracks.shift();
|
||||||
|
|
||||||
|
@ -435,6 +440,7 @@ class Queue<T = unknown> {
|
||||||
* @returns {Track}
|
* @returns {Track}
|
||||||
*/
|
*/
|
||||||
remove(track: Track | number) {
|
remove(track: Track | number) {
|
||||||
|
this.#watchDestroyed();
|
||||||
let trackFound: Track = null;
|
let trackFound: Track = null;
|
||||||
if (typeof track === "number") {
|
if (typeof track === "number") {
|
||||||
trackFound = this.tracks[track];
|
trackFound = this.tracks[track];
|
||||||
|
@ -457,6 +463,7 @@ class Queue<T = unknown> {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
jump(track: Track | number): void {
|
jump(track: Track | number): void {
|
||||||
|
this.#watchDestroyed();
|
||||||
const foundTrack = this.remove(track);
|
const foundTrack = this.remove(track);
|
||||||
if (!foundTrack) throw new Error("Track not found");
|
if (!foundTrack) throw new Error("Track not found");
|
||||||
this.tracks.splice(1, 0, foundTrack);
|
this.tracks.splice(1, 0, foundTrack);
|
||||||
|
@ -466,9 +473,9 @@ class Queue<T = unknown> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {object} PlayerTimestamp
|
* @typedef {object} PlayerTimestamp
|
||||||
* @param {string} current The current progress
|
* @property {string} current The current progress
|
||||||
* @param {string} end The total time
|
* @property {string} end The total time
|
||||||
* @param {number} progress Progress in %
|
* @property {number} progress Progress in %
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -477,6 +484,7 @@ class Queue<T = unknown> {
|
||||||
* @returns {PlayerTimestamp}
|
* @returns {PlayerTimestamp}
|
||||||
*/
|
*/
|
||||||
getPlayerTimestamp(queue = false) {
|
getPlayerTimestamp(queue = false) {
|
||||||
|
this.#watchDestroyed();
|
||||||
const previousTracksTime = this.previousTracks.length > 0 ? this.previousTracks.reduce((p, c) => p + c.durationMS, 0) : 0;
|
const previousTracksTime = this.previousTracks.length > 0 ? this.previousTracks.reduce((p, c) => p + c.durationMS, 0) : 0;
|
||||||
const currentStreamTime = queue ? previousTracksTime + this.streamTime : this.streamTime;
|
const currentStreamTime = queue ? previousTracksTime + this.streamTime : this.streamTime;
|
||||||
const totalTracksTime = this.totalTime;
|
const totalTracksTime = this.totalTime;
|
||||||
|
@ -498,6 +506,7 @@ class Queue<T = unknown> {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
createProgressBar(options: PlayerProgressbarOptions = { timecodes: true, queue: false }) {
|
createProgressBar(options: PlayerProgressbarOptions = { timecodes: true, queue: false }) {
|
||||||
|
this.#watchDestroyed();
|
||||||
const previousTracksTime = this.previousTracks.length > 0 ? this.previousTracks.reduce((p, c) => p + c.durationMS, 0) : 0;
|
const previousTracksTime = this.previousTracks.length > 0 ? this.previousTracks.reduce((p, c) => p + c.durationMS, 0) : 0;
|
||||||
const currentStreamTime = options.queue ? previousTracksTime + this.streamTime : this.streamTime;
|
const currentStreamTime = options.queue ? previousTracksTime + this.streamTime : this.streamTime;
|
||||||
const totalTracksTime = this.totalTime;
|
const totalTracksTime = this.totalTime;
|
||||||
|
@ -532,6 +541,7 @@ class Queue<T = unknown> {
|
||||||
* @type {Number}
|
* @type {Number}
|
||||||
*/
|
*/
|
||||||
get totalTime(): number {
|
get totalTime(): number {
|
||||||
|
this.#watchDestroyed();
|
||||||
return this.tracks.length > 0 ? this.tracks.map((t) => t.durationMS).reduce((p, c) => p + c) : 0;
|
return this.tracks.length > 0 ? this.tracks.map((t) => t.durationMS).reduce((p, c) => p + c) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -666,7 +676,7 @@ class Queue<T = unknown> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#watchDestroyed() {
|
#watchDestroyed() {
|
||||||
if (this.destroyed) throw new Error("Cannot use destroyed queue");
|
if (this.#destroyed) throw new Error("Cannot use destroyed queue");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue