Star ✨ on GitHub

Queue

API reference for the Queue class in Moonlink.js

The Queue class manages a player's track queue, allowing you to add, remove, and manipulate tracks. The queue is persistent and synchronized with the internal database.

Properties

PropertyTypeDescriptionDefault
databaseDatabaseDatabase instance for persistenceRequired
guildIdstringGuild ID associated with the queueRequired
playerPlayerThe player instance this queue belongs toRequired
tracksTrack[]Array containing all tracks in queue[]
sizenumberNumber of tracks in queue0
durationnumberTotal duration of all tracks in the queue (milliseconds)0
isEmptybooleanWhether the queue is emptytrue
firstTrackThe first track in the queuenull
lastTrackThe last track in the queuenull
allTrack[]All tracks currently in the queue[]
positionnumberThe current position of the playing track in the queue (0-based index)0
previousTrack[]All tracks before the current playing track in the queue[]

Methods

add

addmethod

Add Track

Adds a track or an array of tracks to the queue and persists it in the database. Filters out blacklisted tracks. Emits queueAdd event.

Parameters
trackrequiredTrack | Track[]
The track or array of tracks to add

Returns & Example

Returnsboolean — Whether the track was added successfully

queue.add(track);

get

getmethod

Get Track

Gets a track at a specific position in the queue.

Parameters
positionrequirednumber
Queue position (0-based index)

Returns & Example

ReturnsTrack — The track at the specified position

const track = queue.get(0); // Get first track

has

hasmethod

Has Track

Checks if a specific track is in the queue.

Parameters
trackrequiredTrack
The track to check

Returns & Example

Returnsboolean — Whether the track exists in queue

if (queue.has(track)) {
  console.log('Track is in queue');
}

remove

removemethod

Remove Track

Removes a track at a specific position and updates the database. Emits queueRemove event.

Parameters
positionrequirednumber
Queue position (0-based index)

Returns & Example

Returnsboolean — Whether the track was removed successfully

queue.remove(0); // Remove first track

shift

shiftmethod

Shift Track

Removes and returns the first track in the queue. Emits queueRemove event.

Returns & Example

ReturnsTrack — The first track in queue

const nextTrack = queue.shift();

unshift

unshiftmethod

Unshift Track

Adds a track to the beginning of the queue. Emits queueAdd event.

Parameters
trackrequiredTrack
The track to add

Returns & Example

Returnsboolean — Whether the track was added successfully

queue.unshift(track);

pop

popmethod

Pop Track

Removes and returns the last track in the queue. Emits queueRemove event.

Returns & Example

ReturnsTrack — The last track in queue

const lastTrack = queue.pop();

clear

clearmethod

Clear Queue

Clears all tracks from the queue and removes from database. Emits queueRemove event.

Returns & Example

Returnsboolean — Whether the queue was cleared successfully

queue.clear();

shuffle

shufflemethod

Shuffle Queue

Randomly shuffles tracks in the queue using Math.random().

Returns & Example

Returnsboolean — Whether the queue was shuffled successfully

queue.shuffle();

removeDuplicates

removeDuplicatesmethod

Remove Duplicates

Removes duplicate tracks from the queue based on their encoded string. Returns true if duplicates were removed, false otherwise.

Returns & Example

Returnsboolean — Whether duplicates were removed.

const removed = queue.removeDuplicates();
if (removed) {
  console.log('Duplicates removed from queue.');
}

removeBlacklistedTracks

removeBlacklistedTracksmethod

Remove Blacklisted Tracks

Removes tracks from the queue whose source is in the manager's blacklistedSources list. Emits trackBlacklisted event for each removed track.

Returns & Example

Returnsbooleantrue if any blacklisted tracks were removed, false otherwise.

const removed = queue.removeBlacklistedTracks();
if (removed) {
  console.log('Blacklisted tracks removed from queue.');
}

sortByTitle

sortByTitlemethod

Sort by Title

Sorts the queue alphabetically by track title.

Returns & Example

Returnsbooleantrue if the queue was sorted, false if there were fewer than 2 tracks.

queue.sortByTitle();

