Abacki Babacki
Abacki Babacki

Reputation: 11

Always losing (Discordjs V12)

I have a bot in discord.js v12 and I made a piece of code for the bank. I made random coins but the problem is that I always lose coins and never receive/gain.

I want to make it so you lost or gain. Here is my code:

if (message.content === '.randomcoins') {
  let currentBalance = await db.get(`wallet_${message.author.id}`);
  let randomc = Math.floor(Math.random() * 100 + 1);
  var what = Math.floor(Math.random() * 2 + 1);
  if (what === '1') {
    await db.set(`wallet_${message.author.id}`, currentBalance + randomc);
    message.channel.send(`You recived ${randomc} Coins.`);
  } else {
    await db.set(`wallet_${message.author.id}`, currentBalance - randomc);
    message.channel.send(`You lost ${randomc} Coins.`);
  }
}

Upvotes: 1

Views: 20

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23160

It's because you're checking if the variable what is equal to the string "1" but what is always an integer so only the else block runs. The number 1 and the string "1" is not the same thing.

Make sure you check if what === 1 instead and it will work properly:

if (message.content === '.randomcoins') {
  let currentBalance = await db.get(`wallet_${message.author.id}`);
  let randomc = Math.floor(Math.random() * 100 + 1);
  var what = Math.floor(Math.random() * 2 + 1);
  if (what === 1) {
    await db.set(`wallet_${message.author.id}`, currentBalance + randomc);
    message.channel.send(`You recived ${randomc} Coins.`);
  } else {
    await db.set(`wallet_${message.author.id}`, currentBalance - randomc);
    message.channel.send(`You lost ${randomc} Coins.`);
  }
}

Upvotes: 1

Related Questions