mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
Created Role class and permission evaluation within Roles
This commit is contained in:
52
src/structures/Role.js
Normal file
52
src/structures/Role.js
Normal file
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
const Constants = require('../Util/Constants');
|
||||
|
||||
class Role {
|
||||
constructor(guild, data) {
|
||||
this.guild = guild;
|
||||
this.client = guild.client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.id = data.id;
|
||||
this.name = data.name;
|
||||
this.color = data.color;
|
||||
this.hoist = data.hoist;
|
||||
this.position = data.position;
|
||||
this.permissions = data.permissions;
|
||||
this.managed = data.managed;
|
||||
}
|
||||
|
||||
serialize() {
|
||||
let serializedPermissions = {};
|
||||
for (let permissionName in Constants.PermissionFlags) {
|
||||
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||
}
|
||||
|
||||
return serializedPermissions;
|
||||
}
|
||||
|
||||
hasPermission(permission, explicit) {
|
||||
if (permission instanceof String || typeof permission === 'string') {
|
||||
permission = Constants.PermissionFlags[permission];
|
||||
}
|
||||
|
||||
if (!permission) {
|
||||
throw Constants.Errors.NOT_A_PERMISSION;
|
||||
}
|
||||
|
||||
if (!explicit) {
|
||||
if ((this.permissions & Constants.PermissionFlags.MANAGE_ROLES) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return ((this.permissions & permission) > 0);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Role;
|
||||
Reference in New Issue
Block a user