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.
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");
}
}
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!