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

130
Vistas
I tried making a clear command but some of the code didn't work

I am using VSC and the latest discord.js

The code I used in my separate commands.js folder was:

module.exports = {
    name: 'clear',
    description: "clear.message",
    execute(message, args) {
        if(!args[0]) return message.channel.send("Enter a number");
        if(isNaN(args[0])) return message.channel.send("Enter a Real number");

        if(args[0] > 15) return message.channel.send("That number is too high! Try again!");
        if(args[0] < 1) return message.channel.sned("Enter a number larger than 1");
    }
}

In my index.js file my code was:

client.on('message', message => {
 
    const args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0]) {
    case 'clear':
        client.commands.get('clear').execute(message, args);
        break;
    }
});

However when I tried the command with my bot instead of returning the correct messages, it would just send the message "Enter a Real number" even if I did !clear 4. Not too sure what is wrong, help would be much appreciated.

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

0

You are using args[0] when you are looking for a command in index.js and also when looking for an argument in commands.js. So instead of 4, your code is trying to parse clear as a number.

You can change the index of the args to 1 in commands.js.

module.exports = {
    name: 'clear',
    description: "clear.message",
    execute(message, args) {
        if(!args[1]) return message.channel.send("Enter a number");
        if(isNaN(args[1])) return message.channel.send("Enter a Real number");

        if(args[1] > 15) return message.channel.send("That number is too high! Try again!");
        if(args[1] < 1) return message.channel.sned("Enter a number larger than 1");
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Arrays are 0-indexed. Your args array looks like this if you input the command !clear 4:

["clear", "4"]

To access "4" you need to use the correct index (1).

if(!args[1]) return message.channel.send("Enter a number");
if(isNaN(args[1])) return message.channel.send("Enter a Real number");
if(parseInt(args[1] > 15)) return message.channel.send("That number is too high! Try again!");
if(parseInt(args[1] < 1)) return message.channel.send("Enter a number larger than 1"); //there was a typo 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