Danny
Danny

Reputation: 69

How to determine if user doesn't contain a role

I'm working on some new commands in my Discord BOT and I am trying to determine the difference between a user who has an Admin role and a user who has a Founder & Admin Role.

If the user roles contains only Admin and not Founder then they are classed as "CheckUserIsAdminOnly". I use the below code to currently check if the user of the command is an Admin so I've now tried to adapt this to say if the user has an "Admin" role but doesn't contain a "Founder" role.

I feel I am missing something simple here but I cannot figure out my mistake

return (fullContextUser.Roles.Where(x => 
x.Name == "Admin" || 
x.Name == "Head Recruiter" && 
x.Name != "Founder").Count() > 0);

This code still returns back as true when a user has a role called "Admin" and "Founder"

Upvotes: 2

Views: 1321

Answers (2)

NineBerry
NineBerry

Reputation: 28499

Roles is a list of roles. You cannot make a statement about the list by only looking at individual roles and their names as you do.

The expression in the Where will be applied to each individual role in the list.

Check for the existence of the roles and then combine that information.

bool hasAdminRole = fullContextUser.Roles.Any(x => x.Name == "Admin");
bool hasFounderRole = fullContextUser.Roles.Any(x => x.Name == "Founder");

bool isAdminButNotFounder = hasAdminRole && !hasFounderRole;

Upvotes: 6

Affaq Aslam
Affaq Aslam

Reputation: 53

You are missing paranthesis:

return (fullContextUser.Roles.Where(x => 
(x.Name == "Admin" || 
x.Name == "Head Recruiter") && 
x.Name != "Founder").Count() > 0);

Upvotes: -1

Related Questions