Skip to content

Commit

Permalink
refactor: ytdl player improvements (#1059)
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayymmeena authored Jun 20, 2024
1 parent 8c41431 commit 5cda217
Show file tree
Hide file tree
Showing 53 changed files with 1,743 additions and 1,100 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/lava-queue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @discordx/lava-queue

## 4.0.4

### Patch Changes

- improvements

## 4.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/lava-queue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@discordx/lava-queue",
"version": "4.0.3",
"version": "4.0.4",
"private": false,
"description": "Queue system for @discordx/lava-player",
"keywords": [
Expand Down
16 changes: 5 additions & 11 deletions packages/lava-queue/src/queue-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ import { Collection } from "discord.js";
import { Queue } from "./queue.js";

export class QueueManager {
private _leaveOnFinish = true;
public queues = new Collection<Snowflake, Queue>();

get leaveOnFinish(): boolean {
return this._leaveOnFinish;
}

constructor(public node: Node) {
node.on("playerUpdate", (e: GetPlayer) => {
const queue = this.queues.get(e.guildId);
Expand All @@ -39,8 +34,11 @@ export class QueueManager {

void queue.playNext();

/**
* Leave the voice channel if it is enabled and there is no next track to play.
*/
if (
this.leaveOnFinish &&
queue.leaveOnFinish &&
!queue.currentPlaybackTrack &&
!queue.tracks.length
) {
Expand All @@ -50,10 +48,6 @@ export class QueueManager {
});
}

public setLeaveOnFinish(value: boolean): void {
this._leaveOnFinish = value;
}

public queue<T extends Queue = Queue>(
guildId: string,
resolver?: () => T,
Expand All @@ -65,7 +59,7 @@ export class QueueManager {
return queue;
}

const clazz = resolver ? resolver() : (new Queue(this, guildId) as T);
const clazz = resolver ? resolver() : (new Queue(this.node, guildId) as T);

// store queue
this.queues.set(guildId, clazz);
Expand Down
86 changes: 49 additions & 37 deletions packages/lava-queue/src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
*/
import {
type GuildPlayer,
type Node,
PlayerStatus,
type Track,
type TrackResponse,
} from "@discordx/lava-player";
import shuffle from "lodash/shuffle.js";

import type { QueueManager } from "./queue-manager.js";
import { RepeatMode } from "./util.js";

export class Queue {
private _tracks: Track[] = [];
private _currentPlaybackTrack: Track | null = null;
private _currentPlaybackPosition = 0;
private _repeatMode: RepeatMode = RepeatMode.OFF;
private _leaveOnFinish = true;

/**
* Gets the current track being played.
Expand All @@ -29,6 +30,13 @@ export class Queue {
return this._currentPlaybackTrack;
}

/**
* Leave voice channel when track finish
*/
get leaveOnFinish(): boolean {
return this._leaveOnFinish;
}

/**
* Gets the next track in the queue.
* @returns The next track in the queue or undefined if the queue is empty.
Expand Down Expand Up @@ -74,7 +82,7 @@ export class Queue {
* @returns The LavaPlayer instance.
*/
get guildPlayer(): GuildPlayer {
return this.queueManager.node.guildPlayerStore.get(this.guildId);
return this.node.guildPlayerStore.get(this.guildId);
}

get http() {
Expand All @@ -88,10 +96,6 @@ export class Queue {
);
}

get node() {
return this.guildPlayer.node;
}

get rest() {
return this.guildPlayer.rest;
}
Expand All @@ -101,7 +105,7 @@ export class Queue {
}

constructor(
private queueManager: QueueManager,
public node: Node,
public guildId: string,
) {
// empty constructor
Expand All @@ -115,6 +119,14 @@ export class Queue {
this._tracks.push(...tracks);
}

/**
* Adds one or more tracks to top of the queue.
* @param tracks - The tracks to be added to the queue.
*/
addTrackFirst(...tracks: Track[]) {
this._tracks.unshift(...tracks);
}

/**
* Changes the position of a track in the queue.
* @param oldIndex - The current index of the track.
Expand All @@ -137,31 +149,13 @@ export class Queue {
}

/**
* Removes tracks from the queue by their indices.
* @param indices - The indices of the tracks to be removed.
*/
removeTracks(...indices: number[]): void {
// Sort indices in descending order to avoid shifting issues while removing
indices.sort((a, b) => b - a);
for (const index of indices) {
if (index >= 0 && index < this._tracks.length) {
this._tracks.splice(index, 1);
}
}
}

/**
* Removes all tracks from the queue.
*/
removeAllTracks(): void {
this._tracks = [];
}

/**
* Shuffles the tracks in the queue.
* Exits the player, clearing the queue and destroying the LavaPlayer instance.
*/
shuffleTracks(): void {
this._tracks = shuffle(this._tracks);
async exit(): Promise<void> {
this._currentPlaybackTrack = null;
this.removeAllTracks();
await this.guildPlayer.leave();
await this.guildPlayer.destroy();
}

/**
Expand Down Expand Up @@ -197,6 +191,27 @@ export class Queue {
this.guildPlayer.status = PlayerStatus.PAUSED;
}

/**
* Removes tracks from the queue by their indices.
* @param indices - The indices of the tracks to be removed.
*/
removeTracks(...indices: number[]): void {
// Sort indices in descending order to avoid shifting issues while removing
indices.sort((a, b) => b - a);
for (const index of indices) {
if (index >= 0 && index < this._tracks.length) {
this._tracks.splice(index, 1);
}
}
}

/**
* Removes all tracks from the queue.
*/
removeAllTracks(): void {
this._tracks = [];
}

/**
* Resumes playing the current track.
*/
Expand Down Expand Up @@ -242,12 +257,9 @@ export class Queue {
}

/**
* Exits the player, clearing the queue and destroying the LavaPlayer instance.
* Shuffles the tracks in the queue.
*/
async exit(): Promise<void> {
this._currentPlaybackTrack = null;
this.removeAllTracks();
await this.guildPlayer.leave();
await this.guildPlayer.destroy();
shuffleTracks(): void {
this._tracks = shuffle(this._tracks);
}
}
10 changes: 10 additions & 0 deletions packages/lava-queue/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ export function fromMS(duration: number): string {
const hoursPad = hours.padStart(2, "0");
return `${hours ? `${hoursPad}:` : ""}${minutesPad}:${secondsPad}`;
}

export function toMS(duration: string): number {
const reducer = (prev: number, curr: string, i: number, arr: string[]) => {
return prev + parseInt(curr) * Math.pow(60, arr.length - 1 - i);
};

const seconds = duration.split(":").reduceRight(reducer, 0);
const milliseconds = seconds * 1e3;
return milliseconds;
}
6 changes: 6 additions & 0 deletions packages/music/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @discordx/music

## 6.2.0

### Minor Changes

- improved player

## 6.1.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/music/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@discordx/music",
"version": "6.1.3",
"version": "6.2.0",
"private": false,
"description": "Powerful Discord music player library using YTDL",
"keywords": [
Expand Down
5 changes: 3 additions & 2 deletions packages/music/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the Apache License. See License.txt in the project root for license information.
* -------------------------------------------------------------------------------------------------------
*/
export * from "./queue-node.js";
export * from "./track-queue.js";
export * from "./node.js";
export * from "./queue.js";
export * from "./queue-manager.js";
export * from "./types/index.js";
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
WorkerOperation,
} from "./types/index.js";

export interface QueueNode extends EventEmitter {
export interface Node extends EventEmitter {
on<T extends QueueEvent>(
event: T,
listener: (payload: QueueEventPayloads[T]) => void,
): this;
}

export class QueueNode extends EventEmitter {
export class Node extends EventEmitter {
private worker: Worker;

constructor(public client: Client) {
Expand Down Expand Up @@ -117,8 +117,8 @@ export class QueueNode extends EventEmitter {
this.sendOp({ data, op: WorkerOperation.Pause });
}

unpause(data: GuildData): void {
this.sendOp({ data, op: WorkerOperation.Unpause });
resume(data: GuildData): void {
this.sendOp({ data, op: WorkerOperation.Resume });
}

stop(data: GuildData): void {
Expand Down
Loading

0 comments on commit 5cda217

Please sign in to comment.