damonjs
Version:
A modified Shoukaku wrapper with enhanced queue support.
129 lines (128 loc) • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DamonJsQueue = void 0;
const DamonJsTrack_1 = require("./DamonJsTrack");
const Interfaces_1 = require("../../Modules/Interfaces");
class DamonJsQueue extends Array {
constructor(player) {
super();
/** Current playing trackId
* Do not do anything to this if you do anything to this player is likely gonna fail
*/
this.currentId = 0;
this.player = player;
}
/** Get the size of queue */
get size() {
return this.length;
}
/** Get the size of queue including current */
get totalSize() {
return this.length + (this.current ? 1 : 0);
}
/** Check if the queue is empty or not */
get isEmpty() {
return this.length === 0;
}
/** Check if the queue is ended or not */
get isEnd() {
return this.length <= this.currentId + 1;
}
/** Get the queue's duration */
get durationLength() {
return this.reduce((acc, cur) => acc + (cur.length || 0), 0);
}
/** Current playing track */
get current() {
return this._current;
}
/**
* set current track of the queue
* @param track DamonJsTrack to add
*/
set current(track) {
this._current = track;
this.player.emit(Interfaces_1.Events.InitQueue, this.player);
}
/** last played/playing track */
get lastTrack() {
return this.at(this.length - 1);
}
/**
* Add track(s) to the queue
* @param track DamonJsTrack to add
* @returns DamonJsQueue
*/
add(track) {
if (Array.isArray(track) && track.some((t) => !(t instanceof DamonJsTrack_1.DamonJsTrack)))
throw new Interfaces_1.DamonJsError(1, 'Track must be an instance of DamonJsTrack');
if (!Array.isArray(track) && !(track instanceof DamonJsTrack_1.DamonJsTrack))
track = [track];
if (Array.isArray(track))
for (const t of track)
this.push(t);
else
this.push(track);
this.player.emit(Interfaces_1.Events.InitQueue, this.player);
return this;
}
/**
* Remove track from the queue
* @param position Position of the track
* @returns DamonJsQueue
*/
remove(position) {
if (position < 0 || position >= this.length)
throw new Interfaces_1.DamonJsError(1, 'Position must be between 0 and ' + (this.length - 1));
if (position === this.currentId)
throw new Interfaces_1.DamonJsError(1, 'You cannot remove the current Playing song');
this.splice(position, 1);
this.player.emit(Interfaces_1.Events.InitQueue, this.player);
return this;
}
/** Shuffle the queue */
shuffle() {
const unplayedSongs = this.slice(this.currentId + 1); // Get unplayed songs after the current song
for (let i = unplayedSongs.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[unplayedSongs[i], unplayedSongs[j]] = [unplayedSongs[j], unplayedSongs[i]];
}
// Reconstruct the queue with the shuffled unplayed songs after the current song
const newQueue = [...this.slice(0, this.currentId + 1), ...unplayedSongs];
this.splice(0, this.length, ...newQueue);
this.player.emit(Interfaces_1.Events.InitQueue, this.player);
return this;
}
/** Clear the queue */
clear() {
const currentTrack = this.splice(this.currentId, 1); // Remove and keep the element at the specified index
this.splice(0, this.length, ...currentTrack);
this.currentId = 0;
this.player.emit(Interfaces_1.Events.InitQueue, this.player);
return this;
}
removeDupes() {
const trackUris = new Set();
const playedTracks = this.slice(0, this.currentId);
const unplayedTracks = this.slice(this.currentId + 1);
const currentTrack = this[this.currentId];
trackUris.add(currentTrack.uri);
const newPlayedTracks = playedTracks.filter((track) => {
if (trackUris.has(track.uri))
return false;
trackUris.add(track.uri);
return true;
});
const newUnplayedTracks = unplayedTracks.filter((track) => {
if (trackUris.has(track.uri))
return false;
trackUris.add(track.uri);
return true;
});
this.currentId = newPlayedTracks.length;
this.splice(0, this.length, ...newPlayedTracks, currentTrack, ...newUnplayedTracks);
this.player.emit(Interfaces_1.Events.InitQueue, this.player);
return this;
}
}
exports.DamonJsQueue = DamonJsQueue;