From 9313c51b4b50abcb02ff3957c6af67beef77baa6 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Tue, 27 Oct 2026 09:10:50 -0500 Subject: [PATCH] bot: fall back to Discord's voice-state API when the gateway cache misses the caller --- src/bot/commands.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/bot/commands.js b/src/bot/commands.js index 3cb6dfd..40b3e54 100644 --- a/src/bot/commands.js +++ b/src/bot/commands.js @@ -24,10 +24,19 @@ const commandDefs = [ const ephemeral = (content) => ({ content, flags: MessageFlags.Ephemeral, allowedMentions: { parse: [] } }); -function userVoiceChannel(interaction) { +// the caller's voice channel: gateway cache first, then ask Discord (the cache can miss +// users who joined before the bot connected, or right after a reconnect) +async function userVoiceChannel(interaction) { const g = interaction.guild; - const vs = g && g.voiceStates.cache.get(interaction.user.id); - return (vs && vs.channelId) || null; + if (!g) return null; + const cached = g.voiceStates.cache.get(interaction.user.id); + if (cached && cached.channelId) return cached.channelId; + try { + const vs = await g.voiceStates.fetch(interaction.user.id, { force: true }); + return (vs && vs.channelId) || null; + } catch { + return null; // 404 = not in voice + } } // "movie:550" / "tv:1399" from autocomplete, else null (free text) @@ -62,7 +71,7 @@ async function adoptReply(ctx, room, interaction, deferred) { } async function watch(interaction, ctx) { - const vc = userVoiceChannel(interaction); + const vc = await userVoiceChannel(interaction); if (!vc) return interaction.reply(ephemeral('Join a voice channel first, then use `/watch` there.')); const raw = interaction.options.getString('query', true); const season = interaction.options.getInteger('season'); @@ -84,20 +93,20 @@ async function watch(interaction, ctx) { await adoptReply(ctx, room, interaction, true); } -function roomFor(interaction, ctx) { - const vc = userVoiceChannel(interaction); +async function roomFor(interaction, ctx) { + const vc = await userVoiceChannel(interaction); return (vc && ctx.rooms.get(vc)) || ctx.rooms.all().find((r) => r.state.textChannelId === interaction.channelId) || null; } async function np(interaction, ctx) { - const room = roomFor(interaction, ctx); + const room = await roomFor(interaction, ctx); if (!room || !room.state.media) return interaction.reply(ephemeral('Nothing is playing. Use `/watch` in a voice channel.')); await interaction.deferReply(); await adoptReply(ctx, room, interaction, true); } async function stop(interaction, ctx) { - const room = roomFor(interaction, ctx); + const room = await roomFor(interaction, ctx); if (!room) return interaction.reply(ephemeral('Nothing is playing.')); if (!ctx.isInVoice(room.id, interaction.user.id)) return interaction.reply(ephemeral(`Join <#${room.id}> to control playback.`)); await room.command('stop', {}, interaction.user.id);