Type Error: Cannot read property 'user' of null

Purpose: To create an invite and message the guild owner once bot has joined a new guild.

Code: Error Location

client.on("guildCreate", async guild => {
    // This event triggers when the bot joins a guild.
    console.log(``)
    console.log(greenBright(`[GUILD JOINED] ${guild.name} | [ID] ${guild.id} | [ (+) MEMBERCOUNT: ${guild.memberCount}]`));
    console.log(``)

    const getOwner = guild.owner.user;
    const InviteLogGuild = client.guilds.cache.find(g => g.id === "GUILD_ID");
    const InviteLogChannel = InviteLogGuild.channels.cache.find(ch => ch.id === "CHANNEL_ID");
    const getChennel = guild.channels.cache.first();

    getOwner.send(`Thank you for allowing lovell to  be added to your server, ${guild.name}, ${guild.owner.user.username} !\nPrefix:\`${prefix}\`\nAll commands are displayed by saying \`${prefix}help\`\nFeel free to join lovell server: ${MainServer}`)
        .then(console.log(magentaBright(`\n[GUILD OWNER MESSAGED]\n`)))
        .catch(() => {
            console.error(redBright(`\n[GUILD OWNER MESSAGE DEINIED]\n`));
        });
...
});

Error:

(node:9168) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'user' of null

I’ve tried other ways to getting the guild owner as a Discord#User property but to no avail. Any help would be appreciated!

52 thoughts on “Type Error: Cannot read property 'user' of null”

Leave a Comment