title: Anti add role discord.js
body: <p>I coded a bot that automatically deranks, ban kicks when someone deletes a role and creates a new role but I can't do the same thing when someone adds a new role with important permissions to a person so I redirected myself to this site which helped me a lot here is the code (the code that allows to derank, ban, kick the person) :</p>
  <pre><code>const config = require(&quot;../config&quot;);
  const Discord = require(&quot;discord.js&quot;);
  const { user } = require(&quot;..&quot;);
  
  module.exports = class {
  
      constructor(client) {
          this.client = client;
          this.name = &quot;ROLE_ADD&quot;
      }
  
      async run(member) {
          let startAt = Date.now()
  
          var client = this.client
          let guild = member.guild
  
          let guildData = await client.database.models.guilds.findOne({
              where: {
                  guildID: guild.id,
              }
          })
  
  
          if (!guildData) return guild.leave()
  
          if (member.user.bot) {
              if (!member.guild.me.hasPermission(&quot;VIEW_AUDIT_LOG&quot;)) return client.config.owners.forEach(async (o) =&gt; {
                  client.users.cache.get(o).send(client.translator(guildData.language).error(null, &quot;notEnoughPermissions&quot;, {
                      action: `ROLE_ADD`
                  }))
              })
  
              const action = await member.guild.fetchAuditLogs({ type: &quot;ROLE_ADD&quot; }).then(async (audit) =&gt; audit.entries.first());
              if (action.executor.id === client.user.id) return;
  
              let userData = await client.database.models.members.findOne({
                  where: {
                      userID: action.executor.id,
                  }
              })
  
              if (!userData) userData = await client.database.models.members.create({
                  userID: action.executor.id,
              })
  
              if (client.config.owners.includes(action.executor.id) || guild.owner.id == action.executor.id || guildData.whitelist.includes(action.executor.id) || !guildData.antirole) {
                  let logChannel = client.guilds.cache.get(guild.id).channels.cache.get(guildData.protectLog)
  
                  if (logChannel) logChannel.send(client.translator(guildData.language).event(&quot;ROLE_ADD&quot;, {
                      ...action,
                      timeout: Date.now() - startAt,
                      triggered: true
                  }))
              } else if (guildData.antirole &amp;&amp; !client.config.owners.includes(action.executor.id) || guild.owner.id !== action.executor.id) {
                  member.kick(`Protection - Type: ${this.name}`)
  
                  let logChannel = client.guilds.cache.get(guild.id).channels.cache.get(guildData.protectLog)
  
                  let after = await client.database.models.detections.findOne({
                      where: {
                          type: this.name
                      }
                  })
  
                  let userAlerts = await client.database.models.logs.findAll({
                      where: {
                          author: action.executor.id
                      }
                  })
  
                  if (userAlerts.length &gt;= 1) {
                      if (userAlerts.length &gt;= after.max) {
                          const TimeAgo = (date, s) =&gt; {
                              const hourago = Date.now() - s;
  
                              return date &gt;= hourago;
                          }
  
                          if (TimeAgo(userAlerts.pop().makedAt, after.time)) {
                              if (after.sanctions === 'ban') {
                                  action.target.guild.member(action.executor.id).ban({
                                      reason: `Protection - Type: ${this.name} | Alerts: ${userAlerts.filter(x =&gt; TimeAgo(x.makedAt, after.time)).length}`
                                  })
                              } else if (after.sanctions === 'kick') {
                                  action.target.guild.member(action.executor.id).kick(`Protection - Type: ${this.name} | Alerts: ${userAlerts.filter(x =&gt; TimeAgo(x.makedAt, after.time)).length}`)
  
                              } else if (after.sanctions === 'unrank') {
                                  action.target.guild.members.cache.get(action.executor.id).roles.remove(action.target.guild.members.cache.get(action.executor.id).roles.cache.array(), `Protection - Type: ${this.name} | Alerts: ${userAlerts.filter(x =&gt; TimeAgo(x.makedAt, after.time)).length}`)
                              }
                          }
                      }
                  }
  
                  if (logChannel) logChannel.send(client.translator(guildData.language).event(&quot;ROLE_ADD&quot;, {
                      ...action,
                      timeout: Date.now() - startAt,
                      triggered: true
                  }))
  
                  userData.antirole++
                  userData.save()
              }
              client.pushDetection(this.name, Date.now() - startAt, action.executor.id)
          }
      }
  };
  </code></pre>