-
Notifications
You must be signed in to change notification settings - Fork 417
/
Permission.js
67 lines (61 loc) · 2.21 KB
/
Permission.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use strict";
const Base = require("./Base");
const Permissions = require("../Constants").Permissions;
/**
* Represents a calculated permissions number
* @prop {Number} allow The allowed permissions number
* @prop {Number} deny The denied permissions number
* @prop {Object} json A JSON representation of the permissions number.
* If a permission key isn't there, it is not set by this permission.
* If a permission key is false, it is denied by the permission.
* If a permission key is true, it is allowed by the permission.
* i.e.:
* {
* "readMessages": true,
* "sendMessages": true,
* "manageMessages": false
* }
* In the above example, readMessages and sendMessages are allowed permissions, and manageMessages is denied. Everything else is not explicitly set.
* [A full list of permission nodes can be found on the docs reference page](/Eris/docs/reference)
*/
class Permission extends Base {
constructor(allow, deny) {
super();
this.allow = allow;
this.deny = deny || 0;
}
get json() {
if(!this._json) {
this._json = {};
for(var perm of Object.keys(Permissions)) {
if(!perm.startsWith("all")) {
if(this.allow & Permissions[perm]) {
this._json[perm] = true;
} else if(this.deny & Permissions[perm]) {
this._json[perm] = false;
}
}
}
}
return this._json;
}
/**
* Check if this permission allows a specific permission
* @arg {String} permission The name of the permission. [A full list of permission nodes can be found on the docs reference page](/Eris/docs/reference)
* @returns {Boolean} Whether the permission allows the specified permission
*/
has(permission) {
return !!(this.allow & Permissions[permission]);
}
toJSON() {
var base = super.toJSON(true);
if(base.hasOwnProperty("id")) {
delete base.id;
}
for(var prop of ["allow", "deny"]) {
base[prop] = this[prop] !== undefined && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
return base;
}
}
module.exports = Permission;