Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
How do I blindly pull functional blocks from a file with node js?

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
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda