简体   繁体   English

discord.js 中的头像命令未提及

[英]Avatar command in discord.js not picking up mentions

this thread did not solve my problem thats why im asking again.该线程没有解决我的问题,这就是我再次询问的原因。 I am trying to make a discord.js avatar command, and the mentioning portion doesn't work correctly 我正在尝试制作 discord.js 头像命令,但提及部分无法正常工作

when i use my avatar command without mentioning the user it shows my avatar as intended but when i mention a user it says "undefined's avatar" and shows no avatar当我使用我的头像命令而不提及用户时,它会按预期显示我的头像,但是当我提到用户时,它会显示“未定义的头像”并且不显示头像

module.exports.run = async (Client, msg, args) => {
    let member = msg.mentions.members.first() || msg.author

    let embed = new Discord.RichEmbed()
    .setTitle(`${member.username}'s Avatar`)
    .setImage(member.avatarURL)
    .setColor(0x233f23);

    msg.channel.send(embed)

}

it looks like its not finding the mentioned user but i have no idea why看起来它没有找到提到的用户,但我不知道为什么

before i had used在我使用之前

if (!member) member = msg.author

but that still gave me the same problem so i applied the fix from the thread i linked above and its still the same thing.但这仍然给了我同样的问题,所以我从上面链接的线程中应用了修复程序,它仍然是同样的事情。

When you define member in let member = msg.mentions.members.first() you are working with the class GuildMember , when you define it through let member = msg.author , you are working with the class User .当您在let member = msg.mentions.members.first()中定义成员时,您正在使用 class GuildMember ,当您通过let member = msg.author定义它时,您正在使用 class User Be sure to check in the documentation what you are working with.请务必检查您正在使用的文档

Now, when you get the member from the mention both username and avatarURL are null because those properties don't exist in the member class, as an example, to get a user's username, you would need to first go to the user property in GuildMember like this, let username = member.user.username you could alternatively get the member nickname, witch is a property that exists in Member .现在,当您从提及中获取成员时, usernameavatarURL都是 null,因为这些属性在成员 class 中不存在,例如,要获取用户的用户名,您需要首先将 Z34D1F91FB2E514B856BZFAB1 中的user属性GuildMember像这样, let username = member.user.username你也可以得到成员昵称,witch 是存在于Member中的一个属性。

After all of this, here's your code with the fixes I just described in mind:毕竟,这是您的代码,其中包含我刚刚描述的修复程序:

module.exports.run = async (Client, msg, args) => {
    let member = msg.mentions.members.first() || msg.member // Changed from msg.author so that we can work with the same class in both cases

    let embed = new Discord.RichEmbed()
        .setTitle(`${member.user.username}'s Avatar`)
        .setImage(member.user.avatarURL)
        .setColor(0x233f23);

    msg.channel.send(embed)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM