feat(Queue): add skipTo method

This commit is contained in:
DevAndromeda 2021-09-06 20:02:55 +05:45
parent ad16ae197f
commit 751ce122bc
No known key found for this signature in database
GPG key ID: FA40E3EC5CB6DCD6

View file

@ -486,6 +486,17 @@ class Queue<T = unknown> {
return trackFound;
}
/**
* Returns the index of the specified track. If found, returns the track index else returns -1.
* @param {number|Track|Snowflake} track The track
* @returns {number}
*/
getTrackPosition(track: number | Track | Snowflake) {
if (this.#watchDestroyed()) return;
if (typeof track === "number") return this.tracks[track] != null ? track : -1;
return this.tracks.findIndex((pred) => pred.id === (track instanceof Track ? track.id : track));
}
/**
* Jumps to particular track
* @param {Track|number} track The track
@ -501,6 +512,22 @@ class Queue<T = unknown> {
return void this.skip();
}
/**
* Jumps to particular track, removing other tracks on the way
* @param {Track|number} track The track
* @returns {void}
*/
skipTo(track: Track | number): void {
if (this.#watchDestroyed()) return;
const trackIndex = this.getTrackPosition(track);
const removedTrack = this.remove(track);
if (!removedTrack) throw new PlayerError("Track not found", ErrorStatusCode.TRACK_NOT_FOUND);
this.tracks.splice(0, trackIndex, removedTrack);
return void this.skip();
}
/**
* Inserts the given track to specified index
* @param {Track} track The track to insert