Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda