处理程序命令中有什么方法可以做子文件夹吗?

时间:2019-04-20 11:07:26

标签: discord.js

我正在尝试使另一个命令处理程序更高级。我已经看过几种类型的代码来解释带有文件夹/子文件夹/命令的处理程序命令,但我仍然不太了解如何做到这一点。

我已经尝试过使用fs,并且我想使用它来执行此操作,但是我仍然不能。

这是我当前的代码(没有尝试)。

const Discord = require("discord.js");
const client = new Discord.Client();
const db = require('quick.db');
const fs = require("fs");
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.events = new Discord.Collection();

const utils = require("./utils/utils");
const config = require("./utils/config.json");

fs.readdir("./src/events/", (err, files) => {
    if (err) return console.error(err);
    files.forEach(file => {
        let eventFunction = require(`./src/events/${file}`);
        let eventStart = eventFunction.run.bind(null, client);
        let eventName = file.split(".")[0];
        client.events.set(eventName, eventStart);
        client.on(eventName, (...args) => eventFunction.run(client, utils, ...args));
    });
});

fs.readdir('./src/commands/', (err, files) => {
    if (err) console.error(err);
    files.forEach(f => {
        let props = require(`./src/commands/${ f }`);
        props.fileName = f;
        client.commands.set(props.help.name, props);
        props.help.aliases.forEach(alias => {
            client.aliases.set(alias, props.help.name);
        });
    });
});

client.on("message", async message => {
    try {
        let prefix = await db.fetch(`prefixo_${message.guild.id}`);

        if (prefix === null) prefix = "m!";
        if (message.author.bot) return;
        if (message.content.indexOf(prefix) !== 0) return;

        const args = message.content.slice(prefix.length).trim().split(/ +/g);
        let command = args.shift().toLowerCase();

        if (client.aliases.has(command)) command = client.commands.get(client.aliases.get(command)).help.name;

        if (client.commands.get(command).config.restricted == true) {
            if (message.author.id !== config.ownerID) return utils.errorEmbed(message, 'No permission.');
        }

        if (client.commands.get(command).config.args == true) {
            if (!args[0]) return utils.errorEmbed(message, `Invalid arguments. Use: ${prefix + 'help ' + client.commands.get(command).help.name}`);
        }

        let commandFile = require(`./src/commands/${command}.js`);
        commandFile.run(client, message, args, utils);

    } catch (err) {
        if (err.message === `Cannot read property 'config' of undefined`) return;
        if (err.code == "MODULE_NOT_FOUND") return;
    console.error(err);
    }
});

client.login(config.token);

1 个答案:

答案 0 :(得分:0)

我也想要同样的东西,花了我一段时间才弄清楚! 这是我的ready.js(就绪事件)中的命令处理程序:

const { promisify } = require("util");
const readdir = promisify(require("fs").readdir);

const stuff = ['dev','donators', 'fun', 'misc', 'moderation']; //my subfolders (i.e: modules)
stuff.forEach(c => { //loop through each module in the array
readdir(`./commands/${c}/`, (err, files) => { //use fs to read the directory with module name as path
  if (err) throw err;
  console.log(`Loading a total of ${files.length} commands (${c})`);
  files.forEach(f => {
    if (!f.endsWith(".js")) return;
    client.loadCommand(`${f}`, `./commands/${c}/${f}`);
  });
 });
});

我的client.loadCommand()方法使用文件名f和该文件./commands/${c}/${f}的文件路径,并使用需要文件路径的props方法,并添加props.help.nameclient.commands

请记住file structure of your project will need to be like this,其中模块名称(const stuff = ["module", "anothermod"])与子文件夹名称完全相同,它们都位于父文件夹commands下。