如何使漫游器对具有特定Webhook的特定频道下的消息做出反应

时间:2019-07-08 04:24:03

标签: javascript discord.js commando

这是在我的index.js文件中。 我的机器人有一个

  

Typeerror:无法读取未定义的属性“ id”

即使代码可以更早地工作。

流程基本上是:

  • 发生消息事件
  • 它获得了发生消息的公会的网络钩子
  • 然后为每个Web钩上它:
    • 检查Webhook的名称。
    • 检查所有者ID,并查看其与机器人的ID是否相同
    • 检查Webhook是否在发送消息的地方
  • 它会与表情符号反应。

问题是它不知道什么是webhook.owner.id
我把Webhook和其他错误的Webhooks混在一起了。

我的代码不执行任何操作或在控制台中出现错误。

稍微修改一下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.'));
})})

我希望输出是bot在对每条消息作出反应之前检查所有内容,但是实际结果是bot不执行任何操作或在控制台中吐出错误。较早的if()语句之一必须为false,但我不知道是哪个。

1 个答案:

答案 0 :(得分:0)

这是我设法改进的代码。

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中添加以解决问题
  • 由于您要检查机器人是否进行了网络挂接,因此我添加了一项功能来检查机器人
  • 我加了几个!。某些值必须为!==(!var)。对于布尔值和一些字符串。

这应该可以修复您的机器人并摆脱typeerror,这与您如何安排if语句很重要。

相关问题