From 768b537946da0abd89949648b803d625542c8a7d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 21 Mar 2026 01:41:17 +0100 Subject: qol ceda --- src/handlers/playCommand.js | 55 +++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 14 deletions(-) (limited to 'src/handlers/playCommand.js') 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; } -- cgit v1.2.3