Home > Back-end >  Discord.js: roles.some(r => !message.member.roles.cache.has(r)) doesn't work
Discord.js: roles.some(r => !message.member.roles.cache.has(r)) doesn't work

Time:12-21

So I am switching from single STAFF command (e.g. if(message.member.roles.cache.has(staff_id))) to more flexable method - if member has either helper or mod or admin role, etc. I used some method with array like this:

const roles = [
  "859471924753072188",
  "836214627243262002",
  "922170188089139261",
  "836214626923315221",
  "922170903394127873",
  "859878718599987200",
  "836214626617655296"
]
const isntStaff = roles.some(r => !message.member.roles.cache.has(r))

And now whenever I use this in some code, for example in this one:

...
if (
    rudeWords.some(word =>
      message
        .toString()
        .toLowerCase()
        .includes(word)
    ) &&
    isntStaff
  ) {
    message.delete({ reason: `Blacklisted Word Detected` });
    message.channel
      .send({ content: `${message.author}, do not use that word here, thank you.` })
      .then(msg => {
    setTimeout(() => msg.delete(), 3000)
  })

it just doesn't let me to use the word even if I have helper or mod or admin command. It also logs nothing in console.

CodePudding user response:

Your code requires every single one of those roles. You are checking if there are any roles in that array that aren't in the member's role cache. Instead, put the ! on the outside

const roles = [
  "859471924753072188",
  "836214627243262002",
  "922170188089139261",
  "836214626923315221",
  "922170903394127873",
  "859878718599987200",
  "836214626617655296"
]
const isntStaff = !roles.some(r => message.member.roles.cache.has(r))

CodePudding user response:

There is also another way to do this according to this Discord.js Guide.

This is an example of how you could do this:

// Check if they have one of many roles
const roles = [
  "859471924753072188",
  "836214627243262002",
  "922170188089139261",
  "836214626923315221",
  "922170903394127873",
  "859878718599987200",
  "836214626617655296"
]

if (message.member.roles.cache.some(r => roles.includes(r.id)) ) {
  // has one of the roles
}else {
  // has none of the roles
}
  • Related