mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
fix(voice, 247): harden voice state routing, gateway recovery, and 24/7 rejoin resilience
- Normalize VOICE_STATE_UPDATE fields (snake_case and camelCase) and add fallback to playerMap for resolving disconnected channels - Directly dispatch raw WebSocket VOICE_STATE_UPDATE packets to VoiceStateRouting to prevent missed disconnect events - Clear RejoinBackoff cooldowns on gateway READY/RESUMED and suppress failure penalties during gateway downtime - Reduce RejoinBackoff base cooldown from 120s to 30s for faster self-healing - Defer 24/7 boot recovery and watchdog sweeps until gateway connection is confirmed active - Add REST guild fetch and channel fallbacks in Watchdog247, GatewayHandler, and RejoinManager so uncached guilds never skip 24/7 channels - Clean channel snowflake IDs on both sides in get247ChannelMode
This commit is contained in:
1 parent
cc07c35f2a
commit
6c826891eb
7 files changed
+111
-33
No files matched your search
@@ -189,7 +189,7 @@ const BotVoiceMixin = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const guild = this.client?.guilds?.get?.(cleanG);
|
const guild = this.client?.guilds?.get?.(cleanG);
|
||||||
if (!guild) {
|
if (!guild && this.client?.guilds?.size > 0 && typeof this.client?.guilds?.fetch !== "function") {
|
||||||
logger.voice247(`[247] Bot is not currently in guild ${cleanG} — skipping rejoin for ${cleanCh} (setting kept intact)`);
|
logger.voice247(`[247] Bot is not currently in guild ${cleanG} — skipping rejoin for ${cleanCh} (setting kept intact)`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,10 +54,12 @@ export function get247ChannelMode(set, channelId) {
|
|||||||
if (!set?.get) return "off";
|
if (!set?.get) return "off";
|
||||||
const raw = set.get("stay_247");
|
const raw = set.get("stay_247");
|
||||||
if (!raw || raw === "none") return "off";
|
if (!raw || raw === "none") return "off";
|
||||||
|
const target = cleanId(channelId);
|
||||||
|
if (!target) return "off";
|
||||||
const channels = Array.isArray(raw)
|
const channels = Array.isArray(raw)
|
||||||
? raw.map(id => String(id).trim()).filter(Boolean)
|
? raw.map(id => cleanId(id)).filter(Boolean)
|
||||||
: [String(raw).trim()];
|
: [cleanId(raw)].filter(Boolean);
|
||||||
return channels.includes(channelId) ? "on" : "off";
|
return channels.includes(target) ? "on" : "off";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -242,6 +242,7 @@ class GatewayHandler {
|
|||||||
this._lastReadyAt = Date.now();
|
this._lastReadyAt = Date.now();
|
||||||
remix._lastGatewayHeartbeat = Date.now();
|
remix._lastGatewayHeartbeat = Date.now();
|
||||||
remix._gatewayConnected = true;
|
remix._gatewayConnected = true;
|
||||||
|
this.rejoinBackoff?.clear?.();
|
||||||
const readyGuilds = payload.d?.guilds;
|
const readyGuilds = payload.d?.guilds;
|
||||||
if (Array.isArray(readyGuilds)) {
|
if (Array.isArray(readyGuilds)) {
|
||||||
for (const g of readyGuilds) {
|
for (const g of readyGuilds) {
|
||||||
@@ -277,6 +278,7 @@ class GatewayHandler {
|
|||||||
this._lastReadyAt = Date.now();
|
this._lastReadyAt = Date.now();
|
||||||
remix._lastGatewayHeartbeat = Date.now();
|
remix._lastGatewayHeartbeat = Date.now();
|
||||||
remix._gatewayConnected = true;
|
remix._gatewayConnected = true;
|
||||||
|
this.rejoinBackoff?.clear?.();
|
||||||
|
|
||||||
if (remix._hasBooted) {
|
if (remix._hasBooted) {
|
||||||
logger.voice247("[Gateway] RESUMED session active — scheduling 24/7 self-healing sweep in 3s...");
|
logger.voice247("[Gateway] RESUMED session active — scheduling 24/7 self-healing sweep in 3s...");
|
||||||
@@ -314,9 +316,9 @@ class GatewayHandler {
|
|||||||
|
|
||||||
if (payload.t === "VOICE_STATE_UPDATE") {
|
if (payload.t === "VOICE_STATE_UPDATE") {
|
||||||
const d = payload.d;
|
const d = payload.d;
|
||||||
const userId = d?.user_id;
|
const userId = cleanId(d?.user_id ?? d?.userId);
|
||||||
const channelId = d?.channel_id ?? null;
|
const channelId = cleanId(d?.channel_id ?? d?.channelId) || null;
|
||||||
const guildId = d?.guild_id;
|
const guildId = cleanId(d?.guild_id ?? d?.guildId);
|
||||||
const isBot = resolveIsBotUser({ userId, member: d?.member, guild: client.guilds?.get?.(guildId), client, botId: client.user?.id });
|
const isBot = resolveIsBotUser({ userId, member: d?.member, guild: client.guilds?.get?.(guildId), client, botId: client.user?.id });
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
if (channelId) {
|
if (channelId) {
|
||||||
@@ -324,6 +326,11 @@ class GatewayHandler {
|
|||||||
} else {
|
} else {
|
||||||
remix.voiceCache.updateUser({ guildId, userId, channelId: null, isBot });
|
remix.voiceCache.updateUser({ guildId, userId, channelId: null, isBot });
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
this._handleVoiceStateUpdate(d);
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn("[Gateway] Error in _handleVoiceStateUpdate from raw event:", e?.message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch(e) { logger.warn("[Gateway] Raw gateway handler error:", e?.message); }
|
} catch(e) { logger.warn("[Gateway] Raw gateway handler error:", e?.message); }
|
||||||
};
|
};
|
||||||
@@ -536,8 +543,10 @@ class GatewayHandler {
|
|||||||
this._processGuildDelete(guildId);
|
this._processGuildDelete(guildId);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on(Events.VoiceStateUpdate, async (data) => {
|
client.on(Events.VoiceStateUpdate, async (oldOrNew, maybeNew) => {
|
||||||
this._handleVoiceStateUpdate(data);
|
const data = maybeNew ?? oldOrNew;
|
||||||
|
const extra = maybeNew ? oldOrNew : null;
|
||||||
|
this._handleVoiceStateUpdate(data, extra);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on(Events.VoiceStatesSync, (data) => {
|
client.on(Events.VoiceStatesSync, (data) => {
|
||||||
@@ -674,6 +683,18 @@ class GatewayHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Ensure backoff cooldowns from previous session/downtime are reset */
|
||||||
|
this.rejoinBackoff?.clear?.();
|
||||||
|
|
||||||
|
/* Wait for gateway to establish connection so voice joins do not immediately time out */
|
||||||
|
if (remix._gatewayConnected === false) {
|
||||||
|
logger.voice247("[BootRecovery] Waiting up to 30s for gateway connection before rejoining 24/7 channels...");
|
||||||
|
const startWait = Date.now();
|
||||||
|
while (remix._gatewayConnected === false && Date.now() - startWait < 30_000) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const lava = remix.lavalink;
|
const lava = remix.lavalink;
|
||||||
if (lava?.hasConnectedNode && !lava.hasConnectedNode() && lava.waitForNode) {
|
if (lava?.hasConnectedNode && !lava.hasConnectedNode() && lava.waitForNode) {
|
||||||
@@ -696,7 +717,16 @@ class GatewayHandler {
|
|||||||
const cleanG = cleanId(guildId);
|
const cleanG = cleanId(guildId);
|
||||||
const cleanC = cleanId(channelId);
|
const cleanC = cleanId(channelId);
|
||||||
|
|
||||||
const guild = remix.client?.guilds?.get?.(cleanG);
|
let guild = remix.client?.guilds?.get?.(cleanG);
|
||||||
|
if (!guild && typeof remix.client?.guilds?.fetch === "function") {
|
||||||
|
try { guild = await remix.client.guilds.fetch(cleanG).catch(() => null); } catch (_) {}
|
||||||
|
}
|
||||||
|
if (!guild) {
|
||||||
|
const { channel: chCheck } = await this._resolve247Channel(cleanC);
|
||||||
|
if (chCheck) {
|
||||||
|
guild = chCheck.guild ?? { id: cleanG };
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!guild) {
|
if (!guild) {
|
||||||
logger.voice247(
|
logger.voice247(
|
||||||
`[BootRecovery] Bot is not currently in guild ${cleanG} — skipping channel ${cleanC} (setting kept intact).`
|
`[BootRecovery] Bot is not currently in guild ${cleanG} — skipping channel ${cleanC} (setting kept intact).`
|
||||||
@@ -738,7 +768,8 @@ class GatewayHandler {
|
|||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (channelObj.type !== 2) {
|
const isVoice = channelObj.type === 2 || channelObj.type === 13 || channelObj.type === "voice";
|
||||||
|
if (!isVoice) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`[BootRecovery] Channel ${channelId} in guild ${guildId} is not a voice channel (type: ${channelObj.type}) — skipping.`
|
`[BootRecovery] Channel ${channelId} in guild ${guildId} is not a voice channel (type: ${channelObj.type}) — skipping.`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,12 +13,12 @@
|
|||||||
export class RejoinBackoff {
|
export class RejoinBackoff {
|
||||||
/**
|
/**
|
||||||
* @param {object} [o]
|
* @param {object} [o]
|
||||||
* @param {number} [o.baseMs=120000] - Cool-down after the first failed attempt cycle (doubles each time).
|
* @param {number} [o.baseMs=30000] - Cool-down after the first failed attempt cycle (doubles each time).
|
||||||
* @param {number} [o.maxMs=3600000] - Upper bound for the cool-down.
|
* @param {number} [o.maxMs=3600000] - Upper bound for the cool-down.
|
||||||
* @param {number} [o.maxEntries=5000] - Safety cap on remembered channels (oldest evicted first).
|
* @param {number} [o.maxEntries=5000] - Safety cap on remembered channels (oldest evicted first).
|
||||||
* @param {() => number} [o.now] - Clock (injectable for tests).
|
* @param {() => number} [o.now] - Clock (injectable for tests).
|
||||||
*/
|
*/
|
||||||
constructor({ baseMs = 120_000, maxMs = 3_600_000, maxEntries = 5_000, now = Date.now } = {}) {
|
constructor({ baseMs = 30_000, maxMs = 3_600_000, maxEntries = 5_000, now = Date.now } = {}) {
|
||||||
this.baseMs = baseMs;
|
this.baseMs = baseMs;
|
||||||
this.maxMs = maxMs;
|
this.maxMs = maxMs;
|
||||||
this.maxEntries = maxEntries;
|
this.maxEntries = maxEntries;
|
||||||
@@ -47,6 +47,11 @@ export class RejoinBackoff {
|
|||||||
this._entries.delete(String(channelId));
|
this._entries.delete(String(channelId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Clear all backoff entries (used on gateway reconnection). */
|
||||||
|
clear() {
|
||||||
|
this._entries.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/** @returns {number} Milliseconds until the watchdog may try this channel again (0 = go ahead). */
|
/** @returns {number} Milliseconds until the watchdog may try this channel again (0 = go ahead). */
|
||||||
remainingMs(channelId) {
|
remainingMs(channelId) {
|
||||||
const e = this._entries.get(String(channelId));
|
const e = this._entries.get(String(channelId));
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ const RejoinManager = {
|
|||||||
|| errMsg.includes("internal error")
|
|| errMsg.includes("internal error")
|
||||||
|| errMsg.includes("ETIMEDOUT")
|
|| errMsg.includes("ETIMEDOUT")
|
||||||
|| errMsg.includes("ECONNRESET")
|
|| errMsg.includes("ECONNRESET")
|
||||||
|
|| errMsg.includes("Gateway")
|
||||||
|
|| this.remix?._gatewayConnected === false
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isTransientError && attempt < maxRetries) {
|
if (isTransientError && attempt < maxRetries) {
|
||||||
@@ -97,7 +99,10 @@ 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);
|
let guild = remix.client?.guilds?.get?.(cleanGuildId);
|
||||||
|
if (!guild && typeof remix.client?.guilds?.fetch === "function") {
|
||||||
|
try { guild = await remix.client.guilds.fetch(cleanGuildId).catch(() => null); } catch (_) {}
|
||||||
|
}
|
||||||
if (!guild) {
|
if (!guild) {
|
||||||
logger.voice247(`[Rejoin] Bot is not currently in guild ${cleanGuildId} — skipping ${cleanChannelId} (setting kept intact).`);
|
logger.voice247(`[Rejoin] Bot is not currently in guild ${cleanGuildId} — skipping ${cleanChannelId} (setting kept intact).`);
|
||||||
return;
|
return;
|
||||||
@@ -168,7 +173,10 @@ const RejoinManager = {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this._rejoinAttempts.delete(cleanChannelId);
|
this._rejoinAttempts.delete(cleanChannelId);
|
||||||
this.rejoinBackoff?.recordFailure?.(cleanChannelId);
|
/* Only apply backoff cool-down if failure was not caused by a gateway outage */
|
||||||
|
if (this.remix?._gatewayConnected !== false) {
|
||||||
|
this.rejoinBackoff?.recordFailure?.(cleanChannelId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return player;
|
return player;
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -28,38 +28,47 @@ const VoiceStateRouting = {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
async _handleVoiceStateUpdate(data) {
|
async _handleVoiceStateUpdate(data, extraState = null) {
|
||||||
const { remix } = this;
|
const { remix } = this;
|
||||||
const client = remix.client;
|
const client = remix.client;
|
||||||
|
|
||||||
const userId = data?.user_id;
|
/* Normalize field names across raw gateway payloads and library VoiceState objects */
|
||||||
|
const rawState = data;
|
||||||
|
const userId = cleanId(rawState?.user_id ?? rawState?.userId ?? rawState?.id ?? rawState?.member?.user?.id ?? rawState?.member?.id);
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
const newChannelId = data?.channel_id ?? null;
|
const newChannelId = cleanId(rawState?.channel_id ?? rawState?.channelId ?? rawState?.channel?.id) || null;
|
||||||
const vsuGuildId = data?.guild_id;
|
const vsuGuildId = cleanId(rawState?.guild_id ?? rawState?.guildId ?? rawState?.guild?.id);
|
||||||
const isBot = resolveIsBotUser({
|
|
||||||
|
const botId = cleanId(client.user?.id ?? remix.botId ?? remix.config?.botId ?? client.id ?? "");
|
||||||
|
const isBotUser = Boolean(botId && userId === botId);
|
||||||
|
const isBot = isBotUser || resolveIsBotUser({
|
||||||
userId,
|
userId,
|
||||||
member: data?.member,
|
member: rawState?.member,
|
||||||
guild: client.guilds?.get?.(vsuGuildId),
|
guild: client.guilds?.get?.(vsuGuildId),
|
||||||
client,
|
client,
|
||||||
botId: client.user?.id,
|
botId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const prevEntry = this.findPrevVoiceStateEntry(userId, vsuGuildId);
|
const prevEntry = this.findPrevVoiceStateEntry(userId, vsuGuildId);
|
||||||
const prev = prevEntry.value;
|
const prev = prevEntry.value;
|
||||||
const prevSameGuild = prev &&
|
const prevSameGuild = prev &&
|
||||||
cleanId(prev.guildId ?? "") === cleanId(vsuGuildId ?? "");
|
cleanId(prev.guildId ?? "") === cleanId(vsuGuildId ?? "");
|
||||||
const oldChannelId = prevSameGuild ? (prev?.channelId ?? null) : null;
|
let oldChannelId = prevSameGuild ? (prev?.channelId ?? null) : null;
|
||||||
|
|
||||||
|
|
||||||
this._updatePreviousStates(userId, vsuGuildId, oldChannelId, newChannelId, isBot, prevEntry, prev);
|
|
||||||
this._updateVoiceStateCache(userId, vsuGuildId, oldChannelId, newChannelId, isBot, prev);
|
|
||||||
|
|
||||||
|
if (!oldChannelId && extraState) {
|
||||||
|
oldChannelId = cleanId(extraState?.channel_id ?? extraState?.channelId ?? extraState?.channel?.id) || null;
|
||||||
|
}
|
||||||
|
|
||||||
const { player, playerGuildId, homeChannelId } = this._findAffectedPlayer(vsuGuildId, oldChannelId, newChannelId);
|
const { player, playerGuildId, homeChannelId } = this._findAffectedPlayer(vsuGuildId, oldChannelId, newChannelId);
|
||||||
|
|
||||||
const isBotUser = isBot === true && userId === client.user?.id;
|
/* If this is the bot disconnecting, but oldChannelId was not tracked in _prevVoiceState, resolve it from the player */
|
||||||
|
if (!oldChannelId && isBotUser && player) {
|
||||||
|
oldChannelId = cleanId(player._channelId ?? player._home247Channel ?? "") || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._updatePreviousStates(userId, vsuGuildId, oldChannelId, newChannelId, isBot, prevEntry, prev);
|
||||||
|
this._updateVoiceStateCache(userId, vsuGuildId, oldChannelId, newChannelId, isBot, prev);
|
||||||
|
|
||||||
/* Handle server-mute (admin / community mute) on the bot */
|
/* Handle server-mute (admin / community mute) on the bot */
|
||||||
if (isBotUser && player) {
|
if (isBotUser && player) {
|
||||||
@@ -217,6 +226,14 @@ const VoiceStateRouting = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (guildId) {
|
||||||
|
const cleanG = cleanId(guildId);
|
||||||
|
const player = [...remix.players.playerMap.values()].find(p => !p._destroyed && cleanId(p._guildId) === cleanG);
|
||||||
|
if (player) {
|
||||||
|
return { player, playerGuildId: player._guildId, homeChannelId: player._home247Channel };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return { player: null, playerGuildId: null, homeChannelId: null };
|
return { player: null, playerGuildId: null, homeChannelId: null };
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -150,6 +150,10 @@ const Watchdog247 = {
|
|||||||
logger.voice247("[Watchdog247] Boot recovery in progress — skipping sweep.");
|
logger.voice247("[Watchdog247] Boot recovery in progress — skipping sweep.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (remix._gatewayConnected === false) {
|
||||||
|
logger.voice247("[Watchdog247] Gateway is currently disconnected — deferring sweep until connection is restored.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let healthy = 0, healed = 0, pruned = 0, skipped = 0;
|
let healthy = 0, healed = 0, pruned = 0, skipped = 0;
|
||||||
const savedChannels = [];
|
const savedChannels = [];
|
||||||
@@ -170,9 +174,19 @@ const Watchdog247 = {
|
|||||||
const cleanG = cleanId(guildId);
|
const cleanG = cleanId(guildId);
|
||||||
const cleanCh = cleanId(channelId);
|
const cleanCh = cleanId(channelId);
|
||||||
|
|
||||||
const guild = remix.client?.guilds?.get?.(cleanG);
|
let guild = remix.client?.guilds?.get?.(cleanG);
|
||||||
|
if (!guild && typeof remix.client?.guilds?.fetch === "function") {
|
||||||
|
try { guild = await remix.client.guilds.fetch(cleanG).catch(() => null); } catch (_) {}
|
||||||
|
}
|
||||||
if (!guild) {
|
if (!guild) {
|
||||||
// Bot is not in this guild — keep 24/7 setting intact for future invite, but skip sweep
|
/* If channel itself resolves via REST, the guild is accessible to the bot */
|
||||||
|
const { channel: chCheck } = await this._resolve247Channel(cleanCh);
|
||||||
|
if (chCheck) {
|
||||||
|
guild = chCheck.guild ?? { id: cleanG };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!guild) {
|
||||||
|
/* Bot is not in this guild — keep 24/7 setting intact for future invite, but skip sweep */
|
||||||
skipped++;
|
skipped++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -219,11 +233,12 @@ const Watchdog247 = {
|
|||||||
skipped++;
|
skipped++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (channel && channel.type !== 2) {
|
const isVoice = channel.type === 2 || channel.type === 13 || channel.type === "voice";
|
||||||
|
if (channel && !isVoice) {
|
||||||
if (this._prune247Channel(guildId, channelId)) {
|
if (this._prune247Channel(guildId, channelId)) {
|
||||||
pruned++;
|
pruned++;
|
||||||
remix.cancel247Rejoin?.(channelId);
|
remix.cancel247Rejoin?.(channelId);
|
||||||
logger.voice247(`[Watchdog247] ${channelId} is not a voice channel anymore — pruned.`);
|
logger.voice247(`[Watchdog247] ${channelId} is not a voice channel anymore (type: ${channel.type}) — pruned.`);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user