I’m adding a deposit command but when I try to add the amount into the bankBalance, it doesn’t add them correctly into integers, this may sound confusing, I’ll give you an example.
Example:
Command: ~deposit 100,
User balance: Wallet: 120, Bank: 0,
Output user balance: Wallet: 20, Bank: 0100
I wish this explains it, anyways here is my code:
const { MessageEmbed } = require('discord.js');
const Database = require('@replit/database');
const db = new Database();
module.exports = {
name: 'deposit',
aliases: ['dep'],
category: 'Economy',
description: 'Deposits all or a amount of balance into the bank.',
usage: 'deposit',
userperms: [],
botperms: [],
run: async (client, message, args, prefix) => {
if (message.author.bot) return;
let walletBalance = await db.get(`wallet_${message.author.id}`)
let bankBalance = await db.get(`bank_${message.author.id}`)
if (walletBalance === null) walletBalance = 0
if (bankBalance === null) bankBalance = 0
console.log(walletBalance, bankBalance)
let amount = args[0]
if (!amount) amount = walletBalance
if (walletBalance == 0) return message.reply('You don't have any money!')
if (amount) {
if (isNaN(amount)) {
if (amount.toLowerCase().includes('all')) amount = walletBalance;
}
if (isNaN(amount)) return message.reply('The amount must be a number!')
if (amount > walletBalance) return message.reply('You don't have enough balance in your wallet to do this!')
if (amount) {
await db.set(`wallet_${message.author.id}`, walletBalance - amount).then(
await db.set(`bank_${message.author.id}`, bankBalance + amount).then(
message.reply(`Successfully deposited ${amount.toLocaleString()} into your bank!`)
)
)
}
}
}
}
Note: I’m using replit
Advertisement
Answer
In your case, either the bankBalance or amount variable is a string causing them to both be converted into a string then appended to each other. A solution would be to use the parseInt function to convert both values to a number.
Example:
await db.set(`wallet_${message.author.id}`, parseInt(walletBalance) - parseInt(amount));
await db.set(`bank_${message.author.id}`, parseInt(bankBalance) + parseInt(amount));
Full Example:
const { MessageEmbed } = require('discord.js');
const Database = require('@replit/database');
const db = new Database();
module.exports = {
name: 'deposit',
aliases: ['dep'],
category: 'Economy',
description: 'Deposits all or a amount of balance into the bank.',
usage: 'deposit',
userperms: [],
botperms: [],
run: async (client, message, args, prefix) => {
if (message.author.bot) return;
// Get balances and ensure they are number values
let walletBalance = parseInt(await db.get(`wallet_${message.author.id}`))
let bankBalance = parseInt(await db.get(`bank_${message.author.id}`))
if (!walletBalance) walletBalance = 0
if (!bankBalance) bankBalance = 0
console.log(walletBalance, bankBalance)
let amount = args[0]
if (!amount) amount = walletBalance
amount = parseInt(amount); // Ensure the amount is a number value
if (walletBalance == 0)
return message.reply('You don't have any money!')
if (isNaN(amount)) {
if (amount.toLowerCase().includes('all'))
amount = walletBalance;
}
if (isNaN(amount))
return message.reply('The amount must be a number!')
if (amount > walletBalance)
return message.reply('You don't have enough balance in your wallet to do this!')
if (amount) {
await db.set(`wallet_${message.author.id}`, walletBalance - amount);
await db.set(`bank_${message.author.id}`, bankBalance + amount);
message.reply(`Successfully deposited ${amount.toLocaleString()} into your bank!`);
}
}
}