Discord bitwise permission calculator [closed]

Permissions are Discords primary feature enabling users to customize the workings of their server to their liking. To break it down to essentials: Permissions and permission overwrites tell Discord who is allowed to do what and where. When first confronted with them they can be quite confusing, but no worries we are here to take care of that, so let’s dive in!

# Roles as bot permissions

If you want to keep your bots permission checks simple, you might find it sufficient to just check if the member executing the command has a certain role.

If you have the role ID, you can simply check if the .roles
Collection on a GuildMember object includes it, using .has()
. Should you not know the ID and want to check for something like a role named «Mod», you can use .some()
.

member.
roles.
has
(
'role-id-here'
)
;
 // returns true if the member has the role
  member.
roles.
some
(
role
 =>
 role.
name ===
 'Mod'
)
;
 // returns true if any of the member's roles is exactly named "Mod"
 

If you want to enhance this system slightly, you can include the guild owner by comparing the executing members ID with message.guild.ownerID
.

To include permission checks like ADMINISTRATOR
or MANAGE_GUILD
, keep reading as we will cover Discord Permissions and all their intricacies in the following sections.

# Terminology

  • Permission: The ability to execute a certain action in Discord
  • Overwrite: Rule on a channel to modify the permissions for a member or role
  • Bit field: Binary representation of Discord permissions
  • Flag: Human readable string in MACRO_CASE, for example 'KICK_MEMBERS'
    , refers to a position in the permission bit field. You can find a list of all valid flags in the
  • Base Permissions: Permissions for roles the member has, set on the guild level
  • Final Permissions: Permissions for a member or role, after all overwrites are applied

TIP

# Base permissions

# Setting role permissions

Base permissions are set on roles, not the guild member itself. To change them, you access a Role object (for example via member.roles.first()
or guild.roles.random()
) and use the .setPermissions()
method. This is how you’d change the base permissions for the @everyone role, for example:

guild.
defaultRole.
setPermissions
(
[
'SEND_MESSAGES'
,
 'VIEW_CHANNEL'
]
)
;
 

Any permission not referenced in the flag array or bit field are not granted to the role.

TIP

Note that flag names are literal. Although VIEW_CHANNEL
grants access to view multiple channels the permission flag is still called VIEW_CHANNEL
in singular.

# Creating a role with permissions

guild.
createRole
(
{
 name:
 'Mod'
,
 permissions:
 [
'MANAGE_MESSAGES'
,
 'KICK_MEMBERS'
]
 }
)
;
 

# Checking member permissions

To know if a one of a member’s roles has a permission enabled, you can use the .hasPermission()
method of the class and provide a permission flag, array, or number to check for. You can also specify if you want to allow the ADMINISTRATOR
permission or the guild owner status to override this check with the following parameters.

if
 (
member.
hasPermission
(
'KICK_MEMBERS'
)
)
 {
 console.
log
(
'This member can kick'
)
;
 }
  if
 (
member.
hasPermission
(
[
'KICK_MEMBERS'
,
 'BAN_MEMBERS'
]
)
)
 {
 console.
log
(
'This member can kick and ban'
)
;
 }
  if
 (
member.
hasPermission
(
'KICK_MEMBERS'
,
 false
,
 false
)
)
 {
 console.
log
(
'This member can kick without allowing admin to override'
)
;
 }
  if
 (
member.
hasPermission
(
'KICK_MEMBERS'
,
 false
,
 false
,
 false
)
)
 {
 console.
log
(
'This member can kick without allowing admin or owner to override'
)
;
 }
 

If you provide multiple permissions to the method, it will only return true
if all permissions you specified are granted.

# Channel overwrites

Permission overwrites control the abilities of members for this specific channel or a set of channels if applied to a category with synchronized child channels.

As you have likely already seen in your desktop client, channel overwrites have three states:

  • Explicit allow (true
    , green ✓)
  • Explicit deny (false
    , red X)
  • Default (null
    , gray /)

# Adding overwrites

To add a permission overwrite for a role or guild member, you access the channel object and use the .overwritePermissions()
method. The first parameter is the target of the overwrite, either a Role or GuildMember object (or its respective resolvable), and the second is a object.

Let’s add an overwrite to lock everyone out of the channel. The guild ID doubles as the role id for the default role @everyone as demonstrated below:

channel.
overwritePermissions
(
channel.
guild.
defaultRole,
 {
 VIEW_CHANNEL
:
 false
 }
)
;
 

Any permission flags not specified get neither an explicit allow nor deny overwrite and will use the base permission, unless another role has an explicit overwrite set.

You can also provide an array of overwrites during channel creation as shown below:

