首页 > 解决方案 > 如何显示将用户 ID 添加到 const 的消息作者头像

问题描述

我尝试将此代码用于 avatar 命令以使用外部服务器的 id 我可以使用 .avatar @mention || .avatar id 但显示作者头像的 .avatar 不起作用,出现错误。

(node:9656) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'id' of undefined
    at Object.User (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\util\Constants.js:109:16)
    at RESTMethods.getUser (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\client\rest\RESTMethods.js:406:51)
    at Client.fetchUser (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\client\Client.js:319:30)
    at Client.<anonymous> (C:\Users\zakaria\Desktop\test1011-master\bot.js:963:31)
    at Client.emit (events.js:327:22)
    at MessageCreateHandler.handle (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:108:65)
    at WebSocketConnection.onPacket (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:336:35)
    at WebSocketConnection.onMessage (C:\Users\zakaria\Desktop\test1011-master\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:299:17)
    at WebSocket.onMessage (C:\Users\zakaria\Desktop\test1011-master\node_modules\ws\lib\event-target.js:120:16)
(node:9656) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9656) [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.

我的代码:

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    if(command === 'avatar') {
        const userid = await client.fetchUser(args[0]);
        const User = message.mentions.users.first() || userid || message.author ;
        const embed = new Discord.RichEmbed()
            .setColor('#000000')
            .setAuthor(`${User.username}#${User.discriminator}`, User.displayAvatarURL)
            .setDescription(`[Avatar Link](${User.avatarURL})`)
            .setImage(User.displayAvatarURL)
            .setFooter(`Requested By ${message.author.username}#${message.author.discriminator}`, message.author.displayAvatarURL);
        message.channel.send(embed);
    } 

标签: node.jsdiscord.js

解决方案


此错误源自discord.js 源代码中的这一行:

if (userID.id) userID = userID.id;

userID在这种情况下,直接从fetchUser(args[0]). 这意味着你args[0]是未定义的,但你还是尝试client.fetchUser(undefined)了,discord.js 抛出了一个神秘的错误,因为它没有检查用户在fetchUser().

要解决此问题,您只需要使用条件,这样您就不会尝试发出无效请求。我建议这样的事情:

if(command === 'avatar') {
    const User = message.mentions.users.first() ||
                 (args[0] && await client.fetchUser(args[0])) ||
                 message.author;
    const embed = ...
}

推荐阅读