diff --git a/commands/leave.mjs b/commands/leave.mjs index b754573..8f1356b 100644 --- a/commands/leave.mjs +++ b/commands/leave.mjs @@ -23,13 +23,6 @@ function cleanId(value) { return String(value ?? "").replace(/\D/g, ""); } -/** - * Resolve the guild ID from a player entry. - * Uses _guildId if set (populated after join() completes), otherwise falls - * back to the client channel cache. This is necessary because in the - * multi-voice scenario _guildId can still be null for a freshly-joined player, - * which caused guildPlayers to be empty and leave to report "not in voice". - */ function resolvePlayerGuildId(player, mapKey, client) { const direct = cleanId(player?._guildId); if (direct) return direct; @@ -116,88 +109,42 @@ export async function run(msg, data) { ? new Set(raw.map(id => cleanId(id)).filter(Boolean)) : new Set([cleanId(raw)]); - const other247AutoChannels = [...ch247].filter(id => { - if (id === activeChannelId || id === homeChannelId || id === targetChannelId) return false; - const mode = get247ChannelMode(set, id); - return mode === "auto"; - }); + const is247 = ch247.has(activeChannelId) || ch247.has(homeChannelId); - if (ch247.has(activeChannelId) || ch247.has(homeChannelId)) { + if (is247) { const matchChannel = ch247.has(homeChannelId) ? homeChannelId : ch247.has(activeChannelId) ? activeChannelId : null; const mode = matchChannel ? get247ChannelMode(set, matchChannel) : "off"; + const prefix = this.handler.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id); + this.markIntentionalLeave(activeChannelId); this.players.playerMap.delete(activeChannelId); + this.players._unindexPlayer(player._guildId, activeChannelId); if (activeChannelId !== targetChannelId) this.players.playerMap.delete(targetChannelId); if (homeChannelId !== activeChannelId) this.players.playerMap.delete(homeChannelId); + + const pendingScrobble = this.players._pendingScrobbleTimers?.get(activeChannelId); + if (pendingScrobble) { + clearTimeout(pendingScrobble.timer); + this.players._pendingScrobbleTimers.delete(activeChannelId); + } + await player.leave().catch(() => {}); player.destroy(); if (mode === "auto") { - msg.reply(embed(this.t(msg, "responses.leave.leftRejoin247", { channel: targetChannelId, prefix: this.handler.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id) }))); - const leave247Delay = this.config?.timers?.leave247RejoinDelay ?? 5000; + msg.reply(embed(this.t(msg, "responses.leave.leftRejoin247", { channel: targetChannelId, prefix }))); + const rejoinDelay = this.config?.timers?.leave247RejoinDelay ?? 5000; setTimeout(() => { if (this._spawnPlayer) { this._spawnPlayer(guildId, homeChannelId).catch(e => logger.warn("[leave] 247 rejoin failed for", homeChannelId, e.message) ); } - }, leave247Delay); - - if (other247AutoChannels.length > 0) { - const rejoinBaseDelay = leave247Delay; - const rejoinStagger = this.config?.timers?.rejoin247Delay ?? 3000; - for (let i = 0; i < other247AutoChannels.length; i++) { - const otherChannelId = other247AutoChannels[i]; - const otherPlayer = this.players.playerMap.get(otherChannelId); - if (!otherPlayer || otherPlayer._destroyed) { - if (otherPlayer) { - this.players.playerMap.delete(otherChannelId); - try { otherPlayer.destroy(); } catch (_) {} - } - const delay = rejoinBaseDelay + (i + 1) * rejoinStagger; - logger.recovery( - `[leave] Scheduling 247 auto-rejoin for collateral-disconnected channel ${otherChannelId} in ${delay}ms` - ); - setTimeout(() => { - if (this._spawnPlayer) { - this._spawnPlayer(guildId, otherChannelId).catch(e => - logger.warn("[leave] 247 collateral rejoin failed for", otherChannelId, e.message) - ); - } - }, delay); - } - } - } + }, rejoinDelay); } else { - msg.reply(embed(this.t(msg, "responses.leave.left247On", { prefix: this.handler.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id) }))); - - if (other247AutoChannels.length > 0) { - const rejoinDelay = this.config?.timers?.leave247RejoinDelay ?? 5000; - const rejoinStagger = this.config?.timers?.rejoin247Delay ?? 3000; - for (let i = 0; i < other247AutoChannels.length; i++) { - const otherChannelId = other247AutoChannels[i]; - const otherPlayer = this.players.playerMap.get(otherChannelId); - if (!otherPlayer || otherPlayer._destroyed) { - if (otherPlayer) { - this.players.playerMap.delete(otherChannelId); - try { otherPlayer.destroy(); } catch (_) {} - } - const delay = rejoinDelay + (i + 1) * rejoinStagger; - logger.recovery( - `[leave] Scheduling 247 auto-rejoin for collateral-disconnected channel ${otherChannelId} in ${delay}ms` - ); - setTimeout(() => { - if (this._spawnPlayer) { - this._spawnPlayer(guildId, otherChannelId).catch(e => - logger.warn("[leave] 247 collateral rejoin failed for", otherChannelId, e.message) - ); - } - }, delay); - } - } - } + msg.reply(embed(this.t(msg, "responses.leave.left247On", { prefix }))); } } else { await this.leaveChannel(activeChannelId, guildId, msg); diff --git a/src/constants/FluxerRevoice.mjs b/src/constants/FluxerRevoice.mjs index edb2702..593270e 100644 --- a/src/constants/FluxerRevoice.mjs +++ b/src/constants/FluxerRevoice.mjs @@ -270,63 +270,41 @@ export class FluxerRevoice extends EventEmitter { } /** - * Send a VOICE_STATE_UPDATE leave signal to the Fluxer gateway so it - * knows the bot is no longer in the given channel. Without this, the + * Send a channel-specific leave signal to the Fluxer gateway so it + * knows the bot is no longer in the given channel. Without this, the * gateway may think the bot is still connected and won't send a fresh * VOICE_SERVER_UPDATE on the next joinVoiceChannel() call, causing the * rejoin to hang or fail. * - * IMPORTANT: We try channel-specific leave (leaveChannel) FIRST because - * vm.leave(guildId) disconnects ALL voice channels in that guild, which - * is catastrophic for multi-voice setups where the bot is in multiple - * channels with 24/7 enabled. Only fall back to guild-level leave if - * the channel-specific API is unavailable. + * IMPORTANT: Only uses channel-specific leave (vm.leaveChannel). + * Guild-level leave (vm.leave / vm.updateVoiceState with null channel) + * disconnects ALL voice channels in that guild, which is catastrophic + * for multi-voice setups where the bot is in multiple channels with + * 24/7 enabled. If vm.leaveChannel is unavailable, the gateway leave + * is skipped entirely rather than risk disconnecting other channels. * * @param {string} channelId — The channel ID to leave - * @param {string} [guildId] — The guild ID (for fallback) + * @param {string} [guildId] — Unused, kept for API compatibility */ _leaveGateway(channelId, guildId = null) { try { const vm = getVoiceManager(this.client); if (!vm) return; - const guildForLeave = guildId ?? this._resolveGuildForChannel(channelId); - if (typeof vm.leaveChannel === "function") { try { vm.leaveChannel(channelId); logger.player(`[FluxerRevoice] Sent channel-specific gateway leave via vm.leaveChannel() for channel ${channelId}`); return; } catch (e) { - logger.warn(`[FluxerRevoice] vm.leaveChannel() failed for channel ${channelId}: ${e?.message} — falling back`); + logger.warn(`[FluxerRevoice] vm.leaveChannel() failed for channel ${channelId}: ${e?.message}`); } } - if (guildForLeave && typeof vm.leave === "function") { - try { - vm.leave(guildForLeave); - logger.player(`[FluxerRevoice] Sent guild-level gateway leave via vm.leave() for guild ${guildForLeave} (leaveChannel unavailable — other channels may be disconnected)`); - return; - } catch (e) { - logger.warn(`[FluxerRevoice] vm.leave() failed for guild ${guildForLeave}: ${e?.message}`); - } - } - - if (typeof vm.updateVoiceState === "function") { - if (guildForLeave) { - try { - vm.updateVoiceState(guildForLeave, null, { self_deaf: false, self_mute: false }); - logger.player(`[FluxerRevoice] Sent gateway leave via updateVoiceState() for guild ${guildForLeave}`); - return; - } catch (e1) { - try { vm.updateVoiceState(guildForLeave, null); return; } catch (e2) {} - } - } - logger.warn( - `[FluxerRevoice] Cannot send guild-scoped leave for channel ${channelId} — ` + - `skipping leave signal to avoid disconnecting all guilds.` - ); - } + logger.warn( + `[FluxerRevoice] No channel-specific leave API available for channel ${channelId}. ` + + `Skipping gateway leave to avoid disconnecting other voice channels in the guild.` + ); } catch (e) { logger.warn(`[FluxerRevoice] Gateway leave signal failed for ${channelId}: ${e?.message}`); }