简体   繁体   English

总是输(Discordjs V12)

[英]Always losing (Discordjs V12)

I have a bot in discord.js v12 and I made a piece of code for the bank.我在 discord.js v12 中有一个机器人,我为银行编写了一段代码。 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.`);
  }
}

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.这是因为您正在检查变量what等于字符串"1" ,但始终what integer 所以只有else块运行。 The number 1 and the string "1" is not the same thing.数字1和字符串"1"不是一回事。

Make sure you check if what === 1 instead and it will work properly:确保检查what === 1是否可以正常工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM