Mimayuki
Mimayuki

Reputation: 127

How to define collections of filepaths with forEach()

Trying to fix repetitive action by fitting it in forEach() function. console.log providing desired results but not code itself. Could you explain why it isnt working?

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

Cannot read property 'get' of undefined

Upvotes: 0

Views: 50

Answers (1)

Monte Hayward
Monte Hayward

Reputation: 477

this just adds an attribute v to bot and assigns its value.

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

it looks as if you are trying to use your array elements as attribute names.

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

Upvotes: 1

Related Questions