UNPKG

riffy-extended

Version:

Riffy is a pro lavalink client. It is designed to be simple and easy to use, with a focus on stability and more features.

86 lines (72 loc) 2.42 kB
class Queue extends Array { constructor() { super(); this.currentTrackIndex = -1; // Индекс текущего трека, начнем с -1, т.е. ничего не играет } get size() { return this.length; } get first() { return this.length ? this[0] : null; } get current() { // Возвращает текущий трек return this.currentTrackIndex >= 0 && this.currentTrackIndex < this.length ? this[this.currentTrackIndex] : null; } get previous() { // Возвращает предыдущий трек, если он существует return this.currentTrackIndex > 0 ? this[this.currentTrackIndex - 1] : null; } get next() { // Возвращает следующий трек, если он существует return this.currentTrackIndex + 1 < this.length ? this[this.currentTrackIndex + 1] : null; } add(track) { this.push(track); return this; } remove(index) { if (index >= 0 && index < this.length) { return this.splice(index, 1)[0]; } else { throw new Error("Index out of range"); } } clear() { this.length = 0; this.currentTrackIndex = -1; } shuffle() { for (let i = this.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [this[i], this[j]] = [this[j], this[i]]; } } playNext() { if (this.currentTrackIndex + 1 < this.length) { this.currentTrackIndex++; // Переходим к следующему треку return this.current; } else { return null; // Нет следующего трека } } playPrevious() { if (this.currentTrackIndex > 0) { this.currentTrackIndex--; // Переходим к предыдущему треку return this.current; } else { return null; // Нет предыдущего трека } } play(trackIndex) { if (trackIndex >= 0 && trackIndex < this.length) { this.currentTrackIndex = trackIndex; return this.current; } else { throw new Error("Index out of range"); } } } module.exports = { Queue };