AtomyTR
AtomyTR

Reputation: 3

Discord.js / finding role by ID from an array

I am making role giving command for my discord.js bot. If selected role equals to any ID's from an array, then command will work; if it not then it will return.

Here's the code I was trying:

    let roles = 
    [
      1024591717820813322,
      1023191063449571348,
    ]

    if (testR.id == roles)
    {
      interaction.reply({content: `worked`})
    }

Upvotes: 0

Views: 98

Answers (1)

Caladan
Caladan

Reputation: 2337

You can use the .includes() method to see if an element exists in an array. If the element exists, then it will return true, otherwise false. Your finished code would look something like this:

let roles = [
  "1024591717820813322",
  "1023191063449571348",
]
if (roles.includes(testR.id)) {
    interaction.reply({ content: "Test Role id exists in roles array" })
} else {
    interaction.reply({ content: "Test Role id does not exist in roles array" })
}

(Note: in Discord, ids are stored in the form of strings, not numbers. I have made the changes accordingly and advise you to do the same.)

Upvotes: 1

Related Questions