mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
Overhaul Permissions utilities (EvaluatedPermissions no more)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const os = require('os');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const Constants = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Util = require('../util/Util');
|
||||
const RESTManager = require('./rest/RESTManager');
|
||||
const ClientDataManager = require('./ClientDataManager');
|
||||
@@ -398,7 +399,7 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
generateInvite(permissions) {
|
||||
if (permissions) {
|
||||
if (permissions instanceof Array) permissions = this.resolver.resolvePermissions(permissions);
|
||||
if (permissions instanceof Array) permissions = Permissions.resolve(permissions);
|
||||
} else {
|
||||
permissions = 0;
|
||||
}
|
||||
|
||||
@@ -155,82 +155,6 @@ class ClientDataResolver {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data that can be resolved to give a permission number. This can be:
|
||||
* * A string
|
||||
* * A permission number
|
||||
*
|
||||
* Possible strings:
|
||||
* ```js
|
||||
* [
|
||||
* 'CREATE_INSTANT_INVITE',
|
||||
* 'KICK_MEMBERS',
|
||||
* 'BAN_MEMBERS',
|
||||
* 'ADMINISTRATOR',
|
||||
* 'MANAGE_CHANNELS',
|
||||
* 'MANAGE_GUILD',
|
||||
* 'ADD_REACTIONS', // add reactions to messages
|
||||
* 'READ_MESSAGES',
|
||||
* 'SEND_MESSAGES',
|
||||
* 'SEND_TTS_MESSAGES',
|
||||
* 'MANAGE_MESSAGES',
|
||||
* 'EMBED_LINKS',
|
||||
* 'ATTACH_FILES',
|
||||
* 'READ_MESSAGE_HISTORY',
|
||||
* 'MENTION_EVERYONE',
|
||||
* 'EXTERNAL_EMOJIS', // use external emojis
|
||||
* 'CONNECT', // connect to voice
|
||||
* 'SPEAK', // speak on voice
|
||||
* 'MUTE_MEMBERS', // globally mute members on voice
|
||||
* 'DEAFEN_MEMBERS', // globally deafen members on voice
|
||||
* 'MOVE_MEMBERS', // move member's voice channels
|
||||
* 'USE_VAD', // use voice activity detection
|
||||
* 'CHANGE_NICKNAME',
|
||||
* 'MANAGE_NICKNAMES', // change nicknames of others
|
||||
* 'MANAGE_ROLES_OR_PERMISSIONS',
|
||||
* 'MANAGE_WEBHOOKS',
|
||||
* 'MANAGE_EMOJIS'
|
||||
* ]
|
||||
* ```
|
||||
* @typedef {string|number} PermissionResolvable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resolves a PermissionResolvable to a permission number
|
||||
* @param {PermissionResolvable} permission The permission resolvable to resolve
|
||||
* @returns {number}
|
||||
*/
|
||||
resolvePermission(permission) {
|
||||
if (typeof permission === 'string') permission = Constants.PermissionFlags[permission];
|
||||
if (typeof permission !== 'number' || permission < 1) throw new Error(Constants.Errors.NOT_A_PERMISSION);
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn an array of permissions into a valid Discord permission bitfield
|
||||
* @param {PermissionResolvable[]} permissions Permissions to resolve together
|
||||
* @returns {number}
|
||||
*/
|
||||
resolvePermissions(permissions) {
|
||||
let bitfield = 0;
|
||||
for (const permission of permissions) bitfield |= this.resolvePermission(permission);
|
||||
return bitfield;
|
||||
}
|
||||
|
||||
hasPermission(bitfield, name, explicit = false) {
|
||||
const permission = this.resolvePermission(name);
|
||||
if (!explicit && (bitfield & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
|
||||
return (bitfield & permission) > 0;
|
||||
}
|
||||
|
||||
serializePermissions(bitfield) {
|
||||
const serializedPermissions = {};
|
||||
for (const name in Constants.PermissionFlags) {
|
||||
serializedPermissions[name] = this.hasPermission(bitfield, name);
|
||||
}
|
||||
return serializedPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data that can be resolved to give a string. This can be:
|
||||
* * A string
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const querystring = require('querystring');
|
||||
const long = require('long');
|
||||
const Permissions = require('../../util/Permissions');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Collection = require('../../util/Collection');
|
||||
const Snowflake = require('../../util/Snowflake');
|
||||
@@ -581,7 +582,7 @@ class RESTMethods {
|
||||
if (_data.permissions) {
|
||||
let perms = 0;
|
||||
for (let perm of _data.permissions) {
|
||||
if (typeof perm === 'string') perm = Constants.PermissionFlags[perm];
|
||||
if (typeof perm === 'string') perm = Permissions.FLAGS[perm];
|
||||
perms |= perm;
|
||||
}
|
||||
data.permissions = perms;
|
||||
|
||||
Reference in New Issue
Block a user