diff --git a/commands/skip.mjs b/commands/skip.mjs index 2b9731f..192825c 100644 --- a/commands/skip.mjs +++ b/commands/skip.mjs @@ -66,6 +66,7 @@ export async function run(message) { guildId, channelId, client: this.client, + botId: this.client?.user?.id, voiceCache: this.voiceCache, observedVoiceUsers: this.observedVoiceUsers, }); diff --git a/src/core/Bot.mjs b/src/core/Bot.mjs index a01bf30..230c4a7 100644 --- a/src/core/Bot.mjs +++ b/src/core/Bot.mjs @@ -16,6 +16,7 @@ import { Locale } from "./Locale.mjs"; import { CommandHandler, CommandLoader, PrefixManager } from "../commands/index.mjs"; import { MessageHandler, HelpCommand, setGlobalColor } from "../ui/index.mjs"; import { cleanId } from "../utils/Utils.mjs"; +import { resolveIsBotUser } from "../voice/VoiceStateResolver.mjs"; import * as ShardingUtils from "../utils/ShardingUtils.mjs"; import { RemoteSettingsManager } from "../db/Settings.mjs"; import { PlayerStateStore } from "../db/PlayerStateStore.mjs"; @@ -473,8 +474,13 @@ class Remix { const stateChannel = cleanId(state?.channelId ?? state?.channel_id); if (stateChannel === cleanChanId) { const stateUserId = state?.userId ?? state?.user_id ?? state?.id; - const member = guild?.members?.get?.(stateUserId); - const isBot = member?.user?.bot ?? state?.member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ + userId: stateUserId, + member: state?.member, + guild, + client: this.client, + botId: this.client?.user?.id, + }); if (!isBot) { hasHuman = true; break; diff --git a/src/music/PlayerLifecycleMixin.mjs b/src/music/PlayerLifecycleMixin.mjs index 98ab7cb..e2e027f 100644 --- a/src/music/PlayerLifecycleMixin.mjs +++ b/src/music/PlayerLifecycleMixin.mjs @@ -93,7 +93,8 @@ const PlayerLifecycleMixin = { if (!channelId && guild) { - for (const vs of iterateVoiceStates(guild)) { + const lcClient = this.commands?.client; + for (const vs of iterateVoiceStates(guild, { client: lcClient, botId: lcClient?.user?.id })) { if (vs.userId === String(userId) && !vs.isBot) { channelId = vs.channelId; break; diff --git a/src/voice/VoiceStateResolver.mjs b/src/voice/VoiceStateResolver.mjs index 2167334..5444e94 100644 --- a/src/voice/VoiceStateResolver.mjs +++ b/src/voice/VoiceStateResolver.mjs @@ -2,13 +2,46 @@ import { logger } from "../core/Logger.mjs"; +/** + * Decide whether a voice-state user is a bot, using every available signal: + * explicit self-bot id, the state's own member payload, the guild member + * cache, and the global user cache. Never trusts a single missing flag — + * a voice state without a `member.user` payload used to be classified as + * human, which made third-party bots count toward vote-skip thresholds. + * @param {object} opts + * @param {string} opts.userId + * @param {object} [opts.member] - Member object from the voice state payload (if any). + * @param {object} [opts.guild] - Cached guild object (for the member cache fallback). + * @param {object} [opts.client] - Client (for the global user cache fallback). + * @param {string} [opts.botId] - This bot's own user id. + * @returns {boolean} + */ +export function resolveIsBotUser({ userId, member, guild, client, botId }) { + const id = String(userId ?? ""); + if (!id) return false; + if (botId && id === String(botId)) return true; + + const candidates = [ + member?.user, + guild?.members?.get?.(id)?.user, + client?.users?.cache?.get?.(id) ?? client?.users?.get?.(id), + ]; + for (const user of candidates) { + if (user && typeof user === "object" && typeof user.bot === "boolean") return user.bot; + } + return false; +} + /** * Iterate over a guild's voice states, normalising different data shapes. * Yields objects with `{ userId, channelId, isBot }` for each member in a voice channel. * @param {object} guild - guild object with a `voice_states` property. + * @param {object} [classifier] - Optional extras for bot detection (see resolveIsBotUser). + * @param {object} [classifier.client] + * @param {string} [classifier.botId] * @yields {{ userId: string, channelId: string, isBot: boolean }} */ -export function* iterateVoiceStates(guild) { +export function* iterateVoiceStates(guild, classifier = {}) { if (!guild) return; const voiceStates = guild.voice_states; @@ -37,7 +70,13 @@ export function* iterateVoiceStates(guild) { if (!userId || !channelId) continue; const member = guild.members?.get?.(userId); - const isBot = member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ + userId, + member, + guild, + client: classifier.client, + botId: classifier.botId, + }); yield { userId: String(userId), channelId: String(channelId), isBot }; } @@ -59,7 +98,6 @@ export function* iterateVoiceStates(guild) { export function hasHumansInChannel({ guildId, channelId, client, voiceCache, observedVoiceUsers, room, botId }) { if (!channelId || !guildId) return false; - if (voiceCache && typeof voiceCache.hasHumansInChannel === "function") { if (voiceCache.hasHumansInChannel(guildId, channelId)) return true; } @@ -90,7 +128,7 @@ export function hasHumansInChannel({ guildId, channelId, client, voiceCache, obs try { const guild = client.guilds?.get?.(guildId); if (guild) { - for (const vs of iterateVoiceStates(guild)) { + for (const vs of iterateVoiceStates(guild, { client, botId })) { if (vs.channelId === channelId && !vs.isBot) return true; } } @@ -124,9 +162,10 @@ export function hasHumansInChannel({ guildId, channelId, client, voiceCache, obs * @param {object} [opts.client] - client with `guilds`. * @param {VoiceStateCache} [opts.voiceCache] * @param {Map} [opts.observedVoiceUsers] + * @param {string} [opts.botId] - This bot's own user id. * @returns {number} */ -export function countHumansInChannel({ guildId, channelId, client, voiceCache, observedVoiceUsers }) { +export function countHumansInChannel({ guildId, channelId, client, voiceCache, observedVoiceUsers, botId }) { if (!channelId || !guildId) return 0; if (voiceCache && typeof voiceCache.getHumanCount === "function") { @@ -138,7 +177,7 @@ export function countHumansInChannel({ guildId, channelId, client, voiceCache, o const guild = client.guilds?.get?.(guildId); if (guild) { const users = new Set(); - for (const vs of iterateVoiceStates(guild)) { + for (const vs of iterateVoiceStates(guild, { client, botId })) { if (vs.channelId === channelId && !vs.isBot) users.add(vs.userId); } return users.size; diff --git a/src/voice/gateway/GatewayHandler.mjs b/src/voice/gateway/GatewayHandler.mjs index 9bd8ed8..40332d0 100644 --- a/src/voice/gateway/GatewayHandler.mjs +++ b/src/voice/gateway/GatewayHandler.mjs @@ -13,7 +13,7 @@ import { ServerSettings } from "../../db/Settings.mjs"; import { get247ChannelMode, remove247ChannelMode } from "../../utils/Helpers247.mjs"; import { cleanId } from "../../utils/Utils.mjs"; import * as ShardingUtils from "../../utils/ShardingUtils.mjs"; -import { iterateVoiceStates, hasHumansInChannel, getChannelsWithHumans } from "../VoiceStateResolver.mjs"; +import { iterateVoiceStates, hasHumansInChannel, getChannelsWithHumans, resolveIsBotUser } from "../VoiceStateResolver.mjs"; import { applyMixins } from "../../utils/mixins.mjs"; import VoiceStateRouting from "./VoiceStateRouting.mjs"; import GuildSync from "./GuildSync.mjs"; @@ -142,6 +142,7 @@ class GatewayHandler { if (!remix._rawGatewayHandler) { remix._rawGatewayHandler = (data) => { try { + const client = remix.client; if (typeof data === "string" && !RAW_GATEWAY_INTEREST.test(data)) return; const payload = typeof data === "string" ? JSON.parse(data) : data; @@ -172,7 +173,7 @@ class GatewayHandler { const userId = state.user_id; const channelId = state.channel_id; if (!userId || !channelId) continue; - const isBot = state.member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ userId, member: state.member, guild: client.guilds?.get?.(gId), client, botId: client.user?.id }); remix.voiceCache.updateUser({ guildId: gId, userId, channelId, isBot }); } } @@ -192,7 +193,7 @@ class GatewayHandler { const userId = state.user_id; const channelId = state.channel_id; if (!userId || !channelId) continue; - const isBot = state.member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ userId, member: state.member, guild: client.guilds?.get?.(gId), client, botId: client.user?.id }); remix.voiceCache.updateUser({ guildId: gId, userId, channelId, isBot }); } } @@ -207,7 +208,7 @@ class GatewayHandler { const userId = d?.user_id; const channelId = d?.channel_id ?? null; const guildId = d?.guild_id; - const isBot = d?.member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ userId, member: d?.member, guild: client.guilds?.get?.(guildId), client, botId: client.user?.id }); if (!userId) return; if (channelId) { remix.voiceCache.updateUser({ guildId, userId, channelId, isBot }); @@ -336,13 +337,13 @@ class GatewayHandler { if (voiceStatesRaw) { const newUserIds = new Set(); - for (const vs of iterateVoiceStates(guild)) { + for (const vs of iterateVoiceStates(guild, { client: remix.client, botId: remix.client?.user?.id })) { newUserIds.add(vs.userId); } remix.voiceCache.purgeUsersInGuild(guildId, newUserIds); } if (voiceStatesRaw) { - for (const vs of iterateVoiceStates(guild)) { + for (const vs of iterateVoiceStates(guild, { client: remix.client, botId: remix.client?.user?.id })) { remix.voiceCache.updateUser({ guildId, userId: vs.userId, channelId: vs.channelId, isBot: vs.isBot }); } } diff --git a/src/voice/gateway/GuildSync.mjs b/src/voice/gateway/GuildSync.mjs index 6ae7fbf..4bf2a1b 100644 --- a/src/voice/gateway/GuildSync.mjs +++ b/src/voice/gateway/GuildSync.mjs @@ -14,7 +14,7 @@ import { getVoiceManager } from "@fluxerjs/voice"; import { logger } from "../../core/Logger.mjs"; import { REQUIRED_BOT_PERMISSIONS } from "../../ui/index.mjs"; import { cleanId } from "../../utils/Utils.mjs"; -import { iterateVoiceStates } from "../VoiceStateResolver.mjs"; +import { iterateVoiceStates, resolveIsBotUser } from "../VoiceStateResolver.mjs"; /** * @type {object} @@ -115,7 +115,7 @@ const GuildSync = { } else { const guild = client.guilds.get(cleanGuildId) ?? client.guilds.get(guildId); const member = guild?.members?.get?.(userId); - const isBot = member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ userId, member, guild, client, botId }); remix.voiceCache.updateUser({ guildId: cleanGuildId, userId, channelId, isBot }); if (isBot) totalBots++; else totalHumans++; @@ -137,7 +137,7 @@ const GuildSync = { } for (const [gId, guild] of client.guilds) { - for (const vs of iterateVoiceStates(guild)) { + for (const vs of iterateVoiceStates(guild, { client, botId: client.user?.id })) { remix.voiceCache.updateUser({ guildId: gId, userId: vs.userId, channelId: vs.channelId, isBot: vs.isBot }); } } @@ -175,7 +175,7 @@ const GuildSync = { } else { const guild = client.guilds.get(cleanGuild) ?? client.guilds.get(guildId); const member = guild?.members?.get?.(userId); - const isBot = member?.user?.bot ?? false; + const isBot = resolveIsBotUser({ userId, member, guild, client, botId }); if (isBot) { remix.voiceCache.updateUser({ guildId: cleanGuild, userId, channelId: cleanChannel, isBot: true }); } else { @@ -210,7 +210,7 @@ const GuildSync = { `[Reseed] Guild ${cleanGuild} not in cache — cannot reseed voice states.` ); } else { - for (const vs of iterateVoiceStates(guild)) { + for (const vs of iterateVoiceStates(guild, { client, botId })) { if (vs.isBot) continue; if (vs.channelId === cleanChannel) { remix.voiceCache.updateUser({ guildId: cleanGuild, userId: vs.userId, channelId: vs.channelId, isBot: false }); diff --git a/src/voice/gateway/VoiceStateRouting.mjs b/src/voice/gateway/VoiceStateRouting.mjs index 743f670..cb931b0 100644 --- a/src/voice/gateway/VoiceStateRouting.mjs +++ b/src/voice/gateway/VoiceStateRouting.mjs @@ -13,6 +13,7 @@ import { logger } from "../../core/Logger.mjs"; import { get247ChannelMode, isPlayerConnectionDead } from "../../utils/Helpers247.mjs"; import { VoiceStateCache } from "../VoiceStateCache.mjs"; +import { resolveIsBotUser } from "../VoiceStateResolver.mjs"; import { cleanId } from "../../utils/Utils.mjs"; /** @@ -35,7 +36,13 @@ const VoiceStateRouting = { const newChannelId = data?.channel_id ?? null; const vsuGuildId = data?.guild_id; - const isBot = data?.member?.user?.bot ?? null; + const isBot = resolveIsBotUser({ + userId, + member: data?.member, + guild: client.guilds?.get?.(vsuGuildId), + client, + botId: client.user?.id, + }); const prevEntry = this.findPrevVoiceStateEntry(userId, vsuGuildId); @@ -79,7 +86,13 @@ const VoiceStateRouting = { const channelId = vs.channel_id; if (!userId || !channelId) continue; - const isBot = vs.member?.user?.bot ?? (userId === botId); + const isBot = resolveIsBotUser({ + userId, + member: vs.member, + guild: client.guilds?.get?.(guildId), + client, + botId, + }); remix.voiceCache.updateUser({ guildId, userId, channelId, isBot }); if (isBot) {