mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
Created Role class and permission evaluation within Roles
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
const User = require('./User');
|
||||
const Member = require('./Member');
|
||||
const GuildMember = require('./GuildMember');
|
||||
const GuildDataStore = require('./datastore/GuildDataStore');
|
||||
const TextChannel = require('./TextChannel');
|
||||
const VoiceChannel = require('./VoiceChannel');
|
||||
const Constants = require('../Util/Constants');
|
||||
const Role = require('./Role');
|
||||
|
||||
class Guild {
|
||||
constructor(client, data) {
|
||||
@@ -27,7 +28,7 @@ class Guild {
|
||||
|
||||
_addMember(guildUser) {
|
||||
guildUser.user = this.client.store.NewUser(guildUser.user);
|
||||
let member = this.store.add('members', new Member(this, guildUser));
|
||||
let member = this.store.add('members', new GuildMember(this, guildUser));
|
||||
if (this.client.ws.emittedReady) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
||||
}
|
||||
@@ -81,6 +82,13 @@ class Guild {
|
||||
this.client.store.NewChannel(channel, this);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.roles) {
|
||||
this.store.clear('roles');
|
||||
for (let role of data.roles) {
|
||||
this.store.add('roles', new Role(this, role));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
class Member {
|
||||
class GuildMember {
|
||||
constructor(guild, data) {
|
||||
this.client = guild.client;
|
||||
this.guild = guild;
|
||||
@@ -39,4 +39,4 @@ class Member {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Member;
|
||||
module.exports = GuildMember;
|
||||
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