diff --git a/index.mjs b/index.mjs index 0d702c0..df3a142 100644 --- a/index.mjs +++ b/index.mjs @@ -301,6 +301,7 @@ export class Remix { }); this.revoice = FluxerRevoice.getInstance(client); + client._remix = this; this.playerContext = { client: this.client, diff --git a/src/GatewayHandler.mjs b/src/GatewayHandler.mjs index c93ec0f..e93c45b 100644 --- a/src/GatewayHandler.mjs +++ b/src/GatewayHandler.mjs @@ -1105,6 +1105,10 @@ export class GatewayHandler { logger.voiceState(`[VoiceState] Bot disconnected from ${cleanOld} — intentional leave.`); } else if (remix.revoice?._intentionalDisconnects?.has?.(cleanOld)) { logger.voiceState(`[VoiceState] Bot disconnected from ${cleanOld} — intentional (revoice).`); + } else if (this._isGuildMoveInProgress(cleanGuild, cleanOld)) { + logger.voiceState(`[VoiceState] Bot disconnected from ${cleanOld} — guild move in progress, skipping.`); + } else if (this._bootRecoveryActive) { + logger.voiceState(`[VoiceState] Bot disconnected from ${cleanOld} — boot recovery active, skipping.`); } else { const set = remix.settingsMgr.getServer(guildId); const mode = set ? get247ChannelMode(set, cleanOld) : "off"; @@ -1155,6 +1159,9 @@ export class GatewayHandler { /** @type {Map} Channel IDs with retry attempt counts */ _rejoinAttempts = new Map(); + /** @type {boolean} Whether boot recovery (rejoin247Channels) is currently running */ + _bootRecoveryActive = false; + /** @type {number} Maximum number of rejoin retry attempts */ static MAX_REJOIN_RETRIES = 3; @@ -1245,6 +1252,12 @@ export class GatewayHandler { return; } + if (remix.revoice?._intentionalDisconnects?.has?.(cleanChannelId)) { + logger.voice247(`[Rejoin] Channel ${cleanChannelId} was intentionally left (revoice) — skipping.`); + this._rejoinAttempts.delete(cleanChannelId); + return; + } + const channel = remix.client?.channels?.get?.(cleanChannelId); if (!channel) { logger.voice247(`[Rejoin] Channel ${cleanChannelId} no longer exists — skipping.`); @@ -1272,6 +1285,48 @@ export class GatewayHandler { } } + /** + * Check whether a guild-move operation is currently in progress for the + * given guild and old channel. During a move, Fluxer sends two + * VoiceStateUpdate events (leave old + join new) in rapid succession; + * the "leave" should NOT be treated as an unexpected disconnect. + * + * @param {string} guildId The guild ID to check + * @param {string} oldChannelId The channel the bot appears to have left + * @returns {boolean} True if a move is in progress and the disconnect should be ignored + */ + _isGuildMoveInProgress(guildId, oldChannelId) { + const { remix } = this; + const cleanGuild = cleanId(guildId); + const cleanOld = cleanId(oldChannelId); + if (!cleanGuild || !cleanOld) return false; + + const activePlayers = [...remix.players.playerMap.values()].filter( + p => !p._destroyed && cleanId(p._guildId ?? "") === cleanGuild + ); + + for (const player of activePlayers) { + const playerChannel = cleanId(player._channelId ?? ""); + if (playerChannel && playerChannel !== cleanOld) { + return true; + } + } + + if (remix.players._pendingJoins) { + for (const pendingId of remix.players._pendingJoins) { + const cleanPending = cleanId(pendingId); + if (cleanPending && cleanPending !== cleanOld) { + const pendingChannel = remix.client?.channels?.get?.(cleanPending); + if (pendingChannel && cleanId(pendingChannel.guildId ?? "") === cleanGuild) { + return true; + } + } + } + } + + return false; + } + /** * Invoke after the bot has connected and Moonlink has been initialised. * Seeds voice states, attaches raw WS listener, kicks off presence rotation, @@ -1413,6 +1468,7 @@ export class GatewayHandler { * avoid overwhelming the LiveKit server with concurrent track publications. */ async rejoin247Channels() { + this._bootRecoveryActive = true; const { remix } = this; const channelsToRejoin = []; @@ -1544,6 +1600,8 @@ export class GatewayHandler { logger.voice247( `[BootRecovery] Boot recovery complete. ${channelsToRejoin.length} channel(s) processed.` ); + + setTimeout(() => { this._bootRecoveryActive = false; }, 15_000); } /** diff --git a/src/Player.mjs b/src/Player.mjs index c54e7a8..8fa9499 100644 --- a/src/Player.mjs +++ b/src/Player.mjs @@ -1195,6 +1195,12 @@ export default class Player extends EventEmitter { } const channelId = this._channelId; + if (this._revoice && channelId && typeof this._revoice.markIntentionalDisconnect === "function") { + this._revoice.markIntentionalDisconnect(channelId); + } + + await this.connection.leave(); + if (this._revoice && channelId) { try { if (typeof this._revoice.deleteConnection === "function") { @@ -1205,8 +1211,6 @@ export default class Player extends EventEmitter { } catch(e) { logger.warn("[Player] Delete connection on leave error:", e?.message); } } - await this.connection.leave(); - this.queue.reset(); this.connection = null; this._paused = false; diff --git a/src/constants/FluxerRevoice.mjs b/src/constants/FluxerRevoice.mjs index 147ec8b..f2710a3 100644 --- a/src/constants/FluxerRevoice.mjs +++ b/src/constants/FluxerRevoice.mjs @@ -179,7 +179,8 @@ export class FluxerVoiceConnection extends EventEmitter { } catch (e) { logger.warn("[FluxerVoiceConnection] Room disconnect error:", e?.message); } - } else if (this._nativeConn && typeof this._nativeConn.disconnect === "function") { + } + if (this._nativeConn && typeof this._nativeConn.disconnect === "function") { try { await this._nativeConn.disconnect(); } catch (e) { @@ -275,6 +276,12 @@ export class FluxerRevoice extends EventEmitter { this._intentionalDisconnects.delete(key); this._intentionalDisconnectTokens.delete(key); }, 10_000)); + const cleanChannelId = key.replace(/\D/g, ""); + if (this.client?._remix?.intentionalLeaves && !this.client._remix.intentionalLeaves.has(cleanChannelId)) { + this.client._remix.intentionalLeaves.set(cleanChannelId, setTimeout(() => { + this.client._remix.intentionalLeaves.delete(cleanChannelId); + }, 10_000)); + } } /** @@ -352,7 +359,8 @@ export class FluxerRevoice extends EventEmitter { } catch (e) { logger.warn("[FluxerRevoice] Stale room disconnect error:", e?.message); } - } else if (existing._nativeConn && typeof existing._nativeConn.disconnect === "function") { + } + if (existing._nativeConn && typeof existing._nativeConn.disconnect === "function") { try { await existing._nativeConn.disconnect(); } catch (e) { @@ -546,7 +554,6 @@ export class FluxerRevoice extends EventEmitter { const isIntentional = this._intentionalDisconnects.has(String(channelId)); const reasonLabel = isIntentional ? "intentional" : (reason ?? "unexpected"); logger.player(`[FluxerRevoice] Room disconnected: ${reasonLabel}`); - this._intentionalDisconnects.delete(String(channelId)); conn._connected = false; conn._destroyed = true; @@ -562,7 +569,9 @@ export class FluxerRevoice extends EventEmitter { } } - conn.emit("disconnect"); + if (conn.listenerCount("disconnect") > 0) { + conn.emit("disconnect"); + } }); room.on(LKRoomEvent.ParticipantConnected, (participant) => {