1

I am writing a bot to verify roles on a discord server and I came across one snag when I write more than one variable to guildMember.roles.cache.has(). The bot does not check all of them, it is possible to do something for bot to check multiple roles with operators? Thank you

if (command === 'verify' && mcname !=null && message.channel.type == "dm") {                                                                  
  //IF COMMAND IS VERIFY,MCNAME NOT EMPTY AND CHANNEL IS DM
  const guild = await message.client.guilds.fetch(serverid);
  const guildMember = await guild.members.fetch(message.author.id);

  if (guildMember.roles.cache.has(roleID1 || roleID2 || roleID3 || roleID4 || roleID5 || roleID6 || roleID7)) { 

I have defined role ids upper.

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Romis Pejs
  • 87
  • 4
  • Does this answer your question? [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – Heretic Monkey May 17 '21 at 13:28
  • 1
    [This question](https://stackoverflow.com/questions/62422854/discord-js-multi-role-check) may help you out – Toasty May 17 '21 at 13:29

1 Answers1

1

You could store those role IDs in an array and check if the member has any of these. guildMember.roles.cache returns a collection of roles so you can use the some() method to check if any of the member's role's ID is included in the array of IDs:

const roleIDs = [roleID1, roleID2, roleID3, roleID4, roleID5, roleID6, roleID7];

if (guildMember.roles.cache.some(role => roleIDs.includes(role.id))) {
// ... 

Try the snippet below:

const guildMember = {
  roles: {
    cache: [{ id: '1' }, { id: '5' }, { id: '9' }]
  }
}

const roleIDs1 = ['2', '3', '4']
const roleIDs2 = ['3', '4', '5', '6', '7']

console.log('Member has a role ID included in roleIDs1?', guildMember.roles.cache.some(role => roleIDs1.includes(role.id)))
console.log('Member has a role ID included in roleIDs2?', guildMember.roles.cache.some(role => roleIDs2.includes(role.id)))
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57