Discord.js V14命令执行报错:TypeError: Cannot read properties of undefined (reading 'reply')
Discord.js V14 命令执行错误排查与修复
核心问题与修复方案
1. 命令执行时未传递必要参数
在index.js的interactionCreate事件中,调用command.execute()时没有传入client和interaction参数,导致ping.js里的interaction变量为undefined,触发Cannot read properties of undefined (reading 'reply')错误。
修复:
修改index.js中执行命令的代码,传入所需参数:
client.on('interactionCreate',async (interaction)=>{ if(!interaction.isChatInputCommand()) return; // 补全方法调用的括号 const command = client.commands.get(interaction.commandName); if(!command) return; try{ await command.execute(client, interaction) // 传入client和interaction参数 }catch(err){ const commandnotresponseerrembed = new EmbedBuilder() .setTitle("봇 오류") .setDescription("명령어를 실행하는 데 문제가 있습니다. 밑의 오류 내용을 개발자에게 알려주세요!") .setColor("#ff4040") const errcontentembed = new EmbedBuilder() .setTitle("오류 내용") .setDescription(`${err}`) .setColor("#ff4040") interaction.reply({embeds:[commandnotresponseerrembed, errcontentembed]}) } })
2. ping.js中reply语法错误
ping.js里的interaction.reply.({content:["퐁!"]})存在两处错误:
- 多了一个多余的
.,导致无法正确调用reply方法 content参数不需要用数组包裹,直接传字符串即可
修复:
修改ping.js的execute方法:
async execute(client, interaction){ interaction.reply({content: "퐁!"}) }
3. 补充:isChatInputCommand方法调用遗漏括号
原代码中if(!interaction.isChatInputCommand) return;未调用方法,应改为if(!interaction.isChatInputCommand()) return;,否则无法正确判断交互类型。
内容的提问来源于stack exchange,提问作者404_PageNotFound
相关产品推荐
相关产品推荐

