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

discord.js机器人执行/play命令报错:displayVoiceState未定义

问题解决:discord.js voiceStateUpdate事件报错 Cannot read properties of undefined (reading 'displayVoiceState')

报错核心原因

报错指向voiceStateUpdate.js第12行的client.config.displayVoiceState,问题出在两个地方:

  1. 事件回调参数顺序错误:discord.js的voiceStateUpdate事件回调参数应为(oldState, newState),你写的(client, oldState, newState)会导致第一个参数client实际是oldState对象,而oldState没有config属性,读取displayVoiceState自然报错。
  2. 配置引用方式错误:你已经通过const config = require("../../config/config.js")导入了配置文件,无需从client实例上读取,直接用导入的config即可。

修复代码

修改voiceStateUpdate.js中的事件回调部分:

错误代码

client.on("voiceStateUpdate", async (client, oldState, newState) => {
  const display = client.config.displayVoiceState;
  // ... 其他代码
});

修复后代码

client.on("voiceStateUpdate", async (oldState, newState) => {
  const display = config.displayVoiceState;
  // ... 其他代码
});

额外优化建议

  • 检查index.js是否正确挂载了client.config,如果没有挂载,后续统一使用导入的config模块更稳妥。
  • 对队列操作添加空值判断,避免类似未定义报错:
    const queue = await client.player.getQueue(oldState.guild.id);
    if (!queue) return;
    const botChannelId = queue.connection?.channel?.id;
    // ... 后续逻辑
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 20:40:29