bot: fall back to Discord's voice-state API when the gateway cache misses the caller
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ef7b44f074
commit
75d969e1c5
1 file changed
+16
-7
+16
-7
@@ -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);
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user