我正在尝试编写自己的Discord Bot,但遇到了我无法解决的错误:
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module './commands/${file}'
Require stack:
- C:\Users\vatri\Desktop\DiscordBot\main.js
[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:841:27)[39m
[90m at Module.require (internal/modules/cjs/loader.js:1025:19)[39m
[90m at require (internal/modules/cjs/helpers.js:72:18)[39m
at Object.<anonymous> (C:\Users\vatri\Desktop\DiscordBot\main.js:13:21)
[90m at Module._compile (internal/modules/cjs/loader.js:1137:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:985:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:878:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)[39m {
code: [32m'MODULE_NOT_FOUND'[39m,
requireStack: [ [32m'C:\\Users\\vatri\\Desktop\\DiscordBot\\main.js'[39m ]
}
还有我的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
const fs = require('fs');
client.commands == new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require('./commands/${file}');
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Playlist is on!');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
} else if (command === 'hello'){
}
});
我尝试了很多解决方案,但没有找到任何解决方法,因此它确实可以帮助我
对不起,如果我的英语不好,我不是英语 image of the code
现在我收到此错误
`client.commands.setTo(command.name, command);
^
TypeError: Cannot read property 'setTo' of undefined
at Object.<anonymous> (C:\Users\vatri\Desktop\DiscordBot\main.js:15:21)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules
/run_main.js:71:12)
at internal/main/run_main_module.js:17:47`
答案 0 :(得分:0)
要使用template literals,必须将字符串用反引号引起来,而不要使用引号引起来。
更改
const command = require('./commands/${file}');
收件人
const command = require(`./commands/${file}`);
^ ^