Search code examples
javascriptdiscord.jscommando

How to make a bot react to messages under a certain channel, with a certain webhook


This is in my index.js file. My bot is having a

Typeerror: Cannot read property 'id' of undefined

even though the code was working earlier.

The flow is basically:

  • A message event happens
  • It get the guild's webhooks where the message occurs
  • then for each webhook it:
    • check for the name of the webhook.
    • check for the owner ID and see if it's the same to bot's ID
    • check if the webhook is in where the message was sent
  • It reacts with emojis.

The problem is that it doesn't know what webhook.owner.id is
I have the webhook mixed in with other wrong webhooks.

Either my code does nothing or puts an error in console.

Changing around the if() statements a little. Sometimes the error occurs or nothing happens.

Adding and removing ! in the 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.'));
})})

I expect the output to be the bot checks everything before reacting to every message, but the actual result is the bot either does nothing or spits out an error in console. One of the earlier if() statements must be false, but I don't know which one.


Solution

  • This is the code I managed to improve.

    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.'));
    })})
    

    What I changed

    • Added in console.logs to troubleshoot the problem
    • Since you're checking if your bot made the webhook, I added in a feature to check for bots
    • I added a couple of !'s. Some values need to be !== or (!var). For booleans and some strings.

    This should fix your bot and get rid of the typeerror, it matters on how you arrange the if statements.