Esando
Esando

Reputation: 23

TypeError: Cannot read property 'user' of null | discord.js

So I added this code to my bot, and it stopped working.

client.on("guildCreate", guild => {
    console.log(`Joined Server | Name: ${guild.name} | ID: ${guild.id} | Member Count: ${guild.memberCount} | Owner: ${guild.owner.user.tag}`);
}

The bot can no longer read property 'user', anyone know why?

Upvotes: 2

Views: 295

Answers (1)

Itamar S
Itamar S

Reputation: 1565

A common error with guild owners is that the Owner of a guild is not cached. A simple fix to this is fetching the member object, using the guild#ownerID method, and using it for our needs.

client.on("guildCreate", async guild => {
  const owner = await guild.members.fetch(guild.ownerID)
  console.log(`Joined Server | Name: ${guild.name} | ID: ${guild.id} | Member Count: ${guild.memberCount} | Owner: ${owner.user.tag}`)
}

Upvotes: 3

Related Questions