Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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.`);
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.`);
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda