Discord.js欢迎消息不显示头像

时间:2020-03-31 09:13:40

标签: node.js discord discord.js

我有以下问题。该代码有效,但是如果用户没有个人资料照片,则显示错误,并且没有发送嵌入代码。

代码:

   client.on('guildMemberRemove', member => {   
     welcome = JSON.parse(fs.readFileSync("./commands/welcome.json", "utf8"))
     if(!welcome[member.guild.id]) return
       if(!welcome[member.guild.id].channel) return

        const welcome1 = new Discord.RichEmbed()
        .setTitle(`${member.user.username} left the server`)
        .setDescription("Please come back, it was such a good time with you :(")
        .setColor("RED")
        .setThumbnail(`${member.user.avatarURL}`)

        member.guild.channels.get(welcome[member.guild.id].channel).send(welcome1)

     })

错误:

    (node:16164) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.thumbnail.url: Not a well formed URL.
    at C:\Users\Bendix\Desktop\MemeBot\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15
    at C:\Users\Bendix\Desktop\MemeBot\node_modules\snekfetch\src\index.js:215:21
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:16164) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:16164) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

     welcome = JSON.parse(fs.readFileSync("./commands/welcome.json", "utf8"))
     if(!welcome[member.guild.id]) return
       if(!welcome[member.guild.id].channel) return

        const welcome1 = new Discord.RichEmbed()
        .setTitle(`${member.user.username} left the server`)
        .setDescription("Please come back, it was such a good time with you :(")
        .setColor("RED")
        .setThumbnail(`${member.user.displayAvatarURL()}`)

        member.guild.channels.get(welcome[member.guild.id].channel).send(welcome1)

     })```

答案 1 :(得分:1)

这是因为默认头像不是存储在.avatarURL下,而是存储在.defaultAvatarURL下。因此,您必须执行以下操作:

let url = member.user.AvatarURL == undefined ? member.user.defaultAvatarURL : member.user.avatarURL

const welcome1 = new Discord.RichEmbed()
        .setTitle(`${member.user.username} left the server`)
        .setDescription("Please come back, it was such a good time with you :(")
        .setColor("RED")
        .setThumbnail(url)

        member.guild.channels.get(welcome[member.guild.id].channel).send(welcome1)

通过这种方式,它使用单行if语句来检查用户是否具有自定义头像,如果没有,则将其替换为默认头像。