You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js 获取用户当前voice.channelId返回过时数据如何解决?

问题根源

你没有开启语音状态监听意图,Discord不会向你的机器人推送用户语音频道变更的事件,导致本地缓存的用户语音状态永远停留在机器人启动时的快照,即便手动拉取成员信息也无法更新单独的语音状态缓存。

解决步骤

  1. 首先修改main.js中客户端初始化的配置,新增语音状态意图:
const client = new Discord.Client({
    intents: [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES,
        Discord.Intents.FLAGS.GUILD_MEMBERS,
        Discord.Intents.FLAGS.GUILD_PRESENCES,
        // 新增语音状态监听意图
        Discord.Intents.FLAGS.GUILD_VOICE_STATES
    ]
});
  1. 前往Discord开发者平台对应应用的Bot设置页,在「Privileged Gateway Intents」分区下开启Server Voice States权限开关。
  2. 调整你的hit命令代码,不需要额外手动拉取成员信息,添加意图后直接读取的channelId就是实时值:
module.exports = {
 name: 'hit',
 description: "Return your voiceChannel ID",
 execute(message) {
    console.log('User ' + message.author.username + ' // ' + message.author.id + ' used the hit command.');
    // 此处直接读取即可拿到最新状态
    console.log(message.member.voice.channelId);
 }
};

验证效果

修改完成后重启机器人,用户切换语音频道、进出频道时,本地缓存会自动同步更新,每次调用命令返回的都是用户当前真实的语音频道ID,未加入语音频道时返回null。

内容的提问来源于stack exchange,提问作者Sokker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:54:08