From 42348545a599667c120914f937f31cdfc576e143 Mon Sep 17 00:00:00 2001 From: FAYStarNext Date: Sun, 8 Sep 2024 11:51:39 +0000 Subject: [PATCH] Refactor import statements in Utils.ts and test files --- example/src/index.ts | 3 -- src/Structures/Manager.ts | 5 +++- src/Structures/Player.ts | 63 +++++++++++++++++++++++++++++++++++++++ src/Structures/Utils.ts | 2 +- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/example/src/index.ts b/example/src/index.ts index 955f93b..485f84b 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -90,9 +90,6 @@ client.on("messageCreate", async (message) => { await handlePlayCommand(message, args); } }); -manager.on("SearchCacheClear" , (data) => { - console.log(`Cache cleared: ${data}`); -}); client.on("raw", (data) => manager.updateVoiceState(data)); client.on("ready" , () => { manager.init(client.user?.id as string); diff --git a/src/Structures/Manager.ts b/src/Structures/Manager.ts index 30ab9c1..f29592e 100644 --- a/src/Structures/Manager.ts +++ b/src/Structures/Manager.ts @@ -47,7 +47,7 @@ export class Manager extends TypedEmitter { public caches = new Collection(); /** Returns the nodes that has the least load. */ - private get leastLoadNode(): Collection { + public get leastLoadNode(): Collection { return this.nodes .filter((node) => node.connected) .sort((a, b) => { @@ -138,6 +138,9 @@ export class Manager extends TypedEmitter { this.nodes.set(node.options.identifier, node); } } + setInterval(() => { + this.caches.clear(); + }, this.options.caches.time); } /** diff --git a/src/Structures/Player.ts b/src/Structures/Player.ts index ed04415..bcb012c 100644 --- a/src/Structures/Player.ts +++ b/src/Structures/Player.ts @@ -135,6 +135,69 @@ export class Player { return this; } + /** + * Moves the player to a different node. + * + * @param {string} [node] - The ID of the node to move to. + * @returns {this} - The player instance. + */ + public async moveNode(node?: string): Promise { + node = node || this.manager.leastLoadNode.first().options.identifier || this.manager.nodes.filter((n) => n.connected).first().options.identifier; + if (!this.manager.nodes.has(node)) throw new RangeError("No nodes available."); + if (this.node.options.identifier === node) return this; + + const destroyOldNode = async (node: Node) => { + this.state = "MOVING"; + + if (this.manager.nodes.get(node.options.identifier) && this.manager.nodes.get(node.options.identifier).connected) await node.rest.destroyPlayer(this.guild); + + setTimeout(() => (this.state = "CONNECTED"), 5000); + }; + + const currentNode = this.node; + const destinationNode = this.manager.nodes.get(node); + let position = this.position; + + if (currentNode.connected) { + const fetchedPlayer: any = await currentNode.rest.get(`/v4/sessions/${currentNode.sessionId}/players/${this.guild}`); + position = fetchedPlayer.track.info.position; + } + + await destinationNode.rest.updatePlayer({ + guildId: this.guild, + data: { + encodedTrack: this.queue.current?.track, + position: position, + volume: this.volume, + paused: this.paused, + filters: { + distortion: this.filters.distortion, + equalizer: this.filters.equalizer, + karaoke: this.filters.karaoke, + rotation: this.filters.rotation, + timescale: this.filters.timescale, + vibrato: this.filters.vibrato, + volume: this.filters.volume, + }, + }, + }); + + await destinationNode.rest.updatePlayer({ + guildId: this.guild, + data: { + voice: { + token: this.voiceState.event.token, + endpoint: this.voiceState.event.endpoint, + sessionId: this!.voiceState?.sessionId!, + }, + }, + }); + + this.node = destinationNode; + destroyOldNode(currentNode); + return this; + } + /** Disconnect from the voice channel. */ public disconnect(): this { if (this.voiceChannel === null) return this; diff --git a/src/Structures/Utils.ts b/src/Structures/Utils.ts index 174e1b1..fccf8d5 100644 --- a/src/Structures/Utils.ts +++ b/src/Structures/Utils.ts @@ -226,7 +226,7 @@ export type Sizes = "0" | "1" | "2" | "3" | "default" | "mqdefault" | "hqdefault export type LoadType = "track" | "playlist" | "search" | "empty" | "error"; -export type State = "CONNECTED" | "CONNECTING" | "DISCONNECTED" | "DISCONNECTING" | "DESTROYING"; +export type State = "CONNECTED" | "CONNECTING" | "DISCONNECTED" | "DISCONNECTING" | "DESTROYING" | "MOVING"; export type PlayerEvents = TrackStartEvent | TrackEndEvent | TrackStuckEvent | TrackExceptionEvent | WebSocketClosedEvent;