1

I'm fairly new to discord bots coding and I would like to check if a variable is a number. My code looks like this, I've tried many options for the "if" statement.

const { DiscordAPIError } = require('discord.js');
const Discord = require('discord.js');

module.exports = {
 name: 'givecookies',
 description: 'Gives cookies to the mentioned user',
 execute(message, args) {
  let User = message.guild.member(message.mentions.members.first());
  if (!User) return message.channel.send('Invalid User');
  var cookiesAmount = args.join(' ').slice(22);
  if (!cookiesAmount) {
   message.reply('invalid amount of cookies');
  }
  if (typeof `${cookiesAmount}` === 'number') {
   console.log('Amount of cookies is a number');
   console.log(`USER = ${User}`);
   console.log(`Amount of cookies = ${cookiesAmount}`);
   var UserID = User.id;
   console.log(`USER ID = ${UserID}`);
  } else {
   message.reply('invalid amount of cookies');
   console.log('Amount of cookies is not a number');
   console.log(`USER = ${User}`);
   console.log(`Amount of cookies = ${cookiesAmount}`);
   var UserID = User.id;
   console.log(`USER ID = ${UserID}`);
  }
 },
};

I've also tried if (typeof(cookiesAmount) === 'number') and if (typeof cookiesAmount === 'number'), but none of them worked. Regardless what the value is, it acts like if it wasn't a number. I made it to log the value of cookiesAmount and it is always right, it logs '5', but it acts like if it wasn't a number. Any ideas of how to fix this? Thanks.

I'm using discord.js version 12

Lioness100
  • 8,260
  • 6
  • 18
  • 49
Pandicon
  • 420
  • 1
  • 15
  • 38
  • Does it log ‘5’ or 5, numbers are different color in the terminal I think. Also just try logging typeof cookiesAmount, and see if it is a number or a string – Bagel03 Aug 28 '20 at 13:24
  • I have this code there: console.log(`Amount of cookies = ${cookiesAmount}`) so it logs Amount of cookies = 5 – Pandicon Aug 28 '20 at 13:35
  • Does this answer your question? [(Built-in) way in JavaScript to check if a string is a valid number](https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number) – Jakye Aug 28 '20 at 14:07

2 Answers2

2

Since you are taking the variable from a Discord message, it will always be a string. However, you can convert it using the + operator.

var cookiesAmounts = 'notANumber';

if (Number.isNaN(+cookiesAmounts)) console.log('This is not a number');
else console.log('This is a number')

var cookiesAmounts = '1';

if (Number.isNaN(+cookiesAmounts)) console.log('This is not a number');
else console.log('This is a number')

JSFiddle

Lioness100
  • 8,260
  • 6
  • 18
  • 49
1

First, this is more of a JS question (it is not Discord specific). The reason it always returns false is that all the args are strings. You should convert the string to a number using the parseInt (or parseFloat) function and check if the result is NaN to know if it is a valid number.

If there is anything unclear with my answer, just ask me a question in the comments

Samuel Martineau
  • 143
  • 1
  • 1
  • 11