Search code examples
discorddiscord.js

TypeError: Cannot read properties of null (reading 'permissions') discord.js V13


I try to do a anti link bot discord that only who have the VIEW_AUDIT_LOG permission can send link this is my code :

client.on("messageCreate", message => {
    const { member } = message;
    let blacklisted = ['http://', 'www.', 'https://'];
    let foundInText = false;

    if(!member.permissions.has(Permissions.FLAGS.VIEW_AUDIT_LOG)) {
        for (var i in blacklisted) {
      
            if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
          }
          if (foundInText) {
              const logembed = new MessageEmbed()
              .setColor("DARK_AQUA")
              .addField(' with message :', ` ${message.content} `, true)
              
              if(!message.member.roles.cache.has("929434049011941386")) {
                  message.guild.channels.cache.get('941704496185221221').send({ content: `<@${message.author.id}> tried to send links in <#${message.channel.id}>`, embeds: [logembed] });;
                  message.delete();
                  message.channel.send("No links here, " + `${message.author}`);
              }
          }
    }

});

but it give this error when he delete a message with link :

   if(!member.permissions.has(Permissions.FLAGS.VIEW_AUDIT_LOG)) {
               ^

TypeError: Cannot read properties of null (reading 'permissions')

Solution

  • ok so here's my guess....you're hitting this event handler twice or more times...the first time is the message the user typed. you will get their member info, if you console.log(message.member). The second time through, you'll be getting another event, which is the message(s) you send as a handler.

    So the easy solution is to add something like this at or near the top of this event handler:

        if (message.author.bot) return;
    

    this basically tests to see if it's a bot or not that's calling your event. It's also good hygiene to ensure you're not accepting/reacting to other bots, which could result in spam you likely don't want.