backport: Permissions improvements

This commit is contained in:
Lewdcario
2018-07-17 21:49:21 -05:00
parent 0702a0fcda
commit 524a15df0b
9 changed files with 120 additions and 45 deletions

View File

@@ -969,8 +969,8 @@ class Guild {
/**
* Can be used to overwrite permissions when creating a channel.
* @typedef {Object} ChannelCreationOverwrites
* @property {PermissionResolvable[]|number} [allow] The permissions to allow
* @property {PermissionResolvable[]|number} [deny] The permissions to deny
* @property {PermissionResolvable|number} [allow] The permissions to allow
* @property {PermissionResolvable|number} [deny] The permissions to deny
* @property {RoleResolvable|UserResolvable} id ID of the role or member this overwrite is for
*/

View File

@@ -140,6 +140,28 @@ class GuildChannel extends Channel {
};
}
/**
* Replaces the permission overwrites for a channel
* @param {Object} [options] Options
* @param {Array<PermissionOverwrites|PermissionOverwriteOptions>} [options.overwrites] Permission overwrites
* @param {string} [options.reason] Reason for updating the channel overwrites
* @returns {Promise<GuildChannel>}
* @example
* channel.replacePermissionOverwrites({
* overwrites: [
* {
* id: message.author.id,
* denied: ['VIEW_CHANNEL'],
* },
* ],
* reason: 'Needed to change permissions'
* });
*/
replacePermissionOverwrites({ overwrites, reason } = {}) {
return this.edit({ permissionOverwrites: overwrites, reason })
.then(() => this);
}
/**
* An object mapping permission flags to `true` (enabled), `false` (disabled), or `null` (not set).
* ```js
@@ -215,6 +237,21 @@ class GuildChannel extends Channel {
return this.client.rest.methods.setChannelOverwrite(this, payload, reason).then(() => this);
}
/**
* Locks in the permission overwrites from the parent channel.
* @returns {Promise<GuildChannel>}
*/
lockPermissions() {
if (!this.parent) return Promise.reject(new TypeError('Could not find a parent to this guild channel.'));
const permissionOverwrites = this.parent.permissionOverwrites.map(overwrite => ({
deny: overwrite.deny.bitfield,
allow: overwrite.allow.bitfield,
id: overwrite.id,
type: overwrite.type,
}));
return this.edit({ permissionOverwrites });
}
/**
* The data for a guild channel.
* @typedef {Object} ChannelData

View File

@@ -305,7 +305,7 @@ class GuildMember {
/**
* Checks if any of this member's roles have a permission.
* @param {PermissionResolvable|PermissionResolvable[]} permission Permission(s) to check for
* @param {PermissionResolvable} permission Permission(s) to check for
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permission
* **(deprecated)**
* @param {boolean} [checkAdmin] Whether to allow the administrator permission to override
@@ -323,7 +323,7 @@ class GuildMember {
/**
* Checks whether the roles of this member allows them to perform specific actions.
* @param {PermissionResolvable[]} permissions The permissions to check for
* @param {PermissionResolvable} permissions The permissions to check for
* @param {boolean} [explicit=false] Whether to require the member to explicitly have the exact permissions
* @returns {boolean}
* @deprecated
@@ -335,11 +335,12 @@ class GuildMember {
/**
* Checks whether the roles of this member allows them to perform specific actions, and lists any missing permissions.
* @param {PermissionResolvable[]} permissions The permissions to check for
* @param {PermissionResolvable} permissions The permissions to check for
* @param {boolean} [explicit=false] Whether to require the member to explicitly have the exact permissions
* @returns {PermissionResolvable[]}
* @returns {PermissionResolvable}
*/
missingPermissions(permissions, explicit = false) {
if (!(permissions instanceof Array)) permissions = [permissions];
return this.permissions.missing(permissions, explicit);
}

View File

@@ -1,3 +1,5 @@
const Permissions = require('../util/Permissions');
/**
* Represents a permission overwrite for a role or member in a guild channel.
*/
@@ -27,8 +29,29 @@ class PermissionOverwrites {
*/
this.type = data.type;
/**
* The permissions that are denied for the user or role as a bitfield.
* @type {number}
*/
this.deny = data.deny;
/**
* The permissions that are allowed for the user or role as a bitfield.
* @type {number}
*/
this.allow = data.allow;
/**
* The permissions that are denied for the user or role.
* @type {Permissions}
*/
this.denied = new Permissions(data.deny).freeze();
/**
* The permissions that are allowed for the user or role.
* @type {Permissions}
*/
this.allowed = new Permissions(data.allow).freeze();
}
/**

View File

@@ -153,7 +153,7 @@ class Role {
/**
* Checks if the role has a permission.
* @param {PermissionResolvable|PermissionResolvable[]} permission Permission(s) to check for
* @param {PermissionResolvable} permission Permission(s) to check for
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permission
* **(deprecated)**
* @param {boolean} [checkAdmin] Whether to allow the administrator permission to override
@@ -175,7 +175,7 @@ class Role {
/**
* Checks if the role has all specified permissions.
* @param {PermissionResolvable[]} permissions The permissions to check for
* @param {PermissionResolvable} permissions The permissions to check for
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permissions
* @returns {boolean}
* @deprecated
@@ -201,7 +201,7 @@ class Role {
* @property {ColorResolvable} [color] The color of the role, either a hex string or a base 10 number
* @property {boolean} [hoist] Whether or not the role should be hoisted
* @property {number} [position] The position of the role
* @property {PermissionResolvable[]|number} [permissions] The permissions of the role
* @property {PermissionResolvable|number} [permissions] The permissions of the role
* @property {boolean} [mentionable] Whether or not the role should be mentionable
*/
@@ -282,7 +282,7 @@ class Role {
/**
* Set the permissions of the role.
* @param {PermissionResolvable|PermissionResolvable[]} permissions The permissions of the role
* @param {PermissionResolvable} permissions The permissions of the role
* @param {string} [reason] Reason for changing the role's permissions
* @returns {Promise<Role>}
* @example

View File

@@ -0,0 +1,26 @@
const Permissions = require('../../util/Permissions');
const Collection = require('../../util/Collection');
module.exports = function resolvePermissions(overwrites, guild) {
if (overwrites instanceof Collection || overwrites instanceof Array) {
overwrites = overwrites.map(overwrite => {
const role = this.client.resolver.resolveRole(guild, overwrite.id);
if (role) {
overwrite.id = role.id;
overwrite.type = 'role';
} else {
overwrite.id = this.client.resolver.resolveUserID(overwrite.id);
overwrite.type = 'member';
}
return {
allow: Permissions.resolve(overwrite.allow || overwrite.allowed || 0),
deny: Permissions.resolve(overwrite.deny || overwrite.denied || 0),
type: overwrite.type,
id: overwrite.id,
};
});
}
return overwrites;
};