sortByAuthor

sortByAuthormethod

Sort by Author

Sorts the queue alphabetically by track author.

Returns & Example

Returnsbooleantrue if the queue was sorted, false if there were fewer than 2 tracks.

queue.sortByAuthor();

sortByDuration

sortByDurationmethod

Sort by Duration

Sorts the queue by track duration in ascending order.

Returns & Example

Returnsbooleantrue if the queue was sorted, false if there were fewer than 2 tracks.

queue.sortByDuration();

find

findmethod

Find Track

Finds a track in the queue by its identifier or by a partial match in its title.

Parameters
queryrequiredstring
The identifier or a part of the title to search for.

Returns & Example

ReturnsTrack | undefined — The found track or undefined if not found.

const foundTrack = queue.find('my-track-id');
if (!foundTrack) {
  const anotherTrack = queue.find('song title');
}

move

movemethod

Move Track

Moves a track from one position to another within the queue.

Parameters
fromrequirednumber
The starting 0-based index of the track to move.
torequirednumber
The target 0-based index where the track should be moved.

Returns & Example

Returnsbooleantrue if the track was moved successfully, false otherwise.

queue.move(0, 2); // Moves the first track to the third position

moveRange

moveRangemethod

Move Range

Moves a contiguous range of tracks from one position to another within the queue. Emits queueMoveRange event.

Parameters
fromIndexrequirednumber
The starting 0-based index of the range to move.
toIndexrequirednumber
The target 0-based index where the range should be moved.
countrequirednumber
The number of tracks to move.

Returns & Example

Returnsbooleantrue if the tracks were moved successfully, false otherwise.

// Move 2 tracks starting from index 0 to index 3
queue.moveRange(0, 3, 2);

removeRange

removeRangemethod

Remove Range

Removes a range of tracks from the queue. Emits queueRemoveRange event.

Parameters
startIndexrequirednumber
The starting 0-based index of the range to remove.
endIndexrequirednumber
The ending 0-based index of the range to remove (inclusive).

Returns & Example

Returnsbooleantrue if the tracks were removed successfully, false otherwise.

// Remove tracks from index 1 to 3 (inclusive)
queue.removeRange(1, 3);

duplicate

duplicatemethod

Duplicate Track

Duplicates a track in the queue a specified number of times. The duplicated tracks are inserted immediately after the original track. Emits queueDuplicate event.

Parameters
indexrequirednumber
The 0-based index of the track to duplicate.
countnumber
1
The number of times to duplicate the track.

Returns & Example

Returnsbooleantrue if the track was duplicated successfully, false otherwise.

// Duplicate the first track once
queue.duplicate(0);

// Duplicate the second track 3 times
queue.duplicate(1, 3);

jump

jumpmethod

Jump to Track

Removes all tracks before the specified index, effectively making the track at the given index the new first track in the queue.

Parameters
indexrequirednumber
The 0-based index of the track to jump to.

Returns & Example

Returnsbooleantrue if the jump was successful, false otherwise.

// Make the track at index 5 the new first track
queue.jump(5);

slice

slicemethod

Slice Queue

Returns a shallow copy of a portion of the queue into a new array object selected from start to end (end not included).

Parameters
startrequirednumber
The beginning 0-based index of the specified portion of the queue.
endnumber
The end 0-based index of the specified portion of the queue. slice extracts up to (but not including) end.

Returns & Example

ReturnsTrack[] — A new array containing the extracted elements.

const firstTwoTracks = queue.slice(0, 2);

filter

filtermethod

Filter Queue

Creates a new array with all elements that pass the test implemented by the provided function.

Parameters
predicaterequired(track: Track) => boolean
Function to test each element of the queue. Return true to keep the element, false otherwise.

Returns & Example

ReturnsTrack[] — A new array with the elements that pass the test.

const longTracks = queue.filter(track => track.duration > 300000); // Tracks longer than 5 minutes

reverse

reversemethod

Reverse Queue

Reverses the order of the elements in the queue in place.

Returns & Example

Returnsbooleantrue if the queue was reversed successfully, false otherwise.

queue.reverse();