I am making a mand that gives a user their stats inside of an embed. I would like the embed, instead of being the same color for everyone, to be the user's current color on the server (as in color given by role). Is there any way to do this?
My code:
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username, message.author.displayAvatarURL)
.setColor('USER COLOR HERE')
.addField('XP:', userXp, true)
.addField('XP Reserve:', userXpReserve, true)
.addField('Rank:', "working on this part", false)
.setFooter(message.author.tag)
message.channel.send(embed);
I would like .setColor('USER COLOR HERE')
to be the color of the user who uses the mand.
I am making a mand that gives a user their stats inside of an embed. I would like the embed, instead of being the same color for everyone, to be the user's current color on the server (as in color given by role). Is there any way to do this?
My code:
let embed = new Discord.RichEmbed()
.setAuthor(message.author.username, message.author.displayAvatarURL)
.setColor('USER COLOR HERE')
.addField('XP:', userXp, true)
.addField('XP Reserve:', userXpReserve, true)
.addField('Rank:', "working on this part", false)
.setFooter(message.author.tag)
message.channel.send(embed);
I would like .setColor('USER COLOR HERE')
to be the color of the user who uses the mand.
- make a js variable user from server's user variable, and set color in your main database then do user.color in javascript, if you don't have user variable in your server side, please study authentication systems in node.js – user1735921 Commented Oct 23, 2018 at 4:34
-
@user1735921 The problem is, I have tried using many different binations of discord.js variables with
.color
at the end. It does not work. – Tobyhn Commented Oct 23, 2018 at 5:35
3 Answers
Reset to default 6A GuildMember has a .displayHexColor
property, thus
.setColor(message.member.displayHexColor)
would work. If you don't want a role with no colour (i.e. @everyone, or an invisible admin role), check if the colour equals #000000
. If that is the case, usually .hoistRole
will have a colour. .hoistRole
is the role that puts a member in a separate category in the member list.
let color = message.member.displayHexColor;
if (color == '#000000') color = message.member.hoistRole.hexColor;
// Embed
.setColor(color)
message.member.colorRole.color
this defines the color of the role defining the user's color
In 2021 I use following code:
function check_color ( message ) {
if ( message.member ) {
var role = message.member.roles.color
if ( ! role ) return "#ffffff"
else return role.hexColor
} else return = "#ffffff"
}
It first looks whether the message is from guild by looking if member is defined, and if so, it gets the role that is responsible for color. As in docs:
GuildMemberRoleManager
.color [READ-ONLY]
| The role of the member used to set their color
| Type: (Role)
If no role, returns white. If the message is a DM, also returns white.
Edit:
Why don't use GuildMember#displayColor
?
If the user has no role, it will return black. And checking, if the color is black, and then turning it white is not a good idea in my opinion, because it would kill the possibility that the role actually has black color