如何包含多个命令文件



我正在开发一个有大量命令的discord bot,因此我想将命令分为不同的类别,例如"调节命令"musiccommands";等等。但是,我无法导入超过1个命令文件夹。我希望从名为"的文件夹导入命令;"附加";,但我不知道如何将其添加到我的代码中。这是我从一个名为"的文件夹导入文件的代码;命令";

for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
client.on("message", async (message) => {
if (message.author.bot) return;
if (!message.guild) return;
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(PREFIX)})\s*`);
if (!prefixRegex.test(message.content)) return;
const [, matchedPrefix] = message.content.match(prefixRegex);
const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command =
client.commands.get(commandName) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;

如果有人能建议一种从多个文件夹导入命令的方法,我将不胜感激

这是我发现的使用fs:的最佳方式

// get all folders within the command folder
const commandFolders = fs.readdirSync('./commands'); 
for (const folder of commandFolders) {
// get all files within each command folder
const commandFiles = fs.readdirSync(`./commands/${folder}`);
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}

最新更新