简体   繁体   中英

Is there a setCurrency function for a discord bot using discord.js?

As the Discord.js guide helps you with two functions, currency.add() and currency.getBalance() , there should be a currency.setBalance() function. Here is the code for the two functions:

Reflect.defineProperty(currency, 'add', {
    value: async function add(id, amount) {
        const user = currency.get(id);
        if (user) {
            user.balance += Number(amount);
            return user.save();
        }
        const newUser = await Users.create({ user_id: id, balance: amount });
        currency.set(id, newUser);
        return newUser;
    },
});

Reflect.defineProperty(currency, 'getBalance', {
    value: function getBalance(id) {
        const user = currency.get(id);
        return user ? user.balance : 0;
    },
});

Could anybody help with making a setBalance function? I appreciate all help!

I have adapted the add() function to set the amount instead of adding a certain amount:

Reflect.defineProperty(currency, 'setBalance', {
    value: async function setBalance(id, amount) {
        const user = currency.get(id);
        if (user) {
            user.balance = Number(amount);
            return user.save();
        }
        const newUser = await Users.create({ user_id: id, balance: amount });
        currency.set(id, newUser);
        return newUser;
    },
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM