From 1b5579fdde668847f6aa67783e9cf8f29efb5810 Mon Sep 17 00:00:00 2001 From: jaredthomason Date: Sat, 11 May 2024 16:39:06 -0400 Subject: [PATCH] Add automatic reconnection logic to voice connection --- src/bot.js | 17 +++++++++++++++++ src/config.json | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/bot.js b/src/bot.js index 7d78539..fba24f1 100644 --- a/src/bot.js +++ b/src/bot.js @@ -62,6 +62,23 @@ function JoinChannel(channel, track, volume) { adapterCreator: channel.guild.voiceAdapterCreator, selfDeaf: true }); + + connection.on(dVC.VoiceConnectionStatus.Disconnected, async () => { + let attemptReconnect = true; + while (attemptReconnect) { + try { + console.log("Disconnected. Attempting to reconnect..."); + await Promise.race([ + entersState(connection, dVC.VoiceConnectionStatus.Signalling, 5_000), + entersState(connection, dVC.VoiceConnectionStatus.Connecting, 5_000), + ]); + console.log("Reconnected successfully."); + attemptReconnect = false; + } catch (error) { + console.log("Reconnection attempt failed. Retrying..."); + } + } +}); const resource = dVC.createAudioResource(track, {inlineVolume: true, silencePaddingFrames: 5}); const player = dVC.createAudioPlayer(); diff --git a/src/config.json b/src/config.json index ff3d4ae..117b8f4 100644 --- a/src/config.json +++ b/src/config.json @@ -1,3 +1,3 @@ { - "token": "Insert-token-here" + "token": "Insert-Token-Here" } \ No newline at end of file