如何使用特定的 webhook 使机器人对特定频道下的消息做出反应
How to make a bot react to messages under a certain channel, with a certain webhook
这在我的 index.js 文件中。
我的机器人有
Typeerror: Cannot read property 'id' of undefined
即使代码在早些时候工作。
流程基本是:
- 消息事件发生
- 它获取消息出现的行会的 webhooks
- 然后对于每个 webhook:
- 检查 webhook 的名称。
- 检查所有者 ID 并查看它是否与 bot 的 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() 语句之一必须为假,但我不知道是哪一个。
这是我设法改进的代码。
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
console.log(message.author.bot)
if(message.author.bot) return //checks if author is a bot
else
console.log(webhook.name)
if(webhook.name !== `Marker`) return //checks if webhook name is "Marker"
else
console.log(webhook.owner.bot)
if(webhook.owner.bot !== true) return //checks if the webhook owner id equals the bot's id
else
console.log(`Owner: ${webhook.owner.id} You: ${doopliss.user.id}`)
if(!webhook.owner.id == doopliss.user.id) return //checks if the webhook owner id equals the bot's id
else
console.log(`${message.channel.id} then we have ${webhook.channelID}`)
if(message.channel.id !== webhook.channelID) return //checks if the channel ID is equal to the webhook channel's ID.
else
console.log(`pog`)
var thisWord = ">groc";
if(message.content.includes(thisWord)) return
else
console.log(`pog`)
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.'));
})})
我改变了什么
- 添加到 console.logs 以解决问题
- 由于您要检查您的机器人是否创建了 webhook,我添加了一个功能来检查机器人
- 我添加了几个 !。某些值需要为
!==
或 (!var)
。对于布尔值和一些字符串。
这应该可以修复您的机器人并消除类型错误,这对您如何安排 if 语句很重要。
这在我的 index.js 文件中。 我的机器人有
Typeerror: Cannot read property 'id' of undefined
即使代码在早些时候工作。
流程基本是:
- 消息事件发生
- 它获取消息出现的行会的 webhooks
- 然后对于每个 webhook:
- 检查 webhook 的名称。
- 检查所有者 ID 并查看它是否与 bot 的 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() 语句之一必须为假,但我不知道是哪一个。
这是我设法改进的代码。
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
console.log(message.author.bot)
if(message.author.bot) return //checks if author is a bot
else
console.log(webhook.name)
if(webhook.name !== `Marker`) return //checks if webhook name is "Marker"
else
console.log(webhook.owner.bot)
if(webhook.owner.bot !== true) return //checks if the webhook owner id equals the bot's id
else
console.log(`Owner: ${webhook.owner.id} You: ${doopliss.user.id}`)
if(!webhook.owner.id == doopliss.user.id) return //checks if the webhook owner id equals the bot's id
else
console.log(`${message.channel.id} then we have ${webhook.channelID}`)
if(message.channel.id !== webhook.channelID) return //checks if the channel ID is equal to the webhook channel's ID.
else
console.log(`pog`)
var thisWord = ">groc";
if(message.content.includes(thisWord)) return
else
console.log(`pog`)
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.'));
})})
我改变了什么
- 添加到 console.logs 以解决问题
- 由于您要检查您的机器人是否创建了 webhook,我添加了一个功能来检查机器人
- 我添加了几个 !。某些值需要为
!==
或(!var)
。对于布尔值和一些字符串。
这应该可以修复您的机器人并消除类型错误,这对您如何安排 if 语句很重要。