fix(gateway): auto-rejoin voice on WS-drop-induced disconnects

Previously, only 24/7-auto channels would auto-rejoin after a voice
disconnect. Every other channel would emit autoleave and the bot
would leave voice — even when the disconnect was caused by a transient
WS reconnect, not a user action.

Now the bot tracks when Events.Ready last fires (initial login + every
reconnect). When a voice disconnect is detected within 30s of a Ready
event, it's treated as WS-induced and the bot auto-rejoins the channel
using the same cleanup + schedule rejoin logic as 24/7-auto mode,
regardless of the channel's 24/7 setting.

User-initiated leaves (not preceded by a WS reconnect) still emit
autoleave as before.
This commit is contained in:
NoLogicAlan committed 2026-06-21 21:24:03 +03:00
1 parent 799b527467
commit 23505457fa
2 files changed
+25 -4

No files matched your search

+24 -2
View File
@@ -51,6 +51,23 @@ export class GatewayHandler {
this._inStartupGrace = true;
/** @type {number} Counter for deferred deletions (used for batch summary log) */
this._deferredDeleteCount = 0;
/** @type {number} Timestamp of the last Events.Ready fire (initial + reconnects). 0 = never. */
this._lastReadyAt = 0;
/** @type {number} Window after a Ready event during which voice disconnects are treated as WS-induced (ms). */
this._wsReconnectGraceMs = 30_000;
}
/**
* Check whether a voice disconnect was likely caused by a recent WS reconnect
* rather than a user-initiated leave. Uses the time since the last Events.Ready
* fire as the signal — if the bot just re-identified, any voice session
* invalidation is almost certainly WS-induced.
* @returns {boolean}
*/
isWsReconnectRecent() {
if (!this._lastReadyAt) return false;
return Date.now() - this._lastReadyAt < this._wsReconnectGraceMs;
}
/**
@@ -1111,10 +1128,14 @@ export class GatewayHandler {
} else {
const set = remix.settingsMgr.getServer(guildId);
const mode = set ? get247ChannelMode(set, cleanOld) : "off";
const wsInduced = this.isWsReconnectRecent();
if (mode === "auto") {
if (mode === "auto" || wsInduced) {
const reason = wsInduced && mode !== "auto"
? `WS reconnect detected (${Math.round((Date.now() - this._lastReadyAt) / 1000)}s ago) — treating as transient, scheduling rejoin`
: `24/7 auto mode — cleaning up and scheduling rejoin`;
logger.voice247(
`[VoiceState] Bot unexpectedly disconnected from ${cleanOld} (24/7 auto) — cleaning up and scheduling rejoin.`
`[VoiceState] Bot unexpectedly disconnected from ${cleanOld} — ${reason}.`
);
const discPlayer = remix.players.playerMap.get(cleanOld);
if (discPlayer && !discPlayer._destroyed) {
@@ -1429,6 +1450,7 @@ export class GatewayHandler {
}
onReady() {
this._lastReadyAt = Date.now();
this.seedVoiceStatesFromGuilds();
this.seedGuildsFromRest().catch(e => logger.warn("[onReady] seedGuildsFromRest error:", e.message));
this.attachRawListener();