I have an object containing a lot of hex values, which is:
var perms = {
// 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,
voicePrioritySpeaker: 0x100
};
Those are permissions in the discord API. A role in Discord would have multiple permissions, and therefore, discord sums them together using parseInt()
and converts them to decimal values (regular integer values) and store them in the classes of object.
For example, if the role has both generalCreateInstantInvite
and
generalKickMembers
, the result of role.permissions
would be 9
as it's the sum of 0x1
and 0x8
, and I know that because I know the role has those two specific permissions on them.
A quick summary:
My question is, how do I make the bot that doesn't have any data else the permissions list (perms
)and the permission integer (9
) use them and output the permissions names (generalCreateInstantInvite
and
generalKickMembers
) that they have? Or could there be an algorithm for that? If you have any links that might help, please send them.
Main JavaScript file example:
const Discord = require('discord.js');
const bot = new Discord.Client();
const perms = require('./Storage/perms.js');
bot.on('message', (message) => {
let prefix = '/';
let sender = message.author;
let msg = message.content;
let cont = msg.split(' ');
let args = cont.slice(1);
let cmd = msg.startsWith(prefix) ? cont[0].slice(prefix.length).toUpperCase() : undefined;
if (cmd === 'GETPERMISSIONS') {
let role = message.guild.roles.get(args[0]); // args[0] should be role ID
let rolePerms = role.permissions; // rolePerms is sum of hex values
// Converting back the hex sum to permission names and place in an array...
message.channel.send(permNames.join(' - '));
return;
}
});
The simplest way would be to use discord.js to convert it by using Permisions#toArray
Used like this:
new Discord.Permissions(role.permissions).toArray();
if the role has no permissions it will give an empty array []
otherwise it will give an array filled with Permissions#FLAGS
[ 'KICK_MEMBERS',
'BAN_MEMBERS',
'MANAGE_CHANNELS',
'MANAGE_GUILD'
]
You can obviously also use the other functions of the Permissions Class.
User contributions licensed under CC BY-SA 3.0