【问题标题】:Discord.JS Command Handler Subfolders SearchDiscord.JS 命令处理程序子文件夹搜索
【发布时间】:2019-07-19 08:55:41
【问题描述】:

我的 discord 机器人有一个命令处理程序,它搜索文件夹 ./commands/,其中列出了所有 .js 命令。我想清理所有命令,而不是将它们全部放在同一个文件夹中,而是将它们分别放在自己的类别文件夹中。现在的问题是我不知道如何让机器人搜索./commands/ 文件夹的子目录,以在其自己的类别文件夹中找到每个命令。下面是我用来在./commands/ 中搜索的代码。有什么想法让它搜索./commands/ 中的每个目录吗?

client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();

fs.readdir("./commands/", (err, files) => {
  if (err) return console.error(err);
  console.log(`Loading a total of ${files.length} commands.`);
  files.forEach(file => {
    if (!file.endsWith(".js")) return;
    let props = require(`./commands/${file}`);
    console.log(`Loading Command: ${props.help.name} ✔`);
    client.commands.set(props.help.name, props);
        props.conf.aliases.forEach(alias => {
      client.aliases.set(alias, props.help.name);
    });
  });
});

编辑:

这是 jakemingolla 帮助我创造的答案:

function walk(dir, callback) {
    fs.readdir(dir, function(err, files) {
        if (err) throw err;
        files.forEach(function(file) {
            console.log(`Loading a total of ${files.length} commands.`);
            var filepath = path.join(dir, file);
            fs.stat(filepath, function(err,stats) {
                if (stats.isDirectory()) {
                    walk(filepath, callback);
                } else if (stats.isFile() && file.endsWith('.js')) {
                    let props = require(`./${filepath}`);
                    console.log(`Loading Command: ${props.help.name} ✔`);
                    client.commands.set(props.help.name, props);
                    props.conf.aliases.forEach(alias => {
                    client.aliases.set(alias, props.help.name);
                  });
                }
            });
        });
    });
}
walk(`./commands/`)

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    这来自Discord.js guide

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

    【讨论】:

      【解决方案2】:

      用函数封装重复的工作!

      您已经完成了一半 - 您有一个函数可以获取单个目录并列出其中的文件。如果遇到目录,请重复该过程:

      function walk(dir, callback) {
          fs.readdir(dir, function(err, files) {
              if (err) throw err;
              files.forEach(function(file) {
                  var filepath = path.join(dir, file);
                  fs.stat(filepath, function(err,stats) {
                      if (stats.isDirectory()) {
                          walk(filepath, callback);
                      } else if (stats.isFile() && file.endsWith('.js')) {
                          ... your logic here ...
                      }
                  });
              });
          });
      }
      

      【讨论】:

      • 我试图弄清楚如何在我的代码中实现它,但我收到一个错误,指出path.join 中的path 未定义。关于如何解决这个问题的任何想法?
      • 好吧,我想我知道如何通过安装 npm 模块path 并将const path = require("path"); 放在顶部来修复path.join,但即使我这样做它也不会工作。没有错误,但我尝试在代码中有...your logic here... 的地方放置console.log("True"),但没有任何反应。我做错了什么。
      • 我应该定义dir吗?老实说,我不知道该怎么做。
      • 是的,您对path 的看法是正确的,我应该更明确地调用require()
      • 在这种情况下,dirwalk() 函数的参数 - 为了在您的情况下调用该函数,您需要使用回调并提供起始位置:@ 987654334@
      猜你喜欢
      • 2021-12-13
      • 2022-01-14
      • 2021-11-04
      • 2021-11-02
      • 2020-06-09
      • 2021-08-01
      • 2019-09-23
      • 2020-06-17
      相关资源
      最近更新 更多