diff --git a/README.md b/README.md index f97ed0a..52cba10 100644 --- a/README.md +++ b/README.md @@ -789,3 +789,11 @@ if(previousTrack) { *Allows you to inmplement a custom playerVoiceEmpty handler* - Added the new events and configuration to the docs + +## **Version 2.4.3** +- `managerOptions#playerOptions.onDisconnect.autoReconnect`: + - Added the option `managerOptions#playerOptions.onDisconnect.autoReconnectOnlyWithTracks` to control wether to try reconnecting only when there are tracks in the queue / current track or not + - Added a new debug log for that + - Added the try to play the next track if there is no current track + - *There was a problem trying to auto-reconnect on "empty-queue" events, which caused the player to get destroyed by that and log the error in console "`There is no Track in the Queue, nor provided in the PlayOptions`"* + - *Now you have to handle that case manually if you want to or set autoReconnectOnlyWithTracks to false (default)* diff --git a/package.json b/package.json index ca971ba..09e425c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lavalink-client", - "version": "2.4.2", + "version": "2.4.3", "description": "Easy, flexible and feature-rich lavalink@v4 Client. Both for Beginners and Proficients.", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", diff --git a/src/structures/LavalinkManager.ts b/src/structures/LavalinkManager.ts index a8fbbe2..98d754d 100644 --- a/src/structures/LavalinkManager.ts +++ b/src/structures/LavalinkManager.ts @@ -95,7 +95,8 @@ export class LavalinkManager extends EventEmitter { defaultSearchPlatform: options?.playerOptions?.defaultSearchPlatform ?? "ytsearch", onDisconnect: { destroyPlayer: options?.playerOptions?.onDisconnect?.destroyPlayer ?? true, - autoReconnect: options?.playerOptions?.onDisconnect?.autoReconnect ?? false + autoReconnect: options?.playerOptions?.onDisconnect?.autoReconnect ?? false, + autoReconnectOnlyWithTracks: options?.playerOptions?.onDisconnect?.autoReconnectOnlyWithTracks ?? false, }, onEmptyQueue: { autoPlayFunction: options?.playerOptions?.onEmptyQueue?.autoPlayFunction ?? null, @@ -590,15 +591,21 @@ export class LavalinkManager extends EventEmitter { if(suppressChange) this.emit("playerSuppressChange", player, player.voiceState.suppress); } else { - if (this.options?.playerOptions?.onDisconnect?.destroyPlayer === true) { + + const { + autoReconnectOnlyWithTracks, + destroyPlayer, + autoReconnect + } = this.options?.playerOptions?.onDisconnect ?? {}; + + if (destroyPlayer === true) { return void await player.destroy(DestroyReasons.Disconnected); } - this.emit("playerDisconnect", player, player.voiceChannelId); - - if (this.options?.playerOptions?.onDisconnect?.autoReconnect === true) { + if (autoReconnect === true) { try { - const positionPrevios = player.position; + const previousPosition = player.position; + const previousPaused = player.paused; if (this.options?.advancedOptions?.enableDebugEvents) { this.emit("debug", DebugEvents.PlayerAutoReconnect, { @@ -608,12 +615,23 @@ export class LavalinkManager extends EventEmitter { }); } - await player.connect(); + // connect if there are tracks & autoReconnectOnlyWithTracks = true or autoReconnectOnlyWithTracks is false + if(!autoReconnectOnlyWithTracks || (autoReconnectOnlyWithTracks && (player.queue.current || player.queue.tracks.length))) { + await player.connect(); + } // replay the current playing stream - await player.play({ - position: positionPrevios, - paused: player.paused, - clientTrack: player.queue.current, + if(player.queue.current) { + return void await player.play({ position: previousPosition, paused: previousPaused, clientTrack: player.queue.current, }); + } + // try to play the next track + if(player.queue.tracks.length) { + return void await player.play({ paused: previousPaused }); + } + // debug log if nothing was possible + this.emit("debug", DebugEvents.PlayerAutoReconnect, { + state: "log", + message: `Auto reconnected, but nothing to play`, + functionLayer: "LavalinkManager > sendRawData()", }); } catch (e) { console.error(e); @@ -621,6 +639,8 @@ export class LavalinkManager extends EventEmitter { } } + this.emit("playerDisconnect", player, player.voiceChannelId); + player.voiceChannelId = null; player.voice = Object.assign({}); return diff --git a/src/structures/Types/Manager.ts b/src/structures/Types/Manager.ts index c7ee133..a44b0e6 100644 --- a/src/structures/Types/Manager.ts +++ b/src/structures/Types/Manager.ts @@ -215,6 +215,8 @@ export interface ManagerPlayerOptions { onDisconnect?: { /** Try to reconnect? -> If fails -> Destroy */ autoReconnect?: boolean; + /** Only try to reconnect if there are tracks in the queue */ + autoReconnectOnlyWithTracks?: boolean; /** Instantly destroy player (overrides autoReconnect) | Don't provide == disable feature*/ destroyPlayer?: boolean; };