guild.
createChannel
(
'new-channel'
,
 'text'
,
 [
 {
 id:
 guild.
defaultRole.
id,
 deny:
 [
'VIEW_CHANNEL'
]
,
 }
,
 {
 id:
 user.
id,
 allow:
 [
'VIEW_CHANNEL'
]
,
 }
,
 ]
)
;
 

WARNING

On the master branch, the functionality of GuildChannel#overwritePermissions
is changed to replacing all overwrites. GuildChannel#updateOverwrite
is introduced to take its place in updating a single overwrite while keeping all others intact.

# Replacing overwrites

To replace all permission overwrites on the channel with a provided set of new overwrites, you can use the .replacePermissionOverwrites()
function. This is extremely handy if you want to copy a channels full set of overwrites to another one, as this method allows passing an array or Collection of as well as .

// copying overwrites from another channel
 channel.
replacePermissionOverwrites
(
{
 overwrites:
 otherChannel.
permissionOverwrites }
)
;
  // replacing overwrites with PermissionOverwriteOptions
 channel.
replacePermissionOverwrites
(
{
 overwrites:
 [
 {
 id:
 guild.
defaultRole.
id,
 deny:
 [
'VIEW_CHANNEL'
]
,
 }
,
 {
 id:
 user.
id,
 allow:
 [
'VIEW_CHANNEL'
]
,
 }
,
 ]
,
 }
)
;
 

WARNING

On the master branch, the functionality of GuildChannel#overwritePermissions
is changed to replace overwrites

# Removing overwrites

To remove the overwrite for a specific member or role, you can get it from the channels permissionOverwrites Collection and call the .delete()
method on it. Since the Collection is keyed by the target’s ID (either role ID or user ID), the respective overwrite is very easy to access.

// deleting the channels overwrite for the message author
 channel.
permissionOverwrites.
get
(
message.
author.
id)
.
delete
(
)
;
 

# Syncing with a category

If the permission overwrites on a channel under a category match with the parent (category) the channel is considered to be synchronized. This means that any changes in the categories overwrites will now also change the channels overwrites. Changing the child channels overwrites will not effect the parent.

To easily synchronize permissions with the parent channel you can call the .lockPermissions()
method on the respective child channel.

if
 (
!
channel.
parent)
 {
 return
 console.
log
(
'This channel is not listed under a category'
)
;
 }
  channel.
lockPermissions
(
)
 .
then
(
(
)
 =>
 console.
log
(
'Successfully synchronized permissions with parent channel'
)
)
 .
catch
(
console.
error)
;
 

# Permissions after overwrites

discord.js features two utility methods to easily determine the final permissions for a guild member or role in a specific channel: .permissionsFor()
on the class and .permissionsIn()
on the class (note that the Role class does not yet feature a .permissionsIn()
method on stable). Both return a object.

To check your bots permissions in the channel the command was used in, you could use something like this:

// final permissions for a guild member using permissionsFor
 const
 botPermissionsFor =
 channel.
permissionsFor
(
guild.
me)
;
  // final permissions for a guild member using permissionsIn
 const
 botPermissionsIn =
 guild.
me.
permissionsIn
(
channel)
;
  // final permissions for a role
 const
 rolePermissions =
 channel.
permissionsFor
(
role)
;
 

WARNING

The .permissionsFor()
method returns a bit field with all permissions set if the member or role has the global ADMINISTRATOR
permission and does not take overwrites into consideration in this case. Using the second parameter of the .has()
method as described further down in the guide will not allow you to check without taking ADMINISTRATOR
into account here!

If you want to know how to work with the returned Permissions objects keep reading as this will be our next topic.

# The Permissions object

# Converting permission numbers

Some methods and properties in Discord.js return permission decimals rather than a Permissions object, making it hard to manipulate or read them if you don’t want to use bitwise operations. You can, however, pass these decimals to the Permissions constructor to convert them as shown below.

const
 {
 Permissions }
 =
 require
(
'discord.js'
)
;
 const
 permissions =
 new
 Permissions
(
268550160
)
;
 
const
 {
 Permissions }
 =
 require
(
'discord.js'
)
;
 const
 flags =
 [
 'MANAGE_CHANNELS'
,
 'EMBED_LINKS'
,
 'ATTACH_FILES'
,
 'READ_MESSAGE_HISTORY'
,
 'MANAGE_ROLES'
,
 ]
;
  const
 permissions =
 new
 Permissions
(
flags)
;
 

# Checking for permissions

The Permissions object features the .has()
method, allowing an easy way to check flags in a Permissions bit field. The .has()
method takes two parameters: the first being either a permission number, single flag, or an array of permission numbers and flags, the second being a boolean, indicating if you want to allow the ADMINISTRATOR
permission to override (defaults to true).

