fix(voice): prevent 24/7 rejoin spam when bot is not in guild or voice times out

- Check guild membership before BootRecovery and Watchdog sweeps; keep settings intact if bot is not in server
- Wire up RejoinBackoff to apply exponential cooldown when voice joins time out
- Stop redundant retry loops and deduplicate join failure logs
This commit is contained in:
NoLogicAlan committed 2026-10-21 03:38:12 +03:00
1 parent 355f6b1129
commit f2bfff59b5
5 files changed
+61 -8

No files matched your search

+13 -2
View File
@@ -188,6 +188,18 @@ const BotVoiceMixin = {
return; return;
} }
const guild = this.client?.guilds?.get?.(cleanG);
if (!guild) {
logger.voice247(`[247] Bot is not currently in guild ${cleanG} — skipping rejoin for ${cleanCh} (setting kept intact)`);
return;
}
const backoffRemaining = this.gatewayHandler?.rejoinBackoff?.remainingMs?.(cleanCh) ?? 0;
if (backoffRemaining > 0 && delayMs === null) {
logger.voice247(`[247] Channel ${cleanCh} in backoff cooldown (${Math.round(backoffRemaining / 1000)}s left) — skipping immediate rejoin`);
return;
}
if (!this._247RejoinTimers) this._247RejoinTimers = new Map(); if (!this._247RejoinTimers) this._247RejoinTimers = new Map();
if (this._247RejoinTimers.has(cleanCh)) return; if (this._247RejoinTimers.has(cleanCh)) return;
@@ -407,8 +419,7 @@ const BotVoiceMixin = {
this.players._pendingJoins.delete(cleanChannelId); this.players._pendingJoins.delete(cleanChannelId);
} }
this.players.playerMap.delete(cleanChannelId); this.players.playerMap.delete(cleanChannelId);
try { player.destroy(); } catch(e) { logger.warn("[_spawnPlayer] Cleanup destroy error:", e?.message); } try { player.destroy(); } catch(e) {}
logger.warn(`[_spawnPlayer] Failed to spawn player for channel ${cleanChannelId}:`, err.message);
throw err; throw err;
} }
}, },
+1 -1
View File
@@ -608,7 +608,7 @@ class Player extends EventEmitter {
} catch (e) { } catch (e) {
const causeStr = e.cause ? ` (Cause: ${e.cause})` : ""; const causeStr = e.cause ? ` (Cause: ${e.cause})` : "";
logger.error("[Player] Join failed:", e.message, causeStr); logger.warn("[Player] Join failed:", e.message, causeStr);
if (this._voiceConn) { if (this._voiceConn) {
try { await this._voiceConn.disconnect(); } catch(err) { logger.warn("[Player] voiceConn disconnect on join failure:", err?.message); } try { await this._voiceConn.disconnect(); } catch(err) { logger.warn("[Player] voiceConn disconnect on join failure:", err?.message); }
+17 -2
View File
@@ -19,6 +19,7 @@ import VoiceStateRouting from "./VoiceStateRouting.mjs";
import GuildSync from "./GuildSync.mjs"; import GuildSync from "./GuildSync.mjs";
import RejoinManager from "./RejoinManager.mjs"; import RejoinManager from "./RejoinManager.mjs";
import Watchdog247 from "./Watchdog247.mjs"; import Watchdog247 from "./Watchdog247.mjs";
import RejoinBackoff from "./RejoinBackoff.mjs";
const RAW_GATEWAY_INTEREST = const RAW_GATEWAY_INTEREST =
/"(?:t|op)":\s*(?:"(?:VOICE_STATE_UPDATE|VOICE_SERVER_UPDATE|GUILD_CREATE|READY|RESUMED)"|(?:7|9|10|12)\b)/; /"(?:t|op)":\s*(?:"(?:VOICE_STATE_UPDATE|VOICE_SERVER_UPDATE|GUILD_CREATE|READY|RESUMED)"|(?:7|9|10|12)\b)/;
@@ -41,6 +42,8 @@ class GatewayHandler {
this.presenceContents = remix.config.presenceContents ?? []; this.presenceContents = remix.config.presenceContents ?? [];
this.presenceInterval = remix.config.presenceInterval ?? 30_000; this.presenceInterval = remix.config.presenceInterval ?? 30_000;
this.rejoinBackoff = new RejoinBackoff();
this.wsListenerAttached = false; this.wsListenerAttached = false;
this.presenceTimer = null; this.presenceTimer = null;
this.presenceIndex = 0; this.presenceIndex = 0;
@@ -521,6 +524,16 @@ class GatewayHandler {
for (let i = 0; i < channelsToRejoin.length; i++) { for (let i = 0; i < channelsToRejoin.length; i++) {
const { guildId, channelId, mode } = channelsToRejoin[i]; const { guildId, channelId, mode } = channelsToRejoin[i];
const cleanG = cleanId(guildId);
const cleanC = cleanId(channelId);
const guild = remix.client?.guilds?.get?.(cleanG);
if (!guild) {
logger.voice247(
`[BootRecovery] Bot is not currently in guild ${cleanG} — skipping channel ${cleanC} (setting kept intact).`
);
continue;
}
if (i > 0) { if (i > 0) {
let staggerDelay = baseStagger; let staggerDelay = baseStagger;
@@ -581,13 +594,15 @@ class GatewayHandler {
const player = await this._attemptRejoin(channelId, guildId, 3, 5_000, "BootRecovery"); const player = await this._attemptRejoin(channelId, guildId, 3, 5_000, "BootRecovery");
if (player) { if (player) {
this.rejoinBackoff?.recordSuccess?.(channelId);
logger.voice247( logger.voice247(
`[BootRecovery] Successfully rejoined channel ${channelId} (mode: ${mode}) [${i + 1}/${channelsToRejoin.length}]` `[BootRecovery] Successfully rejoined channel ${channelId} (mode: ${mode}) [${i + 1}/${channelsToRejoin.length}]`
); );
} else { } else {
this.rejoinBackoff?.recordFailure?.(channelId);
logger.warn( logger.warn(
`[BootRecovery] Failed to rejoin channel ${channelId} in guild ${guildId} after retries — ` + `[BootRecovery] Could not rejoin channel ${channelId} in guild ${guildId} — ` +
`keeping 24/7 setting intact, will retry on next disconnect/restart.` `keeping 24/7 setting intact (backed off).`
); );
} }
} }
+14 -2
View File
@@ -57,8 +57,7 @@ const RejoinManager = {
} }
logger.warn( logger.warn(
`[${context}] Failed to rejoin channel ${channelId} (attempt ${attempt}/${maxRetries}): ${errMsg}` + `[${context}] Failed to rejoin channel ${channelId}: ${errMsg} — keeping 24/7 setting intact.`
(attempt > 1 ? ` (after ${attempt} attempts)` : '')
); );
return null; return null;
} }
@@ -79,6 +78,17 @@ const RejoinManager = {
const cleanGuildId = cleanId(guildId); const cleanGuildId = cleanId(guildId);
const cleanChannelId = cleanId(channelId); const cleanChannelId = cleanId(channelId);
const guild = remix.client?.guilds?.get?.(cleanGuildId);
if (!guild) {
logger.voice247(`[Rejoin] Bot is not currently in guild ${cleanGuildId} — skipping ${cleanChannelId} (setting kept intact).`);
return;
}
if (this.rejoinBackoff?.remainingMs?.(cleanChannelId) > 0) {
logger.voice247(`[Rejoin] Channel ${cleanChannelId} in backoff cooldown — skipping.`);
return;
}
if (this._rejoinInProgress.has(cleanChannelId)) { if (this._rejoinInProgress.has(cleanChannelId)) {
logger.voice247(`[Rejoin] Channel ${cleanChannelId} rejoin already in progress — skipping.`); logger.voice247(`[Rejoin] Channel ${cleanChannelId} rejoin already in progress — skipping.`);
return; return;
@@ -131,6 +141,7 @@ const RejoinManager = {
const player = await this._attemptRejoin(cleanChannelId, cleanGuildId, maxRetries, 5_000, "Rejoin"); const player = await this._attemptRejoin(cleanChannelId, cleanGuildId, maxRetries, 5_000, "Rejoin");
if (player) { if (player) {
this._rejoinAttempts.delete(cleanChannelId); this._rejoinAttempts.delete(cleanChannelId);
this.rejoinBackoff?.recordSuccess?.(cleanChannelId);
try { try {
await remix.restorePlayerStateForChannel?.(cleanGuildId, cleanChannelId, player); await remix.restorePlayerStateForChannel?.(cleanGuildId, cleanChannelId, player);
} catch (err) { } catch (err) {
@@ -138,6 +149,7 @@ const RejoinManager = {
} }
} else { } else {
this._rejoinAttempts.delete(cleanChannelId); this._rejoinAttempts.delete(cleanChannelId);
this.rejoinBackoff?.recordFailure?.(cleanChannelId);
} }
return player; return player;
} finally { } finally {
+16 -1
View File
@@ -167,7 +167,22 @@ const Watchdog247 = {
} }
for (const { guildId, channelId } of savedChannels) { for (const { guildId, channelId } of savedChannels) {
if (remix.intentionalLeaves?.has?.(channelId)) { const cleanG = cleanId(guildId);
const cleanCh = cleanId(channelId);
const guild = remix.client?.guilds?.get?.(cleanG);
if (!guild) {
// Bot is not in this guild — keep 24/7 setting intact for future invite, but skip sweep
skipped++;
continue;
}
if (this.rejoinBackoff?.remainingMs?.(cleanCh) > 0) {
skipped++;
continue;
}
if (remix.intentionalLeaves?.has?.(cleanCh)) {
skipped++; skipped++;
continue; continue;
} }