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

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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