Skip to content

Commit

Permalink
v2.4.3 - Add more autoReconnect functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomato6966 committed Dec 12, 2024
1 parent 2bb313c commit 51845d8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
42 changes: 31 additions & 11 deletions src/structures/LavalinkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, {
Expand All @@ -608,19 +615,32 @@ 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);
return void await player.destroy(DestroyReasons.PlayerReconnectFail);
}
}

this.emit("playerDisconnect", player, player.voiceChannelId);

player.voiceChannelId = null;
player.voice = Object.assign({});
return
Expand Down
2 changes: 2 additions & 0 deletions src/structures/Types/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 51845d8

Please sign in to comment.