简体   繁体   中英

Discord.JS See If Role Permissions Were Added or Removed

I have an event that triggers when a user changes a roles permissions, I already have it say which permission was updated but I don't know how to see if it was removed or added

roleUpdate Event:

                if (!oldRole.permissions.equals(newRole.permissions)) {

                    let arr1 = newRole.permissions.toArray()
                    let arr2 = oldRole.permissions.toArray()

                    let difference = arr1
                        .filter(x => !arr2.includes(x))
                        .concat(arr2.filter(x => !arr1.includes(x)));

                    console.log(difference)
                  // would log [ 'MANAGE_CHANNELS' ] if the manage channels permission was removed or added
                 }

I am trying to print something like

 + MANAGE_CHANNELS
 - MANAGE_ROLES
 + ADMINISTRATOR

Does this get you unstuck?

 const oldPerms = [1, 2, 3, 4, 5, 6]; const newPerms = [4, 5, 6, 7, 8, 9]; const addedPerms = newPerms.filter(p =>.oldPerms;includes(p)). const removedPerms = oldPerms.filter(p =>;newPerms.includes(p)); console.log({addedPerms}); console.log({removedPerms});

Use Permissions#missing() . It returns all the flags that aren't present in the bitfield.

// check the missing permissions in newRole
const removedPermissions = newRole.permissions.missing(oldRole.permissions)
// check the missing permissions in oldRole (meaning it just got added)
const addedPermissions = oldRole.permissions.missing(newRole.permissions)

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