最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How would you define a user's color in discord.js? - Stack Overflow

programmeradmin0浏览0评论

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.

Share Improve this question asked Oct 23, 2018 at 3:56 TobyhnTobyhn 3802 gold badges5 silver badges17 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 6

A 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

发布评论

评论列表(0)

  1. 暂无评论