Search code examples
javascriptbotsdiscorddiscord.js

How to exclude something from forEach on discord.js


I am programming in discord.js and I am making a command which would kick everybody from a server. The thing is, I wanted to kick everyone except me, and I don't know how to do that, nor did I find any tutorial on it. Here is my code:

bot.on("message", async message => {
    if(message.content.startsWith(`${prefix}gone`)) {
      message.guild.members.cache.forEach(member => member.ban())
    }
      
}
)

Solution

  • Try something like this:

    const MY_USER_ID = 'your user id'
    bot.on("message", async message => {
      if(message.content.startsWith(`${prefix}gone`)) {
        message.guild.members.cache.forEach(member => {
          if (member.id !== MY_USER_ID) {
            member.ban()
          }
        })
      }
    })