简体   繁体   中英

Remove role event with Discord JS

I am currently working on creating a Discord bot using Discord.js . If someone removes a role of the guild, I want the bot to know the ID of the removed role . How do I do that? Do I have to use the RoleManager ?

Thank you in advance.

You can use event guildMemberUpdate for this. If somethink change for member, its will trigger it. So you need compare roles at the first, and then check removed roles by filter collection.

const Discord = require('discord.js')
const bot = new Discord.Client()

bot.on('guildMemberUpdate', (oldMember, newMember) => {
    const isRolesChanged = !oldMember.roles.cache.equals(newMember.roles.cache)
    if (!isRolesChanged) {
        return
    }
    
    const removedRoles = oldMember.roles.cache.filter(role => newMember.roles.cache.has(role.id))
    console.log(removedRoles)
    
})

bot.login('TOKENHERE')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM