From c058020a7ca033077755594e0cb0a0658d10596f Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 22 Feb 2021 08:20:42 +0100 Subject: :bug: fix Permission BitField --- src/util/BitField.ts | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/util') diff --git a/src/util/BitField.ts b/src/util/BitField.ts index 17eef796..aba98d75 100644 --- a/src/util/BitField.ts +++ b/src/util/BitField.ts @@ -11,24 +11,24 @@ export type BitFieldResolvable = number | BigInt | BitField | string | BitFieldR export class BitField { public bitfield: bigint = BigInt(0); - public static FLAGS: Record; + public static FLAGS: Record = {}; constructor(bits: BitFieldResolvable = 0) { - this.bitfield = BitField.resolve(bits); + this.bitfield = BitField.resolve.call(this, bits); } /** * Checks whether the bitfield has a bit, or any of multiple bits. */ any(bit: BitFieldResolvable): boolean { - return (this.bitfield & BitField.resolve(bit)) !== 0n; + return (this.bitfield & BitField.resolve.call(this, bit)) !== 0n; } /** * Checks if this bitfield equals another */ equals(bit: BitFieldResolvable): boolean { - return this.bitfield === BitField.resolve(bit); + return this.bitfield === BitField.resolve.call(this, bit); } /** @@ -36,7 +36,7 @@ export class BitField { */ has(bit: BitFieldResolvable): boolean { if (Array.isArray(bit)) return bit.every((p) => this.has(p)); - const BIT = BitField.resolve(bit); + const BIT = BitField.resolve.call(this, bit); return (this.bitfield & BIT) === BIT; } @@ -63,7 +63,7 @@ export class BitField { add(...bits: BitFieldResolvable[]): BitField { let total = 0n; for (const bit of bits) { - total |= BitField.resolve(bit); + total |= BitField.resolve.call(this, bit); } if (Object.isFrozen(this)) return new BitField(this.bitfield | total); this.bitfield |= total; @@ -77,7 +77,7 @@ export class BitField { remove(...bits: BitFieldResolvable[]) { let total = 0n; for (const bit of bits) { - total |= BitField.resolve(bit); + total |= BitField.resolve.call(this, bit); } if (Object.isFrozen(this)) return new BitField(this.bitfield & ~total); this.bitfield &= ~total; @@ -128,11 +128,16 @@ export class BitField { * @returns {number} */ static resolve(bit: BitFieldResolvable = 0n): bigint { + // @ts-ignore + const FLAGS = this.FLAGS || this.constructor?.FLAGS; if ((typeof bit === "number" || typeof bit === "bigint") && bit >= 0n) return BigInt(bit); if (bit instanceof BitField) return bit.bitfield; - if (Array.isArray(bit)) - return bit.map((p) => this.resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n); - if (typeof bit === "string" && typeof this.FLAGS[bit] !== "undefined") return this.FLAGS[bit]; + if (Array.isArray(bit)) { + // @ts-ignore + const resolve = this.constructor?.resolve || this.resolve; + return bit.map((p) => resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n); + } + if (typeof bit === "string" && typeof FLAGS[bit] !== "undefined") return this.FLAGS[bit]; throw new RangeError("BITFIELD_INVALID: " + bit); } } -- cgit 1.4.1