Discord Permissions String to Array
So when I get the guild information with the access token, the permissions of the user comes as a string with numbers. Is there any way I could turn it into an array like this ['SEND_MESSAGES' : true, 'READ_MESSAGES' : false] so that I can check if they have Administrator permission, or is there another way do it that I don't know about?
-
If you are using a library, it wouldn't surprise me if there is a built-in method to directly check if a given user has the admin perm. Discord.js (https://discordjs.guide/popular-topics/permissions.html) and Discord.py (https://discordpy.readthedocs.io/en/latest/ext/commands/api.html) both do.
However, if you are writing a pure API implementation, you'll need to use bitwise operations to extract the relevant values. You should definitely read the API docs on permissions (https://discord.com/developers/docs/topics/permissions). The pseudo-code to check for the admin perm (where the permissions variable is an integer) would look something like this:
(permissions & 0x8) == 0x8
Building a dictionary of permission values as you described would require writing the bitwise operations for each of the permissions you'd like to extract.
Though it's not official, I'd recommend joining the main Discord API guild (https://discord.gg/discord-api) if you have further questions about implementing the API or the relevant bit math. Hope that helps! Cheers.
1 -
Thank you, I will do some research on that.
0
Please sign in to leave a comment.
Comments
2 comments