Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BitField): throw an error if bit to resolve is undefined #5565

Merged
merged 9 commits into from Jun 9, 2021
6 changes: 3 additions & 3 deletions src/util/BitField.js
Expand Up @@ -38,10 +38,11 @@ class BitField {
/**
* Checks whether the bitfield has a bit, or multiple bits.
* @param {BitFieldResolvable} bit Bit(s) to check for
* @param {...*} hasParams Additional parameters for the has method, if any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what exactly this solves, but I'd rather drop it and the recursion that requires it.
BitField.resolve handles arrays just fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, it used to return true whenever they had admin no matter if you actually passed true to checkAdmin.
I still do not really understand the issue here, anyway as you said <BitField>.constructor.resolve handles arrays well already.

* @returns {boolean}
*/
has(bit) {
if (Array.isArray(bit)) return bit.every(p => this.has(p));
has(bit, ...hasParams) {
if (Array.isArray(bit)) return bit.every(p => this.has(p, ...hasParams));
bit = this.constructor.resolve(bit);
return (this.bitfield & bit) === bit;
}
Expand Down Expand Up @@ -143,7 +144,6 @@ class BitField {
*/
static resolve(bit) {
const { defaultBit } = this;
if (typeof bit === 'undefined') return defaultBit;
if (typeof defaultBit === typeof bit && bit >= defaultBit) return bit;
if (bit instanceof BitField) return bit.bitfield;
if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, defaultBit);
Expand Down
2 changes: 1 addition & 1 deletion src/util/Permissions.js
Expand Up @@ -41,7 +41,7 @@ class Permissions extends BitField {
* @returns {boolean}
*/
has(permission, checkAdmin = true) {
return (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) || super.has(permission);
return (checkAdmin && super.has(this.constructor.FLAGS.ADMINISTRATOR)) || super.has(permission, checkAdmin);
}
}

Expand Down
2 changes: 1 addition & 1 deletion typings/index.d.ts
Expand Up @@ -203,7 +203,7 @@ declare module 'discord.js' {
public any(bit: BitFieldResolvable<S, N>): boolean;
public equals(bit: BitFieldResolvable<S, N>): boolean;
public freeze(): Readonly<BitField<S, N>>;
public has(bit: BitFieldResolvable<S, N>): boolean;
public has(bit: BitFieldResolvable<S, N>, ...hasParam: readonly unknown[]): boolean;
public missing(bits: BitFieldResolvable<S, N>, ...hasParam: readonly unknown[]): S[];
public remove(...bits: BitFieldResolvable<S, N>[]): BitField<S, N>;
public serialize(...hasParam: readonly unknown[]): Record<S, boolean>;
Expand Down