【发布时间】:2021-02-23 17:15:17
【问题描述】:
所以我制作了这段代码来从这个目录中提取:命令,正如你在屏幕截图中看到的那样:
enter image description here 但我收到一条错误消息,说该文件不存在。该文件与 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!');
}
});
【问题讨论】:
标签: javascript node.js visual-studio-code directory discord.js