Im kind of new to discord.js, and I'm building a bot for the Winter season. I'm building a feature called !gift
, which will let you gift stuff to ppl virtually. ( Not with money or anything). I am using Keyv to store how many gifts each user has received so far. Here is my code -
client.on('message', message => {
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === 'gift') {
let sender = message.author.username;
let receiver = message.mentions.users.first();
let [sender1, gift] = args;
if (keyv.get(receiver) === undefined) {
keyv.set(receiver, giftcount);
}
let prevvalue = keyv.get(receiver).then(prevvalue => {
return;
});
let currentval = keyv.set(receiver, prevvalue+=1).then(currentval => {
return;
});
};
//message.channel.send(currentval);
message.channel.send(sender + ' has gifted ' + receiver + ' ' + gift + '! They have ' + currentval + ' gifts right now.');
});
Before I added .then()
, when it sent the message, it said
They have [object Promise] gifts right now.
My friend suggested I add .then()
to fix that, and I did, but I have no idea where to put it, and neither did he.
When I run the above code, I get the following error -
ReferenceError: currentval is not defined
Could someone please help me?
[object Promise]
isn't an error; it's the string form of a pending promise. Your friend is correct, you'd need to usePromise#then()
, and use the variable provided in that function.Also, use template literals to improve readability.
Guides to understand promises: