fix: leave command no longer disconnects all voice channels in guild

This commit is contained in:
NoLogicAlan committed 2026-06-04 18:21:50 +03:00
1 parent a303d7782a
commit 0d3035b093
2 files changed
+27 -102

No files matched your search

+16 -69
View File
@@ -23,13 +23,6 @@ function cleanId(value) {
return String(value ?? "").replace(/\D/g, ""); 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) { function resolvePlayerGuildId(player, mapKey, client) {
const direct = cleanId(player?._guildId); const direct = cleanId(player?._guildId);
if (direct) return direct; 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(raw.map(id => cleanId(id)).filter(Boolean))
: new Set([cleanId(raw)]); : new Set([cleanId(raw)]);
const other247AutoChannels = [...ch247].filter(id => { const is247 = ch247.has(activeChannelId) || ch247.has(homeChannelId);
if (id === activeChannelId || id === homeChannelId || id === targetChannelId) return false;
const mode = get247ChannelMode(set, id);
return mode === "auto";
});
if (ch247.has(activeChannelId) || ch247.has(homeChannelId)) { if (is247) {
const matchChannel = ch247.has(homeChannelId) ? homeChannelId const matchChannel = ch247.has(homeChannelId) ? homeChannelId
: ch247.has(activeChannelId) ? activeChannelId : ch247.has(activeChannelId) ? activeChannelId
: null; : null;
const mode = matchChannel ? get247ChannelMode(set, matchChannel) : "off"; const mode = matchChannel ? get247ChannelMode(set, matchChannel) : "off";
const prefix = this.handler.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id);
this.markIntentionalLeave(activeChannelId); this.markIntentionalLeave(activeChannelId);
this.players.playerMap.delete(activeChannelId); this.players.playerMap.delete(activeChannelId);
this.players._unindexPlayer(player._guildId, activeChannelId);
if (activeChannelId !== targetChannelId) this.players.playerMap.delete(targetChannelId); if (activeChannelId !== targetChannelId) this.players.playerMap.delete(targetChannelId);
if (homeChannelId !== activeChannelId) this.players.playerMap.delete(homeChannelId); 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(() => {}); await player.leave().catch(() => {});
player.destroy(); player.destroy();
if (mode === "auto") { 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) }))); msg.reply(embed(this.t(msg, "responses.leave.leftRejoin247", { channel: targetChannelId, prefix })));
const leave247Delay = this.config?.timers?.leave247RejoinDelay ?? 5000; const rejoinDelay = this.config?.timers?.leave247RejoinDelay ?? 5000;
setTimeout(() => { setTimeout(() => {
if (this._spawnPlayer) { if (this._spawnPlayer) {
this._spawnPlayer(guildId, homeChannelId).catch(e => this._spawnPlayer(guildId, homeChannelId).catch(e =>
logger.warn("[leave] 247 rejoin failed for", homeChannelId, e.message) logger.warn("[leave] 247 rejoin failed for", homeChannelId, e.message)
); );
} }
}, leave247Delay); }, rejoinDelay);
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);
}
}
}
} else { } else {
msg.reply(embed(this.t(msg, "responses.leave.left247On", { prefix: this.handler.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id) }))); msg.reply(embed(this.t(msg, "responses.leave.left247On", { prefix })));
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);
}
}
}
} }
} else { } else {
await this.leaveChannel(activeChannelId, guildId, msg); await this.leaveChannel(activeChannelId, guildId, msg);
+11 -33
View File
@@ -270,63 +270,41 @@ export class FluxerRevoice extends EventEmitter {
} }
/** /**
* Send a VOICE_STATE_UPDATE leave signal to the Fluxer gateway so it * 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 * 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 * gateway may think the bot is still connected and won't send a fresh
* VOICE_SERVER_UPDATE on the next joinVoiceChannel() call, causing the * VOICE_SERVER_UPDATE on the next joinVoiceChannel() call, causing the
* rejoin to hang or fail. * rejoin to hang or fail.
* *
* IMPORTANT: We try channel-specific leave (leaveChannel) FIRST because * IMPORTANT: Only uses channel-specific leave (vm.leaveChannel).
* vm.leave(guildId) disconnects ALL voice channels in that guild, which * Guild-level leave (vm.leave / vm.updateVoiceState with null channel)
* is catastrophic for multi-voice setups where the bot is in multiple * disconnects ALL voice channels in that guild, which is catastrophic
* channels with 24/7 enabled. Only fall back to guild-level leave if * for multi-voice setups where the bot is in multiple channels with
* the channel-specific API is unavailable. * 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} 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) { _leaveGateway(channelId, guildId = null) {
try { try {
const vm = getVoiceManager(this.client); const vm = getVoiceManager(this.client);
if (!vm) return; if (!vm) return;
const guildForLeave = guildId ?? this._resolveGuildForChannel(channelId);
if (typeof vm.leaveChannel === "function") { if (typeof vm.leaveChannel === "function") {
try { try {
vm.leaveChannel(channelId); vm.leaveChannel(channelId);
logger.player(`[FluxerRevoice] Sent channel-specific gateway leave via vm.leaveChannel() for channel ${channelId}`); logger.player(`[FluxerRevoice] Sent channel-specific gateway leave via vm.leaveChannel() for channel ${channelId}`);
return; return;
} catch (e) { } 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( logger.warn(
`[FluxerRevoice] Cannot send guild-scoped leave for channel ${channelId} — ` + `[FluxerRevoice] No channel-specific leave API available for channel ${channelId}. ` +
`skipping leave signal to avoid disconnecting all guilds.` `Skipping gateway leave to avoid disconnecting other voice channels in the guild.`
); );
}
} catch (e) { } catch (e) {
logger.warn(`[FluxerRevoice] Gateway leave signal failed for ${channelId}: ${e?.message}`); logger.warn(`[FluxerRevoice] Gateway leave signal failed for ${channelId}: ${e?.message}`);
} }