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 <bdistin@gmail.com>

* Remove unreachable code

* Gotta keep the linter happy

* Apply bdistin suggested change to both methods
This commit is contained in:
Ryan Munro
2019-09-10 18:55:42 +10:00
committed by SpaceEEC
parent 4fc461c2f9
commit a6810e2eaa
3 changed files with 22 additions and 2 deletions

View File

@@ -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

View File

@@ -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);
}
}

2
typings/index.d.ts vendored
View File

@@ -99,6 +99,7 @@ declare module 'discord.js' {
constructor(bits?: BitFieldResolvable<S>);
public bitfield: number;
public add(...bits: BitFieldResolvable<S>[]): BitField<S>;
public any(bit: BitFieldResolvable<S>): boolean;
public equals(bit: BitFieldResolvable<S>): boolean;
public freeze(): Readonly<BitField<S>>;
public has(bit: BitFieldResolvable<S>): boolean;
@@ -1111,6 +1112,7 @@ declare module 'discord.js' {
}
export class Permissions extends BitField<PermissionString> {
public any(permission: PermissionResolvable, checkAdmin?: boolean): boolean;
public has(permission: PermissionResolvable, checkAdmin?: boolean): boolean;
public static ALL: number;