有没有办法在处理程序命令中做子文件夹?

凤凰

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

我已经尝试过使用 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);
爪哇

我也想要同样的东西,我花了一段时间才弄明白!这是我的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.

请记住,您的项目文件结构需要是这样的,其中模块名称 ( const stuff = ["module", "anothermod"]) 与子文件夹名称完全相同,都在commands.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有办法在 djs 中重新加载完整的命令文件夹?

有没有办法在Python目录中查找特定的子文件夹?

有没有办法在当前文件夹中打开命令提示符?

有没有办法从azure blob的容器文件夹中获取所有子文件夹名称?

有没有办法在Redis缓存中建立类似文件夹的层次结构?

有没有办法在Windows中自动执行文件夹优化类型?

有没有办法在Mailkit中递归搜索文件夹?

有没有办法通过网站从github存储库中删除文件夹?

有没有办法在Vagrant中取消同步子文件夹?

有没有办法在Windows 10中更改固定文件夹的顺序?

有没有办法从模板中的替代文件夹 {% extends '' %} 父 html?

有没有办法动态定位我当前 c# Winforms 应用程序的子文件夹/子文件的路径更改?

有没有办法在没有 gitignore 的情况下停止跟踪 VS Code 中的文件夹?

有没有办法将命令映射到键盘以在Windows中打开程序或文件?

有没有办法通过批处理脚本在某些条件下将文件保存在两个不同的文件夹中

有没有办法对子文件夹中的所有文件进行md5sum?

有没有办法使用.netcore web api上传文件夹(也有子文件夹)而不是文件

有没有办法在 gnome 应用程序菜单中手动组织应用程序和文件夹?

构建Python软件包时,有没有办法在源代码发布中包含文件/文件夹,但没有办法

有没有办法使用逻辑应用程序将新的Blob创建为文件夹?

有没有办法将多个Excel文件从Dropbox文件夹加载到R-shiny应用程序中?

有没有办法像在 UWP 中一样在 WPF .NET 5 中获取文件夹/文件云提供程序?

有没有办法使用gradle从文件夹解压缩多个文件?

有没有办法访问文件夹及其来自不同网络的文件?

有没有办法查看特定文件夹的文件大小?

有没有办法在 Pycharm(Python 3.6)中用文件夹分隔文件?

有没有办法批量标准化音频文件的文件夹?

有没有办法从项目的输出文件夹中删除NuGet dll?

有没有办法在Google Chrome(或Chromium)中隐藏单个书签或书签文件夹?