如何解决如何从 discord.js 中删除 [object Promise]?
我正在制作一个平衡系统,并创建了一个给定命令。每当我给某人一些钱(例如 10 美元)时,他们的余额都会显示为“10 美元 [object Promise]”。如何删除“[object Promise]”?
完整的命令代码:
if (command === 'give') {
if (!args[0]) return message.channel.send('how much are you gonna send?')
if (isNaN(args[0])) return message.channel.send('format: xd give [amount] [@user]')
const money = args[0];
const emb = new Discord.MessageEmbed()
.setColor('GRAY')
.setTitle('you gave them the $$')
.setDescription(`you gave ${message.mentions.users.first().username} the $$$$$$ (` + args[0] + ` to be exact)`)
.setTimestamp()
.setFooter('sent to pokimane');
message.channel.send(emb);
const money2 = moneyAmount - args[0];
if (isNaN(money2)) {
Number(money2);
}
moneydb.set(message.author.id,money2);
const uMA = moneydb.get(message.mentions.users.first().id);
const money3 = args[0] + uMA;
moneydb.set(message.mentions.users.first().id,money3);
}
// keyv connection
const moneydb = new Keyv('mysql://[user and pass]@localhost:3306/money');
即使是数据库最后也有[object promise]
解决方法
如果 moneydb
是出现在您的问题中的 Keyv 数据库,那么这行代码:
const uMA = moneydb.get(message.mentions.users.first().id);
向 uMA 承诺。您显然缺少这样的 await
:
const uMA = await moneydb.get(message.mentions.users.first().id);
所以,当你这样做时:
const money3 = args[0] + uMA;
而 uMA
是一个 promise,它试图将它们都变成一个字符串,然后你会得到包含 [object Promise]
的输出。
仅供参考,您可以从 Keyv 文档 here 中看到 .set()
和 .get()
都返回承诺。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。