如何从 discord.js 中删除 [object Promise]?
How can I remove [object Promise] from discord.js?
我正在制作一个平衡系统,我创建了一个 give 命令。每当我给某人一些钱(例如 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]
phpMyAdmin
screenshot
如果 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
是一个承诺,它试图将它们都变成一个字符串,并且你得到包含 [object Promise]
.
的输出
仅供参考,您可以从 Keyv 文档 here 中看到 .set()
和 .get()
return 都承诺。
我正在制作一个平衡系统,我创建了一个 give 命令。每当我给某人一些钱(例如 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]
phpMyAdmin screenshot
如果 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
是一个承诺,它试图将它们都变成一个字符串,并且你得到包含 [object Promise]
.
仅供参考,您可以从 Keyv 文档 here 中看到 .set()
和 .get()
return 都承诺。