Search code examples
javascriptarraysdiscord.js

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

Solution

  • 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});