with my bot I have withdraw and deposit setup but I have a slight bug or issue may have coded it slightly wrong but when I try to -deposit 100 or any number it says deposit must be a hole number.
const profileModel = require("../models/profileSchema");
module.exports = {
name: "deposit",
aliases: ["dep"],
permissions: [],
description: "Deposit gold into your bank!",
async execute(message, args, cmd, client, discord, profileData) {
const amount = args[0];
if (amount % 1 != 0 || amount <= 0) return message.channel.send("Deposit amount must be a whole number");
try {
if (amount > profileData.gold) return message.channel.send(`You don't have that amount of gold to deposit`);
await profileModel.findOneAndUpdate(
{
userID: message.author.id,
},
{
$inc: {
gold: -amount,
bank: amount,
},
}
);
return message.channel.send(`You deposited ${amount} of gold into your bank`);
} catch (err) {
console.log(err);
}
},
};
both withdraw and deposit are setup the same just changes in the amount bit instead of - its
CodePudding user response:
There's a nicest way to do a withdraw
and deposit
command, and I don't know why you need to get a whole number to make it that way. I re-modified your code to something easiest and cleaner way.
const amount = parseFloat(args[0]); // Your current code: const amount = args[0];
//if (amount % 1 != 0 || amount <= 0) return message.channel.send("Deposit amount must be a whole number");
try {
// > = Greater than and < = Less than.
if (amount > profileData.gold) return message.channel.send(`You don't have that amount of gold to deposit`);
// await profileModel.findOneAndUpdate(
// {
// userID: message.author.id,
// },
// {
// $inc: {
// gold: -amount,
// bank: amount,
// },
// }
// );
await profileModel.findOneAndUpdate({
userID: message.author.id
}, {$inc: {gold: amount}}, async(err, data) => {
if(data) {
//No data returns a message
} else {
data.bank = amount,
data.gold -= amount
return message.channel.send(`You deposited ${amount} of gold into your bank`);
}
})
} catch (err) {
console.log(err);
}
parseFloat()
1e0 = 1
1e1 = 10
1e2 = 100
1e3 = 1,000
1e4 = 10,000
and so on
CodePudding user response:
You want
amount % 1 == 0
instead of
amount % 1 != 0
if amount is not a whole number, amount % 1
will evaluate to the remainder, otherwise if there is no remainder, amount % 1
will evaluate to 0