This commit is contained in:
NoLogicAlan committed 2026-05-04 16:15:05 +03:00
1 parent 67a75d221a
commit 6c3ae3d27c
1 file changed
+34 -8
+34 -8
View File
@@ -843,7 +843,29 @@ class Remix {
// @fluxerjs/core fires VoiceStateUpdate with ONE arg (raw gateway data), // @fluxerjs/core fires VoiceStateUpdate with ONE arg (raw gateway data),
// not two args (oldState, newState) We maintain prev state manually. // not two args (oldState, newState) We maintain prev state manually.
// Declared before GuildDelete so the delete handler can purge stale entries. // Declared before GuildDelete so the delete handler can purge stale entries.
const _prevVoiceState = new Map(); // userId → { channelId, guildId } const _prevVoiceState = new Map(); // guildId:userId → { channelId, guildId }
const getPrevVoiceStateKey = (userId, guildId) => {
const cleanUserId = String(userId ?? "").replace(/\D/g, "");
const cleanGuildId = String(guildId ?? "").replace(/\D/g, "");
return cleanUserId && cleanGuildId ? `${cleanGuildId}:${cleanUserId}` : null;
};
const findPrevVoiceStateEntry = (userId, guildId = null) => {
const directKey = getPrevVoiceStateKey(userId, guildId);
if (directKey && _prevVoiceState.has(directKey)) {
return { key: directKey, value: _prevVoiceState.get(directKey) };
}
const cleanUserId = String(userId ?? "").replace(/\D/g, "");
if (!cleanUserId) return { key: null, value: null };
for (const [key, value] of _prevVoiceState) {
if (key.endsWith(`:${cleanUserId}`)) {
return { key, value };
}
}
return { key: null, value: null };
};
// ── Bot kicked / banned / server deleted ───────────────────────────────── // ── Bot kicked / banned / server deleted ─────────────────────────────────
client.on(Events.GuildDelete, (guild) => { client.on(Events.GuildDelete, (guild) => {
@@ -873,9 +895,9 @@ class Remix {
if (info.guildId === guildId) this.observedVoiceBots.delete(userId); if (info.guildId === guildId) this.observedVoiceBots.delete(userId);
} }
// Also purge _prevVoiceState entries for this guild so the Map doesn't grow unbounded // Also purge _prevVoiceState entries for this guild so the Map doesn't grow unbounded
for (const [userId, info] of [..._prevVoiceState]) { for (const [stateKey, info] of [..._prevVoiceState]) {
if (String(info.guildId).replace(/\D/g, "") === String(guildId).replace(/\D/g, "")) if (String(info.guildId).replace(/\D/g, "") === String(guildId).replace(/\D/g, ""))
_prevVoiceState.delete(userId); _prevVoiceState.delete(stateKey);
} }
// Drop the cached settings so stale data can't be read back // Drop the cached settings so stale data can't be read back
@@ -897,20 +919,24 @@ class Remix {
const isBot = data?.member?.user?.bot ?? null; const isBot = data?.member?.user?.bot ?? null;
// Capture old channel BEFORE updating prev state // Capture old channel BEFORE updating prev state
const prev = _prevVoiceState.get(userId); const prevEntry = findPrevVoiceStateEntry(userId, guildId);
const prev = prevEntry.value;
const oldChannelId = prev?.channelId ?? null; const oldChannelId = prev?.channelId ?? null;
const prevKey = prevEntry.key;
// Update prev state — evict oldest entry if Map exceeds 10,000 entries // Update prev state — evict oldest entry if Map exceeds 10,000 entries
// (one per user who has ever touched voice since last restart) to prevent // (one per user who has ever touched voice since last restart) to prevent
// unbounded growth on large bots that never restart. // unbounded growth on large bots that never restart.
const nextKey = getPrevVoiceStateKey(userId, guildId ?? prev?.guildId);
if (channelId) { if (channelId) {
if (_prevVoiceState.size >= 10_000 && !_prevVoiceState.has(userId)) { if (prevKey && prevKey !== nextKey) _prevVoiceState.delete(prevKey);
if (nextKey && _prevVoiceState.size >= 10_000 && !_prevVoiceState.has(nextKey)) {
// Remove the oldest inserted entry (Maps preserve insertion order) // Remove the oldest inserted entry (Maps preserve insertion order)
_prevVoiceState.delete(_prevVoiceState.keys().next().value); _prevVoiceState.delete(_prevVoiceState.keys().next().value);
} }
_prevVoiceState.set(userId, { channelId, guildId }); if (nextKey) _prevVoiceState.set(nextKey, { channelId, guildId: guildId ?? prev?.guildId });
} else { } else {
_prevVoiceState.delete(userId); if (prevKey) _prevVoiceState.delete(prevKey);
} }
// Always update/delete the voice maps so they never accumulate stale entries // Always update/delete the voice maps so they never accumulate stale entries
@@ -1454,4 +1480,4 @@ const saveAndExit = () => {
process.once("SIGINT", saveAndExit); process.once("SIGINT", saveAndExit);
process.once("SIGTERM", saveAndExit); process.once("SIGTERM", saveAndExit);
process.once("SIGUSR2", saveAndExit); process.once("SIGUSR2", saveAndExit);