Let’s say you want to know if the decimal bit field representation 268550160
has MANAGE_CHANNELS
referenced:

const
 {
 Permissions }
 =
 require
(
'discord.js'
)
;
  const
 permissions =
 new
 Permissions
(
[
 'MANAGE_CHANNELS'
,
 'EMBED_LINKS'
,
 'ATTACH_FILES'
,
 'READ_MESSAGE_HISTORY'
,
 'MANAGE_ROLES'
,
 ]
)
;
  console.
log
(
permissions.
has
(
'MANAGE_CHANNELS'
)
)
;
 // output: true
  console.
log
(
permissions.
has
(
[
'MANAGE_CHANNELS'
,
 'EMBED_LINKS'
]
)
)
;
 // output: true
  console.
log
(
permissions.
has
(
[
'MANAGE_CHANNELS'
,
 'KICK_MEMBERS'
]
)
)
;
 // output: false
  const
 adminPermissions =
 new
 Permissions
(
'ADMINISTRATOR'
)
;
  console.
log
(
adminPermissions.
has
(
'MANAGE_CHANNELS'
)
)
;
 // output: true
  console.
log
(
adminPermissions.
has
(
'MANAGE_CHANNELS'
,
 true
)
)
;
 // output: true
  console.
log
(
adminPermissions.
has
(
'MANAGE_CHANNELS'
,
 false
)
)
;
 // output: false
 

# Manipulating permissions

The Permissions object enables you to easily add or remove certain permissions from an existing bit field without having to worry about bitwise operations. Both .add()
and .remove()
can take a single permission flag or number, an array of permission flags or numbers, or multiple permission flags or numbers as multiple parameters.

const
 {
 Permissions }
 =
 require
(
'discord.js'
)
;
  const
 permissions =
 new
 Permissions
(
[
 'MANAGE_CHANNELS'
,
 'EMBED_LINKS'
,
 'ATTACH_FILES'
,
 'READ_MESSAGE_HISTORY'
,
 'MANAGE_ROLES'
,
 ]
)
;
  console.
log
(
permissions.
has
(
'KICK_MEMBERS'
)
)
;
 // output: false
  permissions.
add
(
'KICK_MEMBERS'
)
;
 console.
log
(
permissions.
has
(
'KICK_MEMBERS'
)
)
;
 // output: true
  permissions.
remove
(
'KICK_MEMBERS'
)
;
 console.
log
(
permissions.
has
(
'KICK_MEMBERS'
)
)
;
 // output : false
 

You can utilize these methods to adapt permissions or overwrites without touching the other flags. To achieve this you can get the existing permissions for a role, manipulating the bit field as described above and passing the changed bit field to role.setPermissions()
.

TIP

In the stable branch, role.permissions
returns a number which needs to be converted to a Permissions object for this to work as described here. We covered how to achieve this in the section «Converting permission numbers to Objects»

# Resulting code

Asked
1 year, 11 months ago
Active
2 months ago
Viewed
711 times

I’m trying to let my tool/script determine if a logged in user has the correct permission to use the tool. however Discord is responding with a permission int.

It seems that I have to check permission using a bitwise operator, But from everything that i’ve googled or searched almost everything I find seems completely irrelevant.

Example Permission: 2146958591

Permission references;

// General generalCreateInstantInvite: 0x1, generalKickMembers: 0x2, generalBanMembers: 0x4, generalAdministrator: 0x8, generalManageChannels: 0x10, generalManageServer: 0x20, generalChangeNickname: 0x4000000, generalManageNicknames: 0x8000000, generalManageRoles: 0x10000000, generalManageWebhooks: 0x20000000, generalManageEmojis: 0x40000000, generalViewAuditLog: 0x80, // Text textAddReactions: 0x40, textReadMessages: 0x400, textSendMessages: 0x800, textSendTTSMessages: 0x1000, textManageMessages: 0x2000, textEmbedLinks: 0x4000, textAttachFiles: 0x8000, textReadMessageHistory: 0x10000, textMentionEveryone: 0x20000, textUseExternalEmojis: 0x40000, // Voice voiceViewChannel: 0x400, voiceConnect: 0x100000, voiceSpeak: 0x200000, voiceMuteMembers: 0x400000, voiceDeafenMembers: 0x800000, voiceMoveMembers: 0x1000000, voiceUseVAD: 0x2000000
Is there a tool or a sample script that I can study, to determine how this calculation is made?

Thank you in advance!

|
improve this question

