使用 fs 为 discord.js 创建灵活的文件路径 reader?

Making a flexible file path reader for discord.js using fs?

The error from the terminal

我已经为我的 discord.js 机器人制作命令了一段时间,但现在我开始对它们进行分类。我的命令目前正被放入一个新的不和谐集合的收集器中(名为 client.{commandType}commands)。我想在以下代码中将“{commandType}”更改为值“i”。有人知道怎么做吗?

folder.forEach(i => {
  fs.readdir(`/home/runner/commands/${i}`, (err, file) => {
    if (err) console.log(err);

    let jsfile = file.filter(f => f.split(`.`).pop() === `js`);

    jsfile.forEach((f,e) => {
      let props = require(`/home/runner/commands/${i}/${f}`);

      client.commandPermissions.set(props.help.name, props.help.permissionLevel);

      console.log(`${props.help.name}: ${props.help.permissionLevel}`);

      client.{commandType}command.set(props.help.name, props);
             ^^^^^^^^^^^^^

    });

  });

});

那我该如何解决呢?

编辑:我很清楚,我实际上是在尝试这样做:client. + i + commands.set(props.help.name, props);

您应该可以使用:

client[i + 'command'].set(props.help.name, props);

与许多其他语言不同,JavaScript 对象与 dictionary/hashmap 之间没有区别。您可以使用任一符号(obj.propertyobj['property'])来访问对象的 属性。它们之间的主要区别在于 obj.property 看起来更好,但在 obj['property'] 中,'property' 可以是普通字符串、变量或其他一些表达式。