因此,我编写了从该目录:命令中提取的代码,我收到一条错误消息,指出该文件不存在。该文件与 javascript 位于同一文件夹中,但 javascript 似乎无法访问该文件夹。为什么会发生这种情况以及如何解决它。
这是代码
const { prefix, token } = require('./config.json');
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
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('Ready!');
});
client.login(token);
client.on ('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
婷婷同学_
相关分类