Javascript 如何使此代码适用于子文件夹(主';命令';文件夹中的文件夹)?
如何使此代码适用于子文件夹(主“命令”文件夹中的文件夹)?这是我的一些代码 Index.js:Javascript 如何使此代码适用于子文件夹(主';命令';文件夹中的文件夹)?,javascript,node.js,directory,discord.js,fs,Javascript,Node.js,Directory,Discord.js,Fs,如何使此代码适用于子文件夹(主“命令”文件夹中的文件夹)?这是我的一些代码 Index.js: const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(
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.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, client);
} catch (error) {
console.error(error);
const errembed = new Discord.MessageEmbed()
.setColor('#009ACD')
.setDescription('There was an error trying to execute that command!')
message.channel.send(errembed);
}
});
Examplecommand.js:
const Discord = require('discord.js')
module.exports = {
name: 'command name',
description: 'command description',
execute(message, args) {
//code here
}
}
我不知道如何在子文件夹中使用优雅的解决方案来实现此功能,因此我认为您应该这样做:
client.subFolderCommands = new Discord.Collection();
const subFolderFiles = fs.readdirSync('./commands/subfolder').filter(file => file.endsWith('.js'));
for (var i = 0; i < subFolderFiles.length; i++) {
const command = require(`./commands/subfolder/${subFolderFiles[i]}`);
client.subFolderCommands.set(command.name, command);
}
client.subFolderCommands=newdiscord.Collection();
const subFolderFiles=fs.readdirSync('./commands/subfolder').filter(file=>file.endsWith('.js'));
对于(var i=0;i
如果你知道更好的解决方案,请告诉我 您所能做的就是让
readdirSync
方法返回文件和/或目录。您可以通过将方法中的选项withFileTypes
设置为true
来完成此操作。然后您可以迭代结果,如果条目是子目录,则读取该文件夹中的所有文件/目录。您可以使用函数检查这一点
下面是一个可行的示例:
函数readFilesFromPath(路径字符串){
const directoryEntries=fs.readdirSync(路径字符串,{withFileTypes:true});
返回directoryEntries.reduce((filteredEntries,dirEnt)=>{
if(dirEnt.isDirectory()){
//如果条目是目录,请再次调用此函数
//但是现在将目录名添加到路径字符串中。
filteredEntries.push(…readFilesFromPath(`${pathString}/${dirEnt.name}'))
}else if(dirEnt.isFile()){
//检查条目是否为文件。如果是,请检查
//如果文件名以“.js”结尾。
if(dirEnt.name.endsWith('.js')){
//将文件添加到命令文件数组中。
filteredEntries.push(`${pathString}/${dirEnt.name}`);
}
}
返回过滤器配件;
}, []);
}
//使用命令和的根文件夹调用readfiles函数
//将所有文件路径存储在常量中。
const commandfilepath=readFilesFromPath('./commands');
//循环文件路径数组并在客户端上设置命令。
CommandFilePath.forEach((文件路径)=>{
const命令=require(文件路径);
client.commands.set(command.name,command);
});
这是我的命令处理程序,支持子文件夹。 只需将前5行替换为这些
const commandFolders=fs.readdirSync('./commands');
用于(commandFolders的常量文件夹){
const commandFiles=fs.readdirSync(`./commands/${folder}`).filter(file=>file.endsWith('.js'));
for(命令文件的常量文件){
const command=require(`./commands/${folder}/${file}`);
client.commands.set(command.name,command);
}
}