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

420
Vistas
convert string to callback for use as function parameters

I'm working on a bot, and in each of my commands' files I have an execute function in the module.exports, for example: execute(msg, args), I was able to get just the parameters between the brackets in my index.js, and I want to use them to dynamically execute all the functions/variables (and also because I was having problems with the order in which I wrote them for each file) I need in the other file with module.exports. This is my (index.js) code:

    const cmd = client.commands.get(command)
    const stringifiedCmd = cmd.execute.toString()

    var firstLine = stringifiedCmd.split('\n')[0]
    // Gets only the first line with the execute function
    var str = firstLine.substring(
        firstLine.lastIndexOf(`(`) + 1,
        firstLine.indexOf(`)`)
        // Outputs msg, args for example 

 cmd.execute(str)

However when using the command it doesn't seem to be recognizing the parameters as valid, as they're still a string

EDIT:

If needed, here's an example of how all my commands' files are structured:


module.exports = {
    name: 'command',
    description: 'description of command',
    execute(msg, args) {

          // code

    }
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You got quite far along this solution. Having got everything between the opening and closing parenthesis, you'll want to split out that and trim the results

const argsStrings = firstLine.substring(
          firstLine.lastIndexOf(`(`) + 1,
          firstLine.indexOf(`)`)
        ).split(",").map(x => x.trim());

Now you have an array of strings representing the args (["msg","args"]). Now, where are you going to get the values from? Lets assume for a moment you have a map containing the arg as key, and the value as the arg value - eg

const container = { 
     "msg": "Some message", 
     "args": ["foo","bar"] 
};

Now, its as easy as calling apply on the function passing the values looked up from container for each argument

function execute(msg, args) { // Use this method to demo the result

  console.log(msg);
  console.log(args);

}

const stringifiedCmd = execute.toString()
const firstLine = stringifiedCmd.split('\n')[0]
const argsStrings = firstLine.substring(
          firstLine.lastIndexOf(`(`) + 1,
          firstLine.indexOf(`)`)
        ).split(",").map(x => x.trim());
        

const container = { "msg":"Some message", "args":["foo","bar"] }

execute.apply(this, argsStrings.map(a => container[a]))

Caveat: The only thing you might want to watch out for is there is nothing in javascript stopping you splitting the args of a function accross multiple lines

function thisIsValid(
   arg1,
   arg2
){
   // code here
}
about 4 years ago · Juan Pablo Isaza Denunciar
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