Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!