discord.js机器人执行/play命令报错:displayVoiceState未定义
问题解决:discord.js voiceStateUpdate事件报错
Cannot read properties of undefined (reading 'displayVoiceState') 报错核心原因
报错指向voiceStateUpdate.js第12行的client.config.displayVoiceState,问题出在两个地方:
- 事件回调参数顺序错误:discord.js的
voiceStateUpdate事件回调参数应为(oldState, newState),你写的(client, oldState, newState)会导致第一个参数client实际是oldState对象,而oldState没有config属性,读取displayVoiceState自然报错。 - 配置引用方式错误:你已经通过
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
相关产品推荐
相关产品推荐

