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

252
Visualizações
Not combining values in deposit command, using discord.js

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

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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!`);
        }
    }
}
about 4 years ago · Santiago Trujillo 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