This commit is contained in:
NoLogicAlan committed 2026-05-16 17:56:39 +03:00
1 parent 082cdbc3b1
commit b75ea56ca9
5 files changed
+200 -25

No files matched your search

+17 -2
View File
@@ -16,14 +16,29 @@ export async function run(msg, data) {
const channelCounts = new Map();
for (const [, state] of this.observedVoiceUsers) {
// Use VoiceStateCache API for both humans and bots
const cache = this.voiceCache ?? this.observedVoiceUsers;
if (cache) {
// Humans
if (typeof cache.iterateHumanUsers === "function") {
for (const [, state] of cache.iterateHumanUsers()) {
if (state.guildId !== guild.id) continue;
channelCounts.set(state.channelId, (channelCounts.get(state.channelId) ?? 0) + 1);
}
for (const [, state] of this.observedVoiceBots) {
} else {
for (const [, state] of cache) {
if (state.guildId !== guild.id) continue;
channelCounts.set(state.channelId, (channelCounts.get(state.channelId) ?? 0) + 1);
}
}
// Bots
if (typeof cache.iterateBotUsers === "function") {
for (const [, state] of cache.iterateBotUsers()) {
if (state.guildId !== guild.id) continue;
channelCounts.set(state.channelId, (channelCounts.get(state.channelId) ?? 0) + 1);
}
}
}
if (channelCounts.size === 0) {
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(msg, "responses.test.noOneInVoice"));
+6 -8
View File
@@ -324,18 +324,16 @@ export class GatewayHandler {
}
}
// ── Fallback: observedVoiceUsers ────────────────────────────────────────
if (humansFound === 0) {
for (const [uid, info] of remix.observedVoiceUsers) {
const infoGuild = String(info.guildId ?? "").replace(/\D/g, "");
const infoChannel = String(info.channelId ?? "").replace(/\D/g, "");
if (infoGuild === cleanGuild && infoChannel === cleanChannel) {
// ── Fallback: VoiceStateCache (was observedVoiceUsers) ────────────────
if (humansFound === 0 && remix.voiceCache) {
// O(1) lookup via channelMembers index instead of full scan
const humans = remix.voiceCache.getHumansInChannel(cleanGuild, cleanChannel);
for (const uid of humans) {
humansFound++;
logger.voiceState(
`[Reseed] Found human ${uid} in channel ${cleanChannel} (guild ${cleanGuild}) via observedVoiceUsers`
`[Reseed] Found human ${uid} in channel ${cleanChannel} (guild ${cleanGuild}) via VoiceStateCache`
);
}
}
// Update bot's own entry
if (botId) {
+10 -6
View File
@@ -462,13 +462,17 @@ export class PlayerManager {
const seedObserved = (channelId) => {
const cleanChannelId = cleanId(channelId);
if (!cleanChannelId) return null;
if (!this.observedVoiceUsers?.has(userId)) {
this.observedVoiceUsers?.set(userId, { channelId: cleanChannelId, guildId: cleanGuild });
// Use voiceCache (O(1) with guildId) or fall back to observedVoiceUsers
const cache = this.voiceCache ?? this.observedVoiceUsers;
if (cache && !cache.has(userId, cleanGuild)) {
cache.set(userId, { channelId: cleanChannelId, guildId: cleanGuild });
}
return cleanId(this.observedVoiceUsers?.get(userId)?.channelId) || cleanChannelId;
const loc = cache?.get(userId, cleanGuild);
return cleanId(loc?.channelId) || cleanChannelId;
};
const observed = this.observedVoiceUsers?.get?.(userId);
const cache = this.voiceCache ?? this.observedVoiceUsers;
const observed = cache?.get?.(userId, cleanGuild);
if (cleanId(observed?.guildId) === cleanGuild) {
const observedChannelId = cleanId(observed?.channelId);
if (observedChannelId) return observedChannelId;
@@ -1018,8 +1022,8 @@ export class PlayerManager {
const users = this.voiceCache.getHumansInChannel(guildId, channelId);
humanUserIds.push(...users);
} else if (this.observedVoiceUsers) {
// Legacy fallback
for (const [uid, info] of this.observedVoiceUsers.iterateHumanUsers()) {
// Legacy fallback (VoiceStateCache is iterable via [Symbol.iterator])
for (const [uid, info] of this.observedVoiceUsers) {
if (cleanId(info.guildId) === guildId && cleanId(info.channelId) === channelId) {
humanUserIds.push(uid);
}
+135
View File
@@ -503,6 +503,141 @@ export class VoiceStateCache {
return this.botLocations.has(compositeKey);
}
// ── Map-compatible iteration ─────────────────────────────────────────────
// Allows `for (const [uid, info] of voiceCache)` to work the same as
// `for (const [uid, info] of observedVoiceUsers)` on a raw Map.
// Delegates to iterateHumanUsers() so the yielded shape matches.
/**
* Default iterator — iterates human users.
* Yields [userId, {channelId, guildId}] — same shape as old observedVoiceUsers Map.
*/
*[Symbol.iterator]() {
for (const [uKey, loc] of this.userLocations) {
yield [loc.userId, { channelId: loc.channelId, guildId: loc.guildId }];
}
}
/**
* Backward-compat with `observedVoiceUsers.size`.
*/
get size() { return this.userLocations.size; }
/**
* Backward-compat with `observedVoiceUsers.has(userId)`.
* If called with a single argument (userId), scans all guilds.
* If called with two arguments (userId, guildId), does O(1) lookup.
*
* @param {string} userId
* @param {string} [guildId]
* @returns {boolean}
*/
has(userId, guildId) {
if (guildId !== undefined) {
return this.userLocations.has(VoiceStateCache.userKey(guildId, userId));
}
// Legacy: scan for first match by userId only
const cleanUser = String(userId).replace(/\D/g, "");
for (const [, loc] of this.userLocations) {
if (loc.userId === cleanUser) return true;
}
return false;
}
/**
* Backward-compat with `observedVoiceUsers.get(userId)`.
* If called with a single argument (userId), returns first match across all guilds.
* If called with two arguments (userId, guildId), does O(1) lookup.
*
* @param {string} userId
* @param {string} [guildId]
* @returns {{channelId: string, guildId: string}|undefined}
*/
get(userId, guildId) {
if (guildId !== undefined) {
const loc = this.userLocations.get(VoiceStateCache.userKey(guildId, userId));
return loc ? { channelId: loc.channelId, guildId: loc.guildId } : undefined;
}
// Legacy: scan for first match by userId only
const cleanUser = String(userId).replace(/\D/g, "");
for (const [, loc] of this.userLocations) {
if (loc.userId === cleanUser) return { channelId: loc.channelId, guildId: loc.guildId };
}
return undefined;
}
/**
* Backward-compat with `observedVoiceUsers.set(userId, {channelId, guildId})`.
*
* @param {string} userId
* @param {{channelId: string, guildId: string}} info
*/
set(userId, info) {
const guildId = info?.guildId;
const channelId = info?.channelId;
if (guildId && channelId) {
this.updateUser({ guildId, userId, channelId, isBot: false });
}
}
/**
* Backward-compat with `observedVoiceUsers.delete(userId)`.
*
* @param {string} userId
* @param {string} [guildId]
*/
delete(userId, guildId) {
if (guildId !== undefined) {
this.updateUser({ guildId, userId, channelId: null, isBot: false });
} else {
// Legacy: remove from ALL guilds
const cleanUser = String(userId).replace(/\D/g, "");
const toRemove = [];
for (const [uKey, loc] of this.userLocations) {
if (loc.userId === cleanUser) toRemove.push(loc);
}
for (const loc of toRemove) {
this.updateUser({ guildId: loc.guildId, userId: cleanUser, channelId: null, isBot: false });
}
}
}
/**
* Backward-compat with `observedVoiceUsers.forEach(fn)`.
*
* @param {Function} fn callback(userId, info, cache)
*/
forEach(fn) {
for (const [userId, info] of this) {
fn(userId, info, this);
}
}
/**
* Backward-compat with `observedVoiceUsers.entries()`.
*/
*entries() {
yield* this;
}
/**
* Backward-compat with `observedVoiceUsers.keys()`.
*/
*keys() {
for (const [userId] of this) {
yield userId;
}
}
/**
* Backward-compat with `observedVoiceUsers.values()`.
*/
*values() {
for (const [, info] of this) {
yield info;
}
}
// ── Debug / stats ────────────────────────────────────────────────────────
get stats() {
+32 -9
View File
@@ -272,9 +272,10 @@ export class Dashboard {
// Return the user's current voice channel (if any)
if (!user) return { channel: null };
const userId = String(user.id).replace(/\D/g, "");
// Check observedVoiceUsers first (most reliable for Fluxer)
if (this.remix.observedVoiceUsers) {
for (const [mapUserId, info] of this.remix.observedVoiceUsers) {
// Check VoiceStateCache first (most reliable for Fluxer)
// Uses O(1) lookup via iterateHumanUsers() / [Symbol.iterator]
if (this.remix.voiceCache) {
for (const [mapUserId, info] of this.remix.voiceCache) {
if (String(mapUserId).replace(/\D/g, "") === userId && info.channelId) {
const ch = this.remix.client.channels.get(info.channelId);
return {
@@ -362,8 +363,16 @@ export class Dashboard {
const cleanChanId = String(player._channelId ?? "").replace(/\D/g, "");
if (!cleanChanId) return "Player has no active channel";
const observed = this.remix.observedVoiceUsers;
const observed = this.remix.voiceCache;
if (observed) {
// O(1) lookup: check if user is in the same channel as the player
const guildId = String(player._guildId ?? "").replace(/\D/g, "");
const userLoc = observed.get(cleanUserId, guildId || undefined);
if (userLoc) {
const infoChannelId = String(userLoc.channelId ?? "").replace(/\D/g, "");
if (infoChannelId === cleanChanId) return null;
}
// Legacy scan fallback (user might not have guildId)
for (const [mapUserId, info] of observed) {
if (String(mapUserId).replace(/\D/g, "") === cleanUserId) {
const infoChannelId = String(info.channelId ?? "").replace(/\D/g, "");
@@ -602,13 +611,26 @@ export class Dashboard {
}
}
}
// Fallback: observedVoiceUsers (seeded from READY/GUILD_CREATE).
// Fallback: VoiceStateCache (seeded from READY/GUILD_CREATE).
// After a bot reload, guild.voice_states may only contain the bot's
// own voice state, while observedVoiceUsers has ALL humans who were
// own voice state, while VoiceStateCache has ALL humans who were
// already in voice when the bot reconnected.
const ovu = player._observedVoiceUsers;
if (ovu && cleanChannelId && cleanGuildId) {
for (const [mapUserId, info] of ovu) {
// Try O(1) channel lookup first, then fall back to iteration.
const vc = player._voiceCache ?? player._observedVoiceUsers;
if (vc && cleanChannelId && cleanGuildId) {
// O(1) lookup via getHumansInChannel
if (typeof vc.getHumansInChannel === "function") {
const channelHumans = vc.getHumansInChannel(cleanGuildId, cleanChannelId);
for (const hid of channelHumans) {
if (seen.has(hid)) continue;
const botId = player.client?.user?.id;
if (botId && String(hid) === String(botId)) continue;
ids.push(String(hid));
seen.add(hid);
}
} else {
// Legacy iteration fallback
for (const [mapUserId, info] of vc) {
if (seen.has(mapUserId)) continue;
const infoCh = String(info.channelId ?? "").replace(/\D/g, "");
const infoG = String(info.guildId ?? "").replace(/\D/g, "");
@@ -621,6 +643,7 @@ export class Dashboard {
}
}
}
}
// Merge dashboard users (connected via web UI but not necessarily in Discord voice)
if (player._dashboardUsers) {
for (const du of player._dashboardUsers) {