2

如何让此代码适用于子文件夹(主“命令”文件夹内的文件夹)?这是我的一些代码。

索引.js:

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);
    }   
});

示例命令.js:

const Discord = require('discord.js')
module.exports = {
      name: 'command name',
      description: 'command description',
      execute(message, args) {
//code here
      }
}
4

3 回答 3

0

您可以做的是使该readdirSync方法将文件和/或目录返回为directory entry (Dirent). 您可以通过在方法中设置选项withFileTypes来做到这一点。然后您可以遍历结果,如果条目是子目录,则读取该文件夹中的所有文件/目录。您可以使用该功能进行检查。truefs.readdirSyncisDirectory()

这是一个可行的示例:

function readFilesFromPath(pathString) {
    const directoryEntries = fs.readdirSync(pathString, {withFileTypes: true});

    return directoryEntries.reduce((filteredEntries, dirEnt) => {
        if (dirEnt.isDirectory()) {
            // If the entry is a directory, call this function again
            // but now add the directory name to the path string.
            filteredEntries.push(...readFilesFromPath(`${pathString}/${dirEnt.name}`))
        } else if (dirEnt.isFile()) {
            // Check if the entry is a file instead. And if so, check
            // if the file name ends with `.js`.
            if (dirEnt.name.endsWith('.js') {
                // Add the file to the command file array.
                filteredEntries.push(`${pathString}/${dirEnt.name}`);
            }
        }

        return filteredEntries;
    }, []);
}

// Call the read files function with the root folder of the commands and
// store all the file paths in the constant.
const commandFilePaths = readFilesFromPath('./commands');

// Loop over the array of file paths and set the command on the client.
commandFilePaths.forEach((filePath) => {
    const command = require(filePath);

    client.commands.set(command.name, command);
});
于 2020-12-22T09:35:18.183 回答
0

这是我的带有子文件夹支持的命令处理程序。只需用这些替换前 5 行

const commandFolders = fs.readdirSync('./commands');
for (const folder of commandFolders) {
    const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const command = require(`./commands/${folder}/${file}`);
        client.commands.set(command.name, command);
    }
}
于 2021-03-24T18:28:44.510 回答
0

我不知道如何通过优雅的解决方案让它在子文件夹中工作,所以我认为这是你应该做的:

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);
}

如果您知道更好的解决方案,请告诉我!

于 2020-12-22T07:36:38.170 回答