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:
JavaScript
x
46
46
1
const { MessageEmbed } = require('discord.js');
2
const Database = require('@replit/database');
3
const db = new Database();
4
5
module.exports = {
6
name: 'deposit',
7
aliases: ['dep'],
8
category: 'Economy',
9
description: 'Deposits all or a amount of balance into the bank.',
10
usage: 'deposit',
11
userperms: [],
12
botperms: [],
13
run: async (client, message, args, prefix) => {
14
if (message.author.bot) return;
15
16
let walletBalance = await db.get(`wallet_${message.author.id}`)
17
let bankBalance = await db.get(`bank_${message.author.id}`)
18
19
if (walletBalance === null) walletBalance = 0
20
if (bankBalance === null) bankBalance = 0
21
22
console.log(walletBalance, bankBalance)
23
24
let amount = args[0]
25
26
if (!amount) amount = walletBalance
27
28
if (walletBalance == 0) return message.reply('You don't have any money!')
29
30
if (amount) {
31
if (isNaN(amount)) {
32
if (amount.toLowerCase().includes('all')) amount = walletBalance;
33
}
34
if (isNaN(amount)) return message.reply('The amount must be a number!')
35
if (amount > walletBalance) return message.reply('You don't have enough balance in your wallet to do this!')
36
if (amount) {
37
await db.set(`wallet_${message.author.id}`, walletBalance - amount).then(
38
await db.set(`bank_${message.author.id}`, bankBalance + amount).then(
39
message.reply(`Successfully deposited ${amount.toLocaleString()} into your bank!`)
40
)
41
)
42
}
43
}
44
}
45
}
46
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:
JavaScript
1
3
1
await db.set(`wallet_${message.author.id}`, parseInt(walletBalance) - parseInt(amount));
2
await db.set(`bank_${message.author.id}`, parseInt(bankBalance) + parseInt(amount));
3
Full Example:
JavaScript
1
47
47
1
const { MessageEmbed } = require('discord.js');
2
const Database = require('@replit/database');
3
const db = new Database();
4
5
module.exports = {
6
name: 'deposit',
7
aliases: ['dep'],
8
category: 'Economy',
9
description: 'Deposits all or a amount of balance into the bank.',
10
usage: 'deposit',
11
userperms: [],
12
botperms: [],
13
run: async (client, message, args, prefix) => {
14
if (message.author.bot) return;
15
16
// Get balances and ensure they are number values
17
let walletBalance = parseInt(await db.get(`wallet_${message.author.id}`))
18
let bankBalance = parseInt(await db.get(`bank_${message.author.id}`))
19
20
if (!walletBalance) walletBalance = 0
21
if (!bankBalance) bankBalance = 0
22
23
console.log(walletBalance, bankBalance)
24
25
let amount = args[0]
26
27
if (!amount) amount = walletBalance
28
amount = parseInt(amount); // Ensure the amount is a number value
29
if (walletBalance == 0)
30
return message.reply('You don't have any money!')
31
32
if (isNaN(amount)) {
33
if (amount.toLowerCase().includes('all'))
34
amount = walletBalance;
35
}
36
if (isNaN(amount))
37
return message.reply('The amount must be a number!')
38
if (amount > walletBalance)
39
return message.reply('You don't have enough balance in your wallet to do this!')
40
if (amount) {
41
await db.set(`wallet_${message.author.id}`, walletBalance - amount);
42
await db.set(`bank_${message.author.id}`, bankBalance + amount);
43
message.reply(`Successfully deposited ${amount.toLocaleString()} into your bank!`);
44
}
45
}
46
}
47