类型错误:无法读取 null 的属性“user”

时间:2021-01-26 20:57:44

标签: javascript discord discord.js

目的:创建邀请并在机器人加入新公会后向公会所有者发送消息。

代码:错误位置

class FemaleDog < Animal
  default_scope -> { where(:type => "Dog", :gender => "Female") }
end

错误:

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

我尝试了其他方法来让公会所有者成为 (node:9168) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'user' of null 财产,但无济于事。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您是否尝试过:

let ThisGuild = client.guilds.cache.get(g => g.id === guild.id)
const getOwner = ThisGuild.member(ThisGuild.owner)

答案 1 :(得分:0)

感谢 james 指出并非所有属性都出现在所有端点中。

我想出了一个解决问题的解决方案:

const Owner = guild.ownerID;
const fetchOwner = client.users.fetch(Owner);

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