0

I'm trying to make an event that when someone gives a position to another member or places the position on himself with the administrator's permission, the bot will recognize and punish the member

(but it is not acknowledging or activating the event when executed and there is also no error on the console)...

const { Listener } = require("..")
const { RichEmbed, Permissions } = require("discord.js")

let AntiRaidPerm = [
  'ADMINISTRATOR',
  'KICK_MEMBERS',
  'BAN_MEMBERS',
  'MANAGE_CHANNELS',
  'MANAGE_ROLES',
  'MANAGE_EMOJIS',
  'MANAGE_GUILD'
]
module.exports = class MemberRoleUpdate extends Listener {
  constructor (client) {
    super(client)
    this.events = {
      memberRoleUpdate: this.memberroleupdate
    }
  }

  async memberroleupdate (role) {
    if (!this.client.db || !this.client.db.mongoose.connection) return
    if (!role.guild || !role.guild.id) return

    let doc = await this.client.db.guilds.get(role.guild.id)

    const fetchedLogs = await role.guild.fetchAuditLogs({
      limit: 1,
        type: 'MEMBER_ROLE_UPDATE',
    });

    const RoleLog = fetchedLogs.entries.first();

    const { executor, target } = RoleLog;
    
    if( executor.id === this.client.user.id ) return;
    if(executor.id === role.guild.owner) return;

    const adminPerm = new Permissions(target.permissions)
    const adminremove = role.guild.roles.get(target.id)

    if(adminPerm.has('ADMINISTRATOR') === true) {
      const docban = await this.client.db.usersban.get(executor.id)
  
      docban.ban.name = executor.tag
      docban.ban.id = executor.id
  
      docban.ban.counta = docban.ban.counta +1
  
      await docban.save()
  
      let count1 = `${docban.ban.counta}`
      let limit = `${doc.antiraid.alimit}`
      let msg = "Anti-Raid ativado! O usuário {member}"
      const newMsg = msg.replace("{member}", executor.tag);
  
      let warnEmbed = new RichEmbed()
          .setColor("#f7002c")
          .setDescription(newMsg + " foi banido!");

        if(count1 >= limit) {

          role.guild.member(executor).ban(`Proteção de Cargos ativada - \'${executor.username}\' foi banido!`).then(async msg => {
            role.guild.channels.get(doc.antiraid.antilog).send(warnEmbed).catch(()=>{})

            console.log('Estou banindo')

            const perm = new Permissions(adminremove.permissions);
            adminremove.setPermissions(perm.remove('ADMINISTRATOR', ))

            role.guild.roles.forEach(async(role) => {
              let botrole = role.guild.roles.find("name", "The Anti Raiding")

                if(role.position === botrole.position) return;
                if(role.position > botrole.position) return;
          
              if (role.hasPermissions(AntiRaidPerm)) {

              role.setPermissions(perm.remove(AntiRaidPerm))
            }})
          }).catch(console.error)
      }
    }
  }
}

1 Answers1

0

New Gateway Intents

Last year, on October 27, 2020, Discord implemented new gateway intent restrictions, which resulted in issues fetching events that included the GUILD_MEMBER intent. A full explanation by Lioness100 can be found here. In layman's terms, you haven't opted in to the proper intents, meaning your Discord bot will not receive certain events, such as guildMemberUpdate, which is what you want to have

Solution?

To receive these events, you'll need to turn them on via the Discord Developer Portal. To do this, go to https://discord.com/developers/applications and find your application. Typically, your bot's username is the application's name. After selecting the application, go to the "Bot" tab on the right sidebar and scroll down to Privileged Gateway Intents. Tick the two boxes, as shown below, then tap "Save Changes." Your bot should be receiving the events Privileged Gateway Intents

Coder Tavi
  • 449
  • 4
  • 12