summaryrefslogtreecommitdiff
path: root/packages/gateway
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gateway')
-rw-r--r--packages/gateway/src/listeners/clientReady/voiceConnection.ts80
1 files changed, 39 insertions, 41 deletions
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);
};