npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bloomlabs/permission-calculator

v0.0.1

Published

A utility package to manage Discord permission flags using bitwise operations, built for ease of use and flexibility with TypeScript.

Downloads

62

Readme

@bloomlabs/permission-calculator

A utility package to manage Discord permission flags using bitwise operations, built for ease of use and flexibility with TypeScript.

Features

  • Define and manage permission flags using bigint values.
  • Add, remove, and check permissions with an intuitive API.
  • Support for resolving permissions by their names or bigint values.
  • Retrieve a list of permission names from a combined permission value.

Installation

Install the package:

npm install @bloomlabs/permission-calculator
yarn add @bloomlabs/permission-calculator
pnpm add @bloomlabs/permission-calculator
bun add @bloomlabs/permission-calculator

Usage

Importing the Package

import {
  PermissionFlags,
  PermissionCalculator,
} from "@bloomlabs/permission-calculator";

Creating a Permission Calculator

// Initialize a PermissionCalculator with default permissions (none)
const permissions = new PermissionCalculator();

// Add permission to SEND_MESSAGES and MANAGE_CHANNELS
permissions.add(["SEND_MESSAGES", "MANAGE_CHANNELS"]);

// Check if a permission is present
console.log(permissions.has("SEND_MESSAGES")); // true

// Remove a permission
permissions.remove("SEND_MESSAGES");

// Get the combined permission value
console.log(permissions.value); // Permission value as bigint

// Get the list of permissions enabled
console.log(permissions.getPermissionsList()); // ['MANAGE_CHANNELS']

Adding and Removing Permissions

// Add a single permission
permissions.add("BAN_MEMBERS");

// Add multiple permissions
permissions.add(["VIEW_AUDIT_LOG", "KICK_MEMBERS"]);

// Remove a permission
permissions.remove("BAN_MEMBERS");

Checking Permissions

if (permissions.has("ADMINISTRATOR")) {
  console.log("User has administrator permissions");
}

Static Methods

  • PermissionCalculator.getPermissionsFromValue(value): Get the list of permission names from a combined permission value.
  • PermissionCalculator.parsePermissions(value): Parse a permission or an array of permissions into a combined bigint value.
const permissionsList = PermissionCalculator.getPermissionsFromValue(
  permissions.value
);
console.log(permissionsList); // Output: ['VIEW_AUDIT_LOG', 'KICK_MEMBERS']

API Reference

PermissionFlags

A constant object containing all available permission flags. Each flag is a bigint value.

PermissionCalculator

A class to manage permissions using bitwise operations.

Methods

  • add(permission: PermissionFlagResolvable): this: Adds one or more permissions.
  • remove(permission: PermissionFlagResolvable): this: Removes one or more permissions.
  • has(permission: PermissionFlagResolvable): boolean: Checks if one or more permissions are included.
  • get value(): bigint: Returns the combined permissions as a bigint.
  • getPermissionsList(): string[]: Returns a list of permission names included in the current permissions.
  • static getPermissionsFromValue(value: bigint | string | number | undefined | null): string[]: Returns a list of permission names from a combined permission value.
  • static parse(value: bigint | string | number | undefined | null): bigint: Parses a value into a bigint.
  • static parsePermissions(value: PermissionFlagResolvable): bigint: Parses a permission or array of permissions into a bigint.

License

This package is licensed under the MIT License. See LICENSE for more details.


Enjoy using the @bloomlabs/permission-calculator package! Feedback is welcome!