Files
discord.js/src/structures/EvaluatedPermissions.js
Schuyler Cebulskie b7f582b7f0 Clean up a bunch of stuff
- Channel typing data is now a Map
- Client properties on structures are now non-enumerable and
non-configurable
2016-09-07 00:24:45 -04:00

49 lines
1.4 KiB
JavaScript

const Constants = require('../util/Constants');
/**
* The final evaluated permissions for a member in a channel
*/
class EvaluatedPermissions {
constructor(member, permissions) {
/**
* The member this permissions refer to
* @type {GuildMember}
*/
this.member = member;
/**
* A number representing the packed permissions.
* @private
* @type {number}
*/
this.permissions = permissions;
}
/**
* Get an object mapping permission name, e.g. `READ_MESSAGES` to a boolean - whether the user
* can perform this or not.
* @returns {Object<string, boolean>}
*/
serialize() {
const serializedPermissions = {};
for (const permissionName in Constants.PermissionFlags) {
serializedPermissions[permissionName] = this.hasPermission(permissionName);
}
return serializedPermissions;
}
/**
* Checks whether the user has a certain permission, e.g. `READ_MESSAGES`.
* @param {PermissionResolvable} permission The permission to check for
* @param {boolean} [explicit=false] Whether to require the user to explicitly have the exact permission
* @returns {boolean}
*/
hasPermission(permission, explicit = false) {
permission = this.member.client.resolver.resolvePermission(permission);
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
return (this.permissions & permission) > 0;
}
}
module.exports = EvaluatedPermissions;