提问者:小点点

discord.js获取服务器所有者用户标记返回null


我正在尝试让我的bot发送一个嵌入,当bot加入一个服务器时,嵌入应该包含服务器所有者的名字,但它返回null并且不起作用,这给了我错误类型错误:无法读取null的属性'user'

guild.owner.user.username

完整的代码

client.on("guildCreate", (guild) => {        
    const EmbedJoin = new Discord.MessageEmbed()
    .setColor('GREEN')
    .setTitle(`Joined Guild: ${guild.name}!`)
    .setDescription(`Guild owner: ${guild.owner.user.username}\nMembers: ${guild.memberCount}`)
    .setTimestamp()
    console.log(`Joined New Guild: ${guild.name}`);
    client.channels.cache.get(`794483893977088002`).send(EmbedJoin)
});

共1个答案

匿名用户

您可以像这样缓存它:Await client.users.fetch(Guild.OwnerId);

因此,您的代码的一个固定副本:

client.on("guildCreate", async (guild) => {        
    await client.users.fetch(guild.ownerID);
    const EmbedJoin = new Discord.MessageEmbed()
    .setColor('GREEN')
    .setTitle(`Joined Guild: ${guild.name}!`)
    .setDescription(`Guild owner: ${guild.owner.user.username}\nMembers: ${guild.memberCount}`)
    .setTimestamp()
    console.log(`Joined New Guild: ${guild.name}`);
    client.channels.cache.get(`794483893977088002`).send(EmbedJoin)
});