我试图将此代码用于avatar命令,以便甚至可以使用.avatar @mention ||甚至使用外部服务器的ID。 .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);
}
答案 0 :(得分:1)
此错误源自discord.js源文件中的this line:
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 = ...
}