diff options
Diffstat (limited to 'src/handlers')
| -rw-r--r-- | src/handlers/controlCommands.js | 4 | ||||
| -rw-r--r-- | src/handlers/playCommand.js | 55 |
2 files changed, 43 insertions, 16 deletions
diff --git a/src/handlers/controlCommands.js b/src/handlers/controlCommands.js index 14c73f7..42de36e 100644 --- a/src/handlers/controlCommands.js +++ b/src/handlers/controlCommands.js @@ -1,4 +1,4 @@ -const { playSong, safeCleanup, clearSongBuffer } = require('../utils/player'); +const { safeCleanup, clearSongBuffer } = require('../utils/player'); const { getQueueOrReply } = require('../utils/helpers'); function handlePause(interaction, queues) { @@ -80,7 +80,7 @@ function handleQuit(interaction, queues) { const queue = getQueueOrReply(interaction, queues); if (!queue) return; - try { queue.player.stop(); } catch (e) {} + try { queue.player.stop(); } catch (_e) {} safeCleanup(queue, 'Quit'); queue.connection.destroy(); queues.delete(interaction.guild.id); diff --git a/src/handlers/playCommand.js b/src/handlers/playCommand.js index 5551d28..c61e009 100644 --- a/src/handlers/playCommand.js +++ b/src/handlers/playCommand.js @@ -2,6 +2,7 @@ const { EmbedBuilder } = require('discord.js'); const { createAudioPlayer, joinVoiceChannel, + getVoiceConnection, AudioPlayerStatus, VoiceConnectionStatus, entersState, @@ -87,7 +88,7 @@ async function onConnectionDisconnected(connection, queues, guildId) { entersState(connection, VoiceConnectionStatus.Signalling, 5000), entersState(connection, VoiceConnectionStatus.Connecting, 5000), ]); - } catch (error) { + } catch (_error) { connection.destroy(); queues.delete(guildId); } @@ -114,20 +115,46 @@ function setupConnectionEventListeners(connection, queues, guildId, queue) { } async function createVoiceConnection(voiceChannel, interaction) { - const connection = joinVoiceChannel({ - channelId: voiceChannel.id, - guildId: interaction.guild.id, - adapterCreator: voiceChannel.guild.voiceAdapterCreator, - }); + const guildId = interaction.guild.id; + const existingConnection = getVoiceConnection(guildId); + if (existingConnection) { + try { + existingConnection.destroy(); + } catch (e) { + console.warn('Failed to destroy existing voice connection before reconnect:', e); + } + } - try { - await entersState(connection, VoiceConnectionStatus.Ready, 30000); - return connection; - } catch (error) { - console.error('Failed to join voice channel:', error); - connection.destroy(); - throw error; + let lastError; + const maxAttempts = 2; + + for (let attempt = 1; attempt <= maxAttempts; attempt++) { + const connection = joinVoiceChannel({ + channelId: voiceChannel.id, + guildId, + adapterCreator: voiceChannel.guild.voiceAdapterCreator, + selfDeaf: true, + }); + + try { + await entersState(connection, VoiceConnectionStatus.Ready, 30000); + return connection; + } catch (error) { + lastError = error; + console.error(`Failed to join voice channel (attempt ${attempt}/${maxAttempts}):`, error); + try { + connection.destroy(); + } catch (e) { + console.warn('Failed to destroy connection after join failure:', e); + } + + if (attempt < maxAttempts) { + await new Promise((resolve) => setTimeout(resolve, 1000)); + } + } } + + throw lastError; } async function initializeQueue(voiceChannel, interaction, queues, songs) { @@ -136,7 +163,7 @@ async function initializeQueue(voiceChannel, interaction, queues, songs) { try { connection = await createVoiceConnection(voiceChannel, interaction); - } catch (error) { + } catch (_error) { return null; } |
