Skip to main content
Version: 0.6.4

Class: BitField

Extended by​

Constructors​

Constructor​

new BitField(bits?): BitField

Parameters​

ParameterType
bits?BitFieldResolvable

Returns​

BitField

Properties​

bitfield​

bitfield: number | bigint


DefaultBit​

static DefaultBit: number | bigint = 0


Flags​

static Flags: Record<string, number | bigint> = {}

Methods​

[iterator]()​

[iterator](...hasParams): IterableIterator<string>

Iterates over the names of all flags that are set in this bitfield.

Parameters​

ParameterType
...hasParamsany[]

Returns​

IterableIterator<string>

Example​

for (const flag of perms) {
console.log(flag); // 'SendMessage', 'ViewChannel', etc.
}

add()​

add(...bits): this

Adds one or more bits to this bitfield. If the instance is frozen, returns a new BitField with the bits added.

Parameters​

ParameterTypeDescription
...bitsBitFieldResolvable[]The bit(s) to add.

Returns​

this

This instance (or a new one if frozen), with the bits added.

Example​

perms.add("SendMessage", "ViewChannel");

any()​

any(bit): boolean

Checks whether this bitfield has any of the given bits set.

Parameters​

ParameterTypeDescription
bitBitFieldResolvableThe bit(s) to test against.

Returns​

boolean

true if at least one of the provided bits is set.

Example​

perms.any(["SendMessage", "ManageChannel"]); // true if either is set

equals()​

equals(bit): boolean

Checks whether this bitfield is exactly equal to the given bit value.

Parameters​

ParameterTypeDescription
bitBitFieldResolvableThe bit(s) to compare against.

Returns​

boolean

true if the resolved bit value is identical to this bitfield.

Example​

new Permissions(0n).equals(0n); // true

freeze()​

freeze(): Readonly<this>

Freezes this BitField instance, making it immutable. Subsequent calls to add() or remove() will return a new instance instead of mutating this one.

Returns​

Readonly<this>

This instance, frozen.


has()​

has(bit, ..._hasParams): boolean

Checks whether this bitfield has all of the given bits set.

Parameters​

ParameterTypeDescription
bitBitFieldResolvableThe bit(s) to check for.
..._hasParamsany[]-

Returns​

boolean

true if every provided bit is set in this bitfield.

Example​

perms.has("SendMessage");
perms.has(["SendMessage", "ViewChannel"]); // true only if both are set

missing()​

missing(bits, ...hasParams): string[]

Returns the flag names present in bits that are missing from this bitfield.

Parameters​

ParameterTypeDescription
bitsBitFieldResolvableThe bits to check against.
...hasParamsany[]-

Returns​

string[]

An array of flag name strings that are in bits but not in this bitfield.

Example​

// If perms only has SendMessage:
perms.missing(["SendMessage", "ManageChannel"]); // ['ManageChannel']

remove()​

remove(...bits): this

Removes one or more bits from this bitfield. If the instance is frozen, returns a new BitField with the bits removed.

Parameters​

ParameterTypeDescription
...bitsBitFieldResolvable[]The bit(s) to remove.

Returns​

this

This instance (or a new one if frozen), with the bits cleared.

Example​

perms.remove("ManageChannel");

serialize()​

serialize(...hasParams): Record<string, boolean>

Serializes this bitfield to a plain object mapping each flag name to a boolean indicating whether that flag is set.

Parameters​

ParameterType
...hasParamsany[]

Returns​

Record<string, boolean>

A record of { [flagName]: boolean } for all defined flags.

Example​

perms.serialize();
// { SendMessage: true, ManageChannel: false, ... }

toArray()​

toArray(...hasParams): string[]

Returns an array of flag names that are set in this bitfield.

Parameters​

ParameterType
...hasParamsany[]

Returns​

string[]

An array of string flag names.

Example​

perms.toArray(); // ['SendMessage', 'ViewChannel']

toJSON()​

toJSON(): string | number

Returns the bitfield value as a JSON-safe primitive. Numbers are returned as-is; bigints are converted to their string representation to avoid JSON serialization errors.

Returns​

string | number


valueOf()​

valueOf(): number | bigint

Returns the underlying numeric value of this bitfield. Allows BitField instances to be used directly in arithmetic expressions.

Returns​

number | bigint


resolve()​

static resolve(bit?): number | bigint

Resolves a BitFieldResolvable into a raw number | bigint.

Resolution rules (in order):

  • undefined → DefaultBit
  • A number or bigint matching DefaultBit's type and >= 0 → returned as-is
  • A BitField instance → its .bitfield value
  • An array → each element resolved and OR'd together
  • A numeric string → parsed as BigInt or Number depending on DefaultBit's type
  • A named string flag → looked up in Flags
  • Anything else → throws RangeError

Parameters​

ParameterTypeDescription
bit?BitFieldResolvableThe value to resolve.

Returns​

number | bigint

Throws​

If the value cannot be resolved to a valid bit.