I am trying to make my program drag and drop extendable. Just drop a file in the modules directory and let the program try to figure it out. So I can't be sure how many or the name of each module. For testing purposes, I just have two rollAD6 and secondOne. The code below will get me the name of everything exported for the ChatCommands file, but I am not sure how to turn that into anything actionable.
console.dir(Object.keys(require('./Modules/ChatCommands')));
console.log(typeof Object.keys(require('./Modules/ChatCommands')))
let temp = Object.keys(require('./Modules/ChatCommands'))
temp.forEach(element => {
console.log(element)
});
example output:
[ 'rollAD6', 'secondOne' ]
object
rollAD6
secondOne
I could be going about this all wrong I am not familiar with NodeJS. Just need to turn an unknown number of objects from one file into running functions in the main.
./Modules/ChatCommands
class chatCommand {
constructor(key, name) {
this.key = key;
this.name = name;
}
//getter
get name() {
return `${this.name}`
}
get key() {
return `${this.key}`
}
//setter
set name(name) {
return "Once name is set it cannot be changed"
}
set key(key) {
return "Once key is set it cannot be changed"
}
}
class rollAD6 extends chatCommand {
super(key, name, sides) {
this.sides = sides
this.key = key;
this.name = name;
}
payload(sides) {
console.log(`${sides} `);
var randomNumber = Math.floor(Math.random() * sides) + 1;
console.log(`${randomNumber} `);
return `you rolled ${randomNumber} `;
}
}
class secondOne extends chatCommand {
super(key, name) {
this.key = key;
this.name = name;
}
payload() {
return "Hello there"
}
}
module.exports = {
rollAD6,
secondOne
}