#Get user's color

1 messages · Page 1 of 1 (latest)

knotty coral
#

What would be the easiest way to get the color of a user's (or bot) highest colored role?

naive tusk
#

This should work just fine:

[Command("test")]
        public async Task GrabbingUserRoleColor(SocketGuildUser user){

            // grabs the highest role and the color for it
            var userHighestRoleColor = user.Roles.OrderBy(r => r.Position).Last().Color;

            //returns the hex code for it
            await ReplyAsync($"This user's color is {userHighestRoleColor}");

        }
knotty coral
#

sometimes the highest role isn't necessarily the color determinant role

naive tusk
#

just write a conditional statement that if the highest role color is == Default then create a new IEnumerable that filters a sequence of values based on a predicate

naive tusk
# knotty coral sometimes the highest role isn't necessarily the color determinant role

Here you are:

[Command("test")]
        public async Task GrabbingUserRoleColor(SocketGuildUser user){

            // filters out any roles that are default color
            var filterOutDefault = user.Roles.Where(r => r.Color != Color.Default);

            // grabs the highest role and the color for it
            var userHighestRoleColor = filterOutDefault.OrderBy(r => r.Position).Last().Color;

            // returns the hex code for it
            await ReplyAsync($"This user's color is {userHighestRoleColor}");

        }
#

no conditional needed, i just filtered out the roles that are default color