简体   繁体   English

如何在 discord.js 中显示提及用户的角色

[英]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.我为 discord 创建了一个配置文件命令。它需要显示用户的角色。 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.我知道如何显示消息作者的角色,但显示消息作者角色的相同方法不适用于 ping 成员。

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 .第一个有效,因为它映射了GuildMember的角色。 The second one doesn't work because you try to get the roles of a User , but only GuildMember s have roles.第二个不起作用,因为您试图获得User的角色,但只有GuildMember具有角色。

To fix this, you can use message.mentions.members.first() instead as message.mentions.members returns a GuildMember :要解决此问题,您可以使用message.mentions.members.first()而不是message.mentions.members返回GuildMember

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

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

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

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