Tengo un bot en discord.js v12 e hice un código para el banco. Hice monedas al azar, pero el problema es que siempre pierdo monedas y nunca recibo/gano.
Quiero que sea para que pierdas o ganes. Aquí está mi código:
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.`); } }Es porque está verificando si what variable es igual a la cadena "1" pero siempre es un número entero, por what que solo se ejecuta el bloque else . El número 1 y la cadena "1" no son lo mismo.
Asegúrese de verificar si what === 1 en su lugar y funcionará correctamente:
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.`); } }