diff --git a/src/voice/VoiceStateCache.mjs b/src/voice/VoiceStateCache.mjs index 56a52bd..2a35235 100644 --- a/src/voice/VoiceStateCache.mjs +++ b/src/voice/VoiceStateCache.mjs @@ -63,6 +63,19 @@ export class VoiceStateCache { const maxEntries = isBot ? this._maxBots : this._maxUsers; const uKey = VoiceStateCache.userKey(cleanGuild, cleanUser); + const otherLocations = isBot ? this.userLocations : this.botLocations; + const otherChannelIdx = isBot ? this.channelMembers : this.botChannelMembers; + const otherPrev = otherLocations.get(uKey); + if (otherPrev) { + const otherPrevCKey = VoiceStateCache.channelKey(otherPrev.guildId, otherPrev.channelId); + const otherPrevSet = otherChannelIdx.get(otherPrevCKey); + if (otherPrevSet) { + otherPrevSet.delete(cleanUser); + if (otherPrevSet.size === 0) otherChannelIdx.delete(otherPrevCKey); + } + otherLocations.delete(uKey); + } + const prev = locations.get(uKey); if (prev) { const prevCKey = VoiceStateCache.channelKey(prev.guildId, prev.channelId); diff --git a/src/voice/VoiceStateResolver.mjs b/src/voice/VoiceStateResolver.mjs index 5444e94..1373915 100644 --- a/src/voice/VoiceStateResolver.mjs +++ b/src/voice/VoiceStateResolver.mjs @@ -168,6 +168,34 @@ export function hasHumansInChannel({ guildId, channelId, client, voiceCache, obs export function countHumansInChannel({ guildId, channelId, client, voiceCache, observedVoiceUsers, botId }) { if (!channelId || !guildId) return 0; + if (voiceCache && typeof voiceCache.getHumansInChannel === "function") { + const cachedHumans = voiceCache.getHumansInChannel(guildId, channelId); + + if (client) { + let guild = null; + try { guild = client.guilds?.get?.(guildId); } catch (_) { guild = null; } + + let humanCount = 0; + for (const userId of cachedHumans) { + const isBot = resolveIsBotUser({ + userId, + member: guild?.members?.get?.(userId) ?? null, + guild, + client, + botId, + }); + if (isBot) { + try { voiceCache.updateUser({ guildId, userId, channelId, isBot: true }); } catch (_) {} + } else { + humanCount++; + } + } + return humanCount; + } + + return cachedHumans.length; + } + if (voiceCache && typeof voiceCache.getHumanCount === "function") { return voiceCache.getHumanCount(guildId, channelId); }