简体   繁体   中英

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?

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}`)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM