【发布时间】:2019-11-17 14:00:27
【问题描述】:
这是在我的 index.js 文件中。 我的机器人有一个
类型错误:无法读取未定义的属性“id”
即使代码之前可以运行。
流程基本上是:
- 消息事件发生
- 获取消息所在的公会 webhook
- 然后对于每个 webhook:
- 检查 webhook 的名称。
- 检查所有者 ID 并查看它是否与机器人的 ID 相同
- 检查 webhook 是否在发送消息的位置
- 它会与表情符号发生反应。
问题是它不知道 webhook.owner.id 是什么
我将 webhook 与其他错误的 webhook 混合在一起。
我的代码要么什么都不做,要么在控制台中出现错误。
稍微改变一下 if() 语句。有时会发生错误或什么也没有发生。
添加和删除!在webhook.owner.id
doopliss.on('message', async (message) => {
const webhooks = await message.guild.fetchWebhooks();
await webhooks.forEach(async webhook => {
if(message.author.id == doopliss.user.id)
return //checks if author is me(bot)
else if(message.author.bot)
return //checks if author is a bot
else if(webhook.name == `Marker`)
return //checks if webhook name is "Marker"
else if(webhook.owner.id !== doopliss.user.id)
return //checks if the webhook owner id equals the bot's id
else if(message.channel.id == webhook.channelID)
return //checks if the channel ID is equal to the webhook channel's ID.
else
var thisWord = ">groc";
if(!message.content.includes(thisWord))
return
else
var thatWord = ">sc";
if(!message.content.includes(thatWord))
return
else
message.react(doopliss.emojis.find(emoji => emoji.id === "596458828011405334")) //approve
.then(() => message.react(doopliss.emojis.find(emoji => emoji.id === "596458827994497024"))) //deny
.catch(() => console.error('One of the emojis failed to react.'));
})})
我希望输出是机器人在对每条消息做出反应之前检查所有内容,但实际结果是机器人要么什么都不做,要么在控制台中吐出一个错误。前面的 if() 语句之一肯定是假的,但我不知道是哪一个。
【问题讨论】:
-
webhook 是否名为“Marker”?因为如果是这种情况,您将返回函数而不执行。同样,如果 message.channel.id 等于您的 webhook.channelID,则返回该函数,我认为这不是您想要的,您可以在 Typeeror 发生时添加该行吗?
-
console.log(webhook.owner)的结果是什么?根据文档,它可以是一个对象而不是一个用户,但在我的经验中我从未见过。 -
JackRed 为您的回复,这是代码中的第 10 行。
else if(webhook.owner.id !== doopliss.user.id) return //checks if the webhook owner id equals the bot's id -
另外,对于您的问题,Slothiful,它以用户身份返回,我发现一个错误,我忘记添加一个!在我的 webhook 名称代码中,所以它有另一个 webhook,可能是发生错误的原因。
-
@slothiful SomePerson:使用“@”来提及某人,就像我提到 slothiful 所做的那样
标签: javascript discord.js commando