'Cannot read properties of null (reading 'me') (DISCORD JS)

  client.on('message', async message => {
let guild = message.guild
if (!guild.me.hasPermission("ADMINISTRATOR")) return
})

When my bot join a server, there is this error

if (!guild.me.hasPermission("ADMINISTRATOR")) return
               ^

TypeError: Cannot read properties of null (reading 'me')
    at Client.<anonymous> (/home/runner/Todoroki/index.js:236:16)```


Solution 1:[1]

Actually I found out how to do. I just added:

let guild = message.guild
if (!guild) return;
// rest of commands

Solution 2:[2]

That means that the command is runned on a server. Or you don't have the GUILDS intents enabled. simply

client.on('message',message =>{
  if(!message.guild) return;
  // Do whatever you want
})

Solution 3:[3]

Without much information, I could only deduce that me is null. To solve it, you could check if guild.me is truthy using Optional Chaining like so:

if (!guild?.me?.hasPermission ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anindya Dey
Solution 2 Malik Lahlou
Solution 3 MrMythical