Node.js Discord机器人命令处理模块找不到错误求助
模块找不到错误的排查与修复
核心问题1:路径大小写不匹配 + 相对路径错误
错误提示找不到../../src/Commands/tools/ping.js,根源在于:
- 你读取命令文件夹时用的是
./src/commands(小写c),但require时写成了../../src/Commands(大写C),Node.js的require会严格匹配路径字符串,即使Windows文件系统不区分大小写也会导致识别失败。 handleCommands.js位于src/functions/handlers/目录下,相对路径../../src/Commands会跳转到项目根目录下的src/Commands,但实际命令文件就在当前src目录下的commands文件夹里,不需要重复写src/。
修复代码:
const command = require(`../../commands/${folder}/${file}`);
问题2:模板字符串语法错误
代码中日志输出的模板字符串使用了错误的语法$(),导致无法正确渲染命令名称:
// 错误写法 console.log("Command: $(command.data.name) has passed through the handler");
修复代码:
console.log(`Command: ${command.data.name} has passed through the handler`);
问题3:变量名拼写错误
刷新应用命令时,变量名多写了一个n,导致无法正确读取命令数组:
// 错误写法 body: client.commanndArray,
修复代码:
body: client.commandArray,
内容的提问来源于stack exchange,提问作者Marc Dyas
相关产品推荐
相关产品推荐

