@gruvjs/permissions 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Saiteja Madha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ <div align="center">
2
+ <br />
3
+ <p>
4
+ <a href="https://github.com/gruvjs/gruvjs"><img src="https://raw.githubusercontent.com/gruvjs/gruvjs/main/assets/logo.svg" width="546" alt="gruvjs" /></a>
5
+ </p>
6
+ <br />
7
+ <p>
8
+ <a href="https://www.npmjs.com/package/@gruvjs/permissions"><img src="https://img.shields.io/npm/v/@gruvjs/permissions.svg?maxAge=3600" alt="npm version" /></a>
9
+ <a href="https://www.npmjs.com/package/@gruvjs/permissions"><img src="https://img.shields.io/npm/dt/@gruvjs/permissions.svg?maxAge=3600" alt="npm downloads" /></a>
10
+ </p>
11
+ </div>
12
+
13
+ ## About
14
+
15
+ `@gruvjs/permissions` is a human-readable Discord permission bitfield manager. No discord.js required.
16
+
17
+ Node.js 18.0.0 or newer is required.
18
+
19
+ ## Installation
20
+
21
+ ```sh
22
+ npm install @gruvjs/permissions
23
+ yarn add @gruvjs/permissions
24
+ pnpm add @gruvjs/permissions
25
+ ```
26
+
27
+ ## Examples
28
+
29
+ ```js
30
+ const { Permissions } = require("@gruvjs/permissions");
31
+
32
+ // From a raw Discord bitfield (e.g. from a guild member object)
33
+ const perms = Permissions.from(8n);
34
+ perms.has("ADMINISTRATOR"); // → true
35
+ perms.has("SEND_MESSAGES"); // → true (ADMINISTRATOR bypasses all checks)
36
+
37
+ // Build from permission names
38
+ const modPerms = new Permissions(["KICK_MEMBERS", "BAN_MEMBERS", "MANAGE_MESSAGES"]);
39
+ modPerms.has("KICK_MEMBERS"); // → true
40
+ modPerms.has("ADMINISTRATOR"); // → false
41
+
42
+ // Check if any match (useful for "can moderate" checks)
43
+ modPerms.any(["KICK_MEMBERS", "BAN_MEMBERS", "MODERATE_MEMBERS"]); // → true
44
+
45
+ // Find what's missing
46
+ modPerms.missing(["KICK_MEMBERS", "MANAGE_ROLES"]); // → ["MANAGE_ROLES"]
47
+
48
+ // Immutable add/remove
49
+ const updated = modPerms.add("MANAGE_ROLES");
50
+ const reduced = modPerms.remove("BAN_MEMBERS");
51
+
52
+ // All set permission names
53
+ modPerms.toArray(); // → ["KICK_MEMBERS", "BAN_MEMBERS", "MANAGE_MESSAGES"]
54
+ ```
55
+
56
+ See the [docs](./docs) folder for detailed usage.
57
+
58
+ ## Links
59
+
60
+ - [npm](https://www.npmjs.com/package/@gruvjs/permissions)
61
+ - [GitHub](https://github.com/gruvjs/gruvjs/tree/main/packages/permissions)
62
+ - [gruvjs monorepo](https://github.com/gruvjs/gruvjs)
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@gruvjs/permissions",
3
+ "version": "1.0.0",
4
+ "description": "Human-readable Discord permission bitfields. No discord.js required.",
5
+ "main": "src/index.js",
6
+ "author": "gruvjs",
7
+ "license": "MIT",
8
+ "engines": {
9
+ "node": ">=18.0.0"
10
+ },
11
+ "keywords": [
12
+ "discord",
13
+ "permissions",
14
+ "bitfield",
15
+ "bot",
16
+ "zero-dependency"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/gruvjs/gruvjs.git",
21
+ "directory": "packages/permissions"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/gruvjs/gruvjs/issues"
25
+ },
26
+ "homepage": "https://github.com/gruvjs/gruvjs/tree/main/packages/permissions#readme",
27
+ "files": [
28
+ "src/",
29
+ "README.md",
30
+ "LICENSE"
31
+ ],
32
+ "dependencies": {},
33
+ "scripts": {
34
+ "test": "node tests/run.js"
35
+ }
36
+ }
@@ -0,0 +1,80 @@
1
+ const { PermissionsBits } = require("../data/PermissionsBits");
2
+
3
+ /**
4
+ * Wraps a Discord permission bitfield in a clean, human-readable API.
5
+ * Does not require discord.js.
6
+ */
7
+ class Permissions {
8
+ /**
9
+ * @param {bigint|number|string|string[]} [input]
10
+ * - bigint / number → raw bitfield
11
+ * - string → single permission name (e.g. "ADMINISTRATOR")
12
+ * - string[] → array of permission names
13
+ */
14
+ constructor(input) {
15
+ this._bitfield = Permissions._resolve(input ?? 0n);
16
+ }
17
+
18
+ // ── Checks ─────────────────────────────────────────────────────────────────
19
+
20
+ /** Returns true if ALL of the given permissions are set. ADMINISTRATOR always returns true. */
21
+ has(perms) {
22
+ const bits = Permissions._resolve(perms);
23
+ if (this._bitfield & PermissionsBits.ADMINISTRATOR) return true;
24
+ return (this._bitfield & bits) === bits;
25
+ }
26
+
27
+ /** Returns true if ANY of the given permissions are set. ADMINISTRATOR always returns true. */
28
+ any(perms) {
29
+ const bits = Permissions._resolve(perms);
30
+ if (this._bitfield & PermissionsBits.ADMINISTRATOR) return true;
31
+ return (this._bitfield & bits) !== 0n;
32
+ }
33
+
34
+ /** Returns the permission names that are in perms but NOT in this bitfield. */
35
+ missing(perms) {
36
+ return perms.filter((p) => !this.has(p));
37
+ }
38
+
39
+ // ── Mutation (returns new instance — originals are never mutated) ───────────
40
+
41
+ /** Returns a new Permissions with the given permissions added. */
42
+ add(perms) { return new Permissions(this._bitfield | Permissions._resolve(perms)); }
43
+
44
+ /** Returns a new Permissions with the given permissions removed. */
45
+ remove(perms) { return new Permissions(this._bitfield & ~Permissions._resolve(perms)); }
46
+
47
+ // ── Output ─────────────────────────────────────────────────────────────────
48
+
49
+ get bitfield() { return this._bitfield; }
50
+
51
+ /** Returns all set permission names. Does NOT expand ADMINISTRATOR. */
52
+ toArray() {
53
+ return Object.entries(PermissionsBits)
54
+ .filter(([, bit]) => (this._bitfield & bit) === bit)
55
+ .map(([name]) => name);
56
+ }
57
+
58
+ /** Returns the bitfield as a decimal string — safe for JSON serialization. */
59
+ toString() { return this._bitfield.toString(); }
60
+ toJSON() { return this.toString(); }
61
+
62
+ // ── Static ─────────────────────────────────────────────────────────────────
63
+
64
+ static from(bitfield) { return new Permissions(bitfield); }
65
+ static resolve(names) { return Permissions._resolve(names); }
66
+
67
+ static _resolve(input) {
68
+ if (typeof input === "bigint") return input;
69
+ if (typeof input === "number") return BigInt(input);
70
+ if (typeof input === "string") {
71
+ const bit = PermissionsBits[input];
72
+ if (bit === undefined) throw new RangeError(`Unknown permission: "${input}"`);
73
+ return bit;
74
+ }
75
+ if (Array.isArray(input)) return input.reduce((acc, p) => acc | Permissions._resolve(p), 0n);
76
+ throw new TypeError(`Cannot resolve permissions from: ${typeof input}`);
77
+ }
78
+ }
79
+
80
+ module.exports = { Permissions };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * All Discord permission flags as named BigInt constants.
3
+ * Matches the Discord API permission bitfield values.
4
+ * @type {Readonly<Record<string, bigint>>}
5
+ */
6
+ const PermissionsBits = Object.freeze({
7
+ CREATE_INSTANT_INVITE: 1n << 0n,
8
+ KICK_MEMBERS: 1n << 1n,
9
+ BAN_MEMBERS: 1n << 2n,
10
+ ADMINISTRATOR: 1n << 3n,
11
+ MANAGE_CHANNELS: 1n << 4n,
12
+ MANAGE_GUILD: 1n << 5n,
13
+ ADD_REACTIONS: 1n << 6n,
14
+ VIEW_AUDIT_LOG: 1n << 7n,
15
+ PRIORITY_SPEAKER: 1n << 8n,
16
+ STREAM: 1n << 9n,
17
+ VIEW_CHANNEL: 1n << 10n,
18
+ SEND_MESSAGES: 1n << 11n,
19
+ SEND_TTS_MESSAGES: 1n << 12n,
20
+ MANAGE_MESSAGES: 1n << 13n,
21
+ EMBED_LINKS: 1n << 14n,
22
+ ATTACH_FILES: 1n << 15n,
23
+ READ_MESSAGE_HISTORY: 1n << 16n,
24
+ MENTION_EVERYONE: 1n << 17n,
25
+ USE_EXTERNAL_EMOJIS: 1n << 18n,
26
+ VIEW_GUILD_INSIGHTS: 1n << 19n,
27
+ CONNECT: 1n << 20n,
28
+ SPEAK: 1n << 21n,
29
+ MUTE_MEMBERS: 1n << 22n,
30
+ DEAFEN_MEMBERS: 1n << 23n,
31
+ MOVE_MEMBERS: 1n << 24n,
32
+ USE_VAD: 1n << 25n,
33
+ CHANGE_NICKNAME: 1n << 26n,
34
+ MANAGE_NICKNAMES: 1n << 27n,
35
+ MANAGE_ROLES: 1n << 28n,
36
+ MANAGE_WEBHOOKS: 1n << 29n,
37
+ MANAGE_GUILD_EXPRESSIONS: 1n << 30n,
38
+ USE_APPLICATION_COMMANDS: 1n << 31n,
39
+ REQUEST_TO_SPEAK: 1n << 32n,
40
+ MANAGE_EVENTS: 1n << 33n,
41
+ MANAGE_THREADS: 1n << 34n,
42
+ CREATE_PUBLIC_THREADS: 1n << 35n,
43
+ CREATE_PRIVATE_THREADS: 1n << 36n,
44
+ USE_EXTERNAL_STICKERS: 1n << 37n,
45
+ SEND_MESSAGES_IN_THREADS: 1n << 38n,
46
+ USE_EMBEDDED_ACTIVITIES: 1n << 39n,
47
+ MODERATE_MEMBERS: 1n << 40n,
48
+ VIEW_CREATOR_MONETIZATION_ANALYTICS: 1n << 41n,
49
+ USE_SOUNDBOARD: 1n << 42n,
50
+ CREATE_GUILD_EXPRESSIONS: 1n << 43n,
51
+ CREATE_EVENTS: 1n << 44n,
52
+ USE_EXTERNAL_SOUNDS: 1n << 45n,
53
+ SEND_VOICE_MESSAGES: 1n << 46n,
54
+ SEND_POLLS: 1n << 49n,
55
+ USE_EXTERNAL_APPS: 1n << 50n,
56
+ });
57
+
58
+ module.exports = { PermissionsBits };
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Data
2
+ const { PermissionsBits } = require("./data/PermissionsBits");
3
+
4
+ // Core
5
+ const { Permissions } = require("./core/Permissions");
6
+
7
+ module.exports = { Permissions, PermissionsBits };