简体   繁体   中英

How to show roles of a mentioned user in discord.js

I made a profile command for discord. It needs to show the roles of a user. I know how to show roles of message author but the same method for showing roles of message author doesn't work for pinged members.

I'll show my code to make it more clear:

// this one works as expected
message.member.roles.cache.map((role) => role.name).join(", ")
// but this one doesn't work
let pingedUser = message.mentions.users.first(); 
pingedUser.roles.cache.map((role) => role.name).join(", ")

The first one works because it maps the roles of a GuildMember . The second one doesn't work because you try to get the roles of a User , but only GuildMember s have roles.

To fix this, you can use message.mentions.members.first() instead as message.mentions.members returns a GuildMember :

let pingedMember = message.mentions.members.first();

pingedMember.roles.cache.map((role) => role.name).join(', ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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