正在查找另一个用户的平衡



我有一个机器人的命令,它用mongoDB获取另一个用户的余额(经济(-目前要检查你自己的余额,你必须这样做!ponyo余额(你自己的标签(,但它很乏味,所以我做了它,当没有用户被标记时,它会检查作者的余额。然而,我在做这件事时遇到了一些麻烦,这是错误的。以下是错误和代码。

client.on('messageCreate', async message => {
if (message.author.bot) return;
let member = message.mentions.members.first();
if (member) {
if (message.content.match('!ponyo balance') && profileSchema.findOne({ memberId: member.id, guildId: member.guild.id })) {
console.log('trying to execute balance.createBalance() with the user id: ' + member.id)
const profileBalance = await balance.createBalance(member);
console.log(`profileBalance: ${profileBalance}`)
await message.reply(`${message.mentions.members.first()} has ${profileBalance.coins} coins! :D`);
}
} else {
if (message.content.match('!ponyo balance') && profileSchema.findOne({ memberId: member.id, guildId: member.guild.id })) {
console.log('Trying to execute balance.createBalance() with the user id: ' + message.author.id)
let member1 = message.author
await balance.createBalance(member1);
console.log(`profileBalance: ${profileBalance}`)
await message.reply(`${message.author.tag()} has ${profileBalance.coins} coins!`)
}}
})```

您实际上没有包含收到的错误消息,这可能是一个错误,因为您说过要发布它,所以我不确定您的错误是什么。无论如何,您的命令对消息作者无效,因为在进入member不存在的条件后,您将使用member的属性,因为member是一个虚假值,这会引发一个错误。您的条件结构如下:

if (member) {
// code for getting the other member's balance
} else {
// if this code is executing, it means member is undefined, so you can't use member
}

因此,在else语句中,这一行将不起作用,并将抛出一个错误:

if (message.content.match('!ponyo balance') && profileSchema.findOne({ memberId: member.id, guildId: member.guild.id })) {

我看到您已经为消息作者分配了另一个变量member,所以请尝试删除这一行。如果你确实需要这一行;成员";具有message.member的变量,例如

if (message.content.match('!ponyo balance') && profileSchema.findOne({ memberId: message.member.id, guildId: message.guild.id })) {

最新更新