如何使用 forEach() 定义文件路径集合

How to define collections of filepaths with forEach()

尝试通过将重复操作放入 forEach() 函数来修复它。 console.log 提供所需的结果但不提供代码本身。你能解释一下为什么它不起作用吗?

const bot = new Discord.Client({disableEveryone: true});
const cmdHandler = ["commands","automation"];
const fileSys = require("fs"); 

cmdHandler.forEach((v, y) => {
    bot.v = new Discord.Collection();
    console.log(v);

    fileSys.readdir(`./${v}/`, (error, file) => {

        if(error) console.log(error);

        let  jsfile = file.filter(f => f.split (".").pop() === "js")
        if(jsfile.length <= 0){
            console.log("Couldn't find the commands.");
            return
        }

        jsfile.forEach((f, i) => {
            let props = require(`./${v}/${f}`);
            console.log(`${f} loaded.`);    
            bot.v.set(props.help.name, props);
        });  
    });
});

let fullCmd = msg.content.substr(prefixlen);
    let splitCmd = fullCmd.split(" ");
    let mainCmd = splitCmd[0];
    let args = splitCmd.slice(1);

    // Set variable for directory content called from ./commands
    let commandFile = bot.commands.get(mainCmd);
    // execute "run" section of command
    if(commandFile){ 
        commandFile.run(bot,msg,args);

无法读取未定义的 属性 'get'

这只是将属性 v 添加到 bot 并为其分配值。

bot.v = new Discord.Collection();
// bot.v has a value

您似乎在尝试将数组元素用作属性名称。

bot[v] = new Discord.Collection();
// bot.commands assigned, ... bot.automation assigned