From a6810e2eaaea4021d0a283c7f20ff6d643d34f4f Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Tue, 10 Sep 2019 18:55:42 +1000 Subject: [PATCH] feat(Permissions): add new method Permissions#any (#3450) * Add new method Permissions#any * Update src/util/BitField.js This is much better Co-Authored-By: bdistin * Remove unreachable code * Gotta keep the linter happy * Apply bdistin suggested change to both methods --- src/util/BitField.js | 9 +++++++++ src/util/Permissions.js | 13 +++++++++++-- typings/index.d.ts | 2 ++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/util/BitField.js b/src/util/BitField.js index 424a8443d..757da7ab9 100644 --- a/src/util/BitField.js +++ b/src/util/BitField.js @@ -17,6 +17,15 @@ class BitField { this.bitfield = this.constructor.resolve(bits); } + /** + * Checks whether the bitfield has a bit, or any of multiple bits. + * @param {BitFieldResolvable} bit Bit(s) to check for + * @returns {boolean} + */ + any(bit) { + return (this.bitfield & this.constructor.resolve(bit)) !== 0; + } + /** * Checks if this bitfield equals another * @param {BitFieldResolvable} bit Bit(s) to check for diff --git a/src/util/Permissions.js b/src/util/Permissions.js index ef5273ffd..831f17386 100644 --- a/src/util/Permissions.js +++ b/src/util/Permissions.js @@ -18,6 +18,16 @@ class Permissions extends BitField { * @typedef {string|number|Permissions|PermissionResolvable[]} PermissionResolvable */ + /** + * Checks whether the bitfield has a permission, or any of multiple permissions. + * @param {PermissionResolvable} permission Permission(s) to check for + * @param {boolean} [checkAdmin=true] Whether to allow the administrator permission to override + * @returns {boolean} + */ + any(permission, checkAdmin = true) { + return (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) || super.any(permission); + } + /** * Checks whether the bitfield has a permission, or multiple permissions. * @param {PermissionResolvable} permission Permission(s) to check for @@ -25,8 +35,7 @@ class Permissions extends BitField { * @returns {boolean} */ has(permission, checkAdmin = true) { - if (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) return true; - return super.has(permission); + return (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) || super.has(permission); } } diff --git a/typings/index.d.ts b/typings/index.d.ts index aaf9f524d..13405c3f0 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -99,6 +99,7 @@ declare module 'discord.js' { constructor(bits?: BitFieldResolvable); public bitfield: number; public add(...bits: BitFieldResolvable[]): BitField; + public any(bit: BitFieldResolvable): boolean; public equals(bit: BitFieldResolvable): boolean; public freeze(): Readonly>; public has(bit: BitFieldResolvable): boolean; @@ -1111,6 +1112,7 @@ declare module 'discord.js' { } export class Permissions extends BitField { + public any(permission: PermissionResolvable, checkAdmin?: boolean): boolean; public has(permission: PermissionResolvable, checkAdmin?: boolean): boolean; public static ALL: number;