confusedpiglet
confusedpiglet

Reputation: 33

Can I check what specific role a user has, from a list of roles?

I am using Discord.js for this btw! I have a command which checks if a user has a role, from a list of different roles:

const roleIds = new Set([
'1051468892050429579',
'1051466682357410846', 
'1051466680342833567', 
'1051466670713395144', 
]);

const userHasRole = event.member.roles.some(r => roleIds.has(r));

If the user has the role, it returns with 'True'. But I would like to have a command which returns the actual role ID the user has, instead of it just showing as 'True'.

For example, if the user had the second & fourth role on the list, it would return '1051466682357410846', '1051466670713395144', instead of just 'True' to confirm the role is there.

Is something like that possible?

Upvotes: 0

Views: 359

Answers (1)

Androz2091
Androz2091

Reputation: 3005

You should use .filter() instead of .some(), then.

const roleIds = new Set([
'1051468892050429579',
'1051466682357410846', 
'1051466680342833567', 
'1051466670713395144', 
]);

const roles = event.member.roles.filter(r => roleIds.has(r));
console.log(roles);

if (roles.length === 0) console.log('no role!');

The printed roles in the console will be the ones the user have in the list.

Note: if you are using Discord.js v13, you should use event.member.roles.cache.filter instead of event.member.roles.filter.

Upvotes: 1

Related Questions