From 8ddfdb890bbeeb74d3c11a04e8b014d50440a22c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 4 Oct 2025 12:23:48 -0700 Subject: fix(gateway:voiceConnection): Improve error handling --- .../src/listeners/clientReady/voiceConnection.ts | 80 +++++++++++----------- 1 file changed, 39 insertions(+), 41 deletions(-) (limited to 'packages') diff --git a/packages/gateway/src/listeners/clientReady/voiceConnection.ts b/packages/gateway/src/listeners/clientReady/voiceConnection.ts index 81fc539..78a9e47 100644 --- a/packages/gateway/src/listeners/clientReady/voiceConnection.ts +++ b/packages/gateway/src/listeners/clientReady/voiceConnection.ts @@ -2,6 +2,7 @@ import { Client } from "discord.js"; import { joinVoiceChannel, VoiceConnectionStatus, + VoiceConnectionDisconnectReason, createAudioPlayer, } from "@discordjs/voice"; @@ -33,52 +34,42 @@ const createVoiceConnection = ( const player = createAudioPlayer(); connection.subscribe(player); - connection.on("error", (error) => { - console.error(`Voice connection error for guild ${guildId}:`, error); - setTimeout(() => { - createVoiceConnection(client, guildId, channelId); - }, 15000); + connection.on(VoiceConnectionStatus.Connecting, () => { + console.log(`Connecting to voice channel in guild ${guildId}...`); }); - connection.on(VoiceConnectionStatus.Disconnected, () => { - setTimeout(() => { - try { - const newConnection = joinVoiceChannel({ - channelId: voiceChannel.id, - guildId: voiceChannel.guildId!, - adapterCreator: voiceChannel.guild.voiceAdapterCreator, - }); - const newPlayer = createAudioPlayer(); - - newConnection.subscribe(newPlayer); - newConnection.on("error", (error) => { - console.error( - `Voice reconnection error for guild ${guildId}:`, - error, - ); - setTimeout(() => { - createVoiceConnection(client, guildId, channelId); - }, 15000); - }); - newConnection.on(VoiceConnectionStatus.Disconnected, () => { - setTimeout(() => { - createVoiceConnection(client, guildId, channelId); - }, 5000); - }); - } catch (error) { - console.error( - `Failed to reconnect to voice channel for guild ${guildId}:`, - error, - ); - setTimeout(() => { - createVoiceConnection(client, guildId, channelId); - }, 10000); - } - }, 5000); + connection.on(VoiceConnectionStatus.Ready, () => { + console.log(`Successfully connected to voice channel in guild ${guildId}`); + }); + connection.on(VoiceConnectionStatus.Disconnected, (oldState, newState) => { + console.log(`Voice disconnected in guild ${guildId}: ${oldState.status} -> ${newState.status}`); + + if (newState.reason !== VoiceConnectionDisconnectReason.Manual) + setTimeout(() => { + console.log(`Attempting to reconnect voice in guild ${guildId}...`); + createVoiceConnection(client, guildId, channelId); + }, 5000); }); connection.on(VoiceConnectionStatus.Destroyed, () => { + console.log(`Voice connection destroyed in guild ${guildId}, attempting to reconnect...`); setTimeout(() => { createVoiceConnection(client, guildId, channelId); - }, 5000); + }, 10000); + }); + connection.on("error", (error) => { + console.error(`Voice connection error for guild ${guildId}:`, error); + + const errorCode = (error as any).code; + + if (errorCode === 'ENOTFOUND' || errorCode === 'UND_ERR_CONNECT_TIMEOUT') { + console.log(`Network error for guild ${guildId}, retrying in 30 seconds...`); + setTimeout(() => { + createVoiceConnection(client, guildId, channelId); + }, 30000); + } else { + setTimeout(() => { + createVoiceConnection(client, guildId, channelId); + }, 15000); + } }); } catch (error) { console.error( @@ -92,6 +83,13 @@ const createVoiceConnection = ( }; export const handleVoiceConnection = (client: Client) => { + process.on('uncaughtException', (error) => { + console.error('Uncaught Exception in voice connection:', error); + }); + process.on('unhandledRejection', (reason, promise) => { + console.error('Unhandled Rejection in voice connection:', reason); + }); + for (const [guildId, channelId] of Object.entries(GUILD_VOICE_CHANNELS)) createVoiceConnection(client, guildId, channelId); }; -- cgit v1.2.3