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

125
Vistas
How to loop the same command with different value in discord.js

I'm coding a discord bot with discord.js, and one of the features is that when someone sends a message with the prefix (.exe) with a word right after (e.g. .exebot or .exeserverinv), the bot returns a specific message. The code works just fine, but I know that by just copy pasting the same code over 30 times and just changing the values isn't good optimization.

Is there any simple way that I can make it so that I can just use a loop or something for the command to repeat, but with different values?

Here's the non-optimized code:

command(client, 'command name', message => {
    message.channel.send('bot message')
})
command(client, 'random', message =>{
    message.channel.send('some text')
})
command(client, 'bot', message =>{
    message.channel.send('random text')
})
command(client, 'testing', message =>{
    message.channel.send('text')
})
command(client, 'test', message =>{
    message.channel.send('example text')
})
command(client, 'second to last test', message =>{
    message.channel.send('is almost there')
})
command(client, 'last test', message =>{
    message.channel.send('is the very last one')
})

The command handler that this command is running from is:

const { prefix } = require('./config.json')

module.exports = (client, aliases, callback) => {
    if (typeof aliases === 'string') {
        aliases = [aliases]
    }

    client.on('message', message => {
        const { content } = message;

        aliases.forEach(alias => {
            const command = `${prefix}${alias}`

            if (content.startsWith(`${command} `) || content === command) {
                console.log(`Running the command ${command}`)
                callback(message)
            }
        })
    })
}

Please advise.

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

0

You can use JavaScript's object notation and map the command names to the replies.

const commandToReply = {
    "command name": "bot message",
    "random": "some text",
    "bot": "random text",
    "testing": "text",
    "test": "example text",
    "second to last test": "is almost there",
    "last test": "is the very last one"
};

for (const [name, reply] of Object.entries(commandToReply)) {
    command(client, name, (message) => {
        message.channel.send(reply);
    });
}

To iterate through the object, look at how Object.entries() works.

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