feat(Queue): add skipTo method
This commit is contained in:
parent
ad16ae197f
commit
751ce122bc
1 changed files with 27 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue