Editing Channel Permission Overrides with Permission Value

Viewed 100

The 53-bit integer value defines the permissions of the bot. But one sentence confused me and it brought me to this easier idea.

...you can pass in keyword arguments denoting Permissions attributes (quoted from here)

I want to set my VoiceChannel permissions with the set_permissions function, but using the 53-bit integer value (my idea is to set value in the Permission parameters), which would make everything much easier.

What I tried so far

  • I tried setting the permission bit in the overwrite parameter of set_permissions with discord.Permissions(value=..)
  • I tried not using discord.Permissions
  • I tried setting in the value parameter of the permissions parameter for set_permissions as the integer (e.g. value=1024)

They all don't work. I didn't find anything about this so I guess it's either not possible or nobody thought about this.

The code I have so far

--- Just a example code cut out of my code ---

ow_n = 1024

await voice_channel.set_permissions(everyone, permissions=ow_n)    # voice_channel & everyone defined corrrectly before

Error:

discord.errors.InvalidArgument: Invalid permissions given to keyword arguments

To reiterate my question, how do I set channel permission overrides using a permission bit integer?

1 Answers

discord.py is a full fledged Discord API wrapper. There's no need to calculate the permission value yourself. discord.py has a helper class PermissionOverwrite for setting channel permissions (Permissions is different as it's used for server-wide role permissions).

The main difference between Permissions and PermissionOverwrite, is that Permissions has a fixed boolean value for each permission—either the permission is granted (True) or is not (False). Whereas PermissionOverwrite which is used for channel permissions overrides, has three possible values for each permission, the permission is explicitly granted (overrides role permission for this channel), the permission is explicitly denied (overrides role permission for this channel), or this permission isn't overridden (unset, default role permission applies).

I believe you want to edit a voice channel and grant everyone the VIEW_CHANNEL permission (since that's the permission for 1024).

overwrite = discord.PermissionOverwrite()
overwrite.view_channel = True
await voice_channel.set_permissions(everyone, overwrite=overwrite)

Or as your quote is referring:

await voice_channel.set_permissions(everyone, view_channel=True)

Now to answer your question:

The reason why it's not possible to pass in a permission value directly to permission overrides is because it's too trivial to implement. For normal Permissions, it works because each flag bit that's set indicates that the permission is granted, while not set indicates that it's denied. This system wouldn't work for PermissionOverwrite since there's three states as mentioned earlier.

The only viable implementation would have PermissionOverwrite takes two permission bit values, one for "allowed" and one for "denied". This is actually how Discord API implement overrides. However, IMO this would cause confusion and wouldn't benefit much people.

Related