Search code examples
javascriptnode.jsdiscorddiscord.js

Discord Js not updating permissions


I thought this code was working but now it seems to have stopped. I've tried giving the bot more permissions (eg Admin) but still not luck. There is no error and I really can't tell why this won't work.

Basically I'm trying to read the permissions on the everyone role and explicitly assign them to my new role_object. (Ultimately to remove the permissions from the everyone role).

For the purposes of testing @everyone only has the permission to kick members (0b10) and the new role has no permissions (0b0).

I expect to see:

2n
Permissions { bitfield: 0n }
Permissions { bitfield: 2n }

But I see:

2n
Permissions { bitfield: 0n }
Permissions { bitfield: 0n }

When I run:

const role_object = await interaction.guild.roles.fetch(verify_role);
const normal_perms = interaction.guild.roles.everyone.permissions;

console.log(normal_perms.bitfield);

console.log(role_object.permissions);
await role_object.permissions.add(normal_perms.bitfield);
console.log(role_object.permissions);

Thanks for any help!


Solution

  • Well, first of all, the Role#permissions property is readonly according to the docs. Calling permissions.add() does not actually modify the contents of the permissions object at all, as you've seen. But in fact, it also does not query the API to modify the permissions of the role, so even if it were modifying the contents of the permissions object, it would still not set the permissions of the role. You're using the wrong method. What you're looking for is Role#setPermissions(). This can accept the bitfield BigInt as an argument, allowing you to easily set the permissions of the role. Here's an example, based on the same code you provided:

    const role_object = await interaction.guild.roles.fetch(verify_role);
    const normal_perms = interaction.guild.roles.everyone.permissions;
    
    console.log(normal_perms.bitfield);
    
    console.log(role_object.permissions);
    await role_object.setPermissions(normal_perms.bitfield);
    console.log(role_object.permissions);
    

    And with that, your expectations become reality. I tested this code with my own bot and guild, and this was my result (keep in mind my everyone role has different perms than yours, hence the different bitfield value):

    969693056576n
    Permissions { bitfield: 0n }
    Permissions { bitfield: 969693056576n }