closed
as off-topic by Makyen, River, EJoshuaS, TylerH, NkosiApr 15 ’18 at 16:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • «Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource
    are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.» – Makyen, River, EJoshuaS, TylerH, Nkosi

If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

active oldest votes
4

Imagine that you have 4
different actions you want to allow or disallow for each user:

  • create post
  • update post
  • read post
  • delete post.

Let’s create a 4-characters string of 1
and . The first character in the string represents the user’s possibility to create post
action. The second character – update post
, third – read post
and the fourth – delete post
. For example, if a user has permissions 1001
then he can create posts and delete posts, but can’t update and read.

What is the most efficient way to store these permissions? We have only and 1
in every position of our string, so we can store this data not inside the string but inside the binary representation of the number. So, the user’s permission will be some decimal number, and every bit of this number will represent permission for a specific action.

For example, our permission string 1001
will be just decimal number 9
(= 1*2^0 + 0*2^1 + 0*2^2 + 1*2^3
).

We can represent every permission as binary:

  • create post = 1000
  • update post = 0100
  • read post = 0010
  • delete post = 0001

But how can we check that user has specific permission or has a group of permissions? It’s easy, let’s use bitwise operator &
:

If user permission equals 9
= 1001
b, then:

  • 1001 & 1000 = 1000
    , 1000 > 0
    – user can create posts
  • 1001 & 0100 = 0000
    , 0000 == 0
    – user can’t update posts
  • 1001 & 0010 = 0000
    , 0000 == 0
    – user can’t read posts
  • 1001 & 0001 = 0001
    , 0001 > 0
    – user can delete posts
|
improve this answer
3

To check if the given permission value has a selected permission, you use a Bitwise AND operator.

Example of this:

$canKickMembers = ($permissionCode & 0x2) != 0; 
|
improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.

lang-php

Permissions
is a feature on Discord that allows different users to perform various actions within servers or specific channels.

Permissions list
Edit

Role Settings
Edit

  • Display members separately from online
  • Allow anyone to @mention this role

General Permissions
Edit

  • Administrator
    — Members with this permission have ever permission and also bypass channel specific permissions. This is a very dangerous permission to grant.
  • View Audit Log
    — Members with this permission have access to the server’s audit logs.
  • Manage Server
    — Members with this permission can change the server’s name or move the server’s region.
  • Manage Roles
    — Members with this permission can create new roles and edit/delete roles lower than the user’s highest role.
  • Manage Channels
    — Members with this permission can create new channels and edit or delete existing ones.
  • Kick Members
    — Removes a member from the guild. They will be able to rejoin with a new invite.
  • Ban Members
    — Removes a member from the guild and adds them to the ban list.
  • Create Instant Invite
    — Allows the member to create a invite that allows other users to join.
  • Change Nickname
    — Members with this permission can change nicknames of other members.
  • Manage Emojis
    — Allows the member to add/remove emojis from the server.
  • Manage Webhooks
    — Members with this permission can create, edit, and delete webhooks.

Text Permissions
Edit

  • Read Text & See Voice Channels
    — Allows the user to see the channel.
  • Send Messages
    — Allows the user to send messages within the channel.
  • Send TTS Messages
    — Allows the user to send text-to-speech messages by starting a message with /tts. These messages can be heard by everyone focused on the channel.
  • Manage Messages
    — Members with this permission can delete message sent by other members or pin any message to a channel.
  • Embed Links
  • Attach Links
  • Read Message History
    — Allows the member to read message history within the current channel.
  • Mention Everyone
    — Members with this permission can trigger push notifications for all members of the server (role) or all members of the channel (Channel Permission).
  • Use External Emojis
    — Members with this permission can use emojis from other server within the server.
  • Add Reactions
    — Allows members to add new reactions to a message. Members can still react using reactions already add to messages without this permission.

Voice Permissions
Edit

  • View Channel
    — Allows the member to see the voice channel.
  • Connect
    — Allows the member to join the voice channel.
  • Speak
    — Allows the member to speak in the voice channel
  • Mute Members
    — Allows the member to mute other users from speaking in the voice channel.
  • Deafen Members
    — Allows the member to deafen other members in the voice channel.
  • Move Members
    — Members with this permission can drag other members out of the channel. They can only move members between channels that both they and the member that they are moving have access.
  • Use Voice Activity
    — Members must use push-to-talk in the voice channel if this permission the denied.

Trivia
Edit

  • In an update log, they refer to permissions as persimmons
    , a type of orange fruit that resembles a crunchy tomato.
Community content is available under CC-BY-SA unless otherwise noted.
Рейтинг автора
5
Подборку подготовил
Андрей Ульянов
Наш эксперт
Написано статей
168
Ссылка на основную публикацию
Похожие публикации