From 4de1f4ce991a142851c2888e4cead6fb5af2c0c2 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sun, 17 Apr 2016 15:03:23 +0100 Subject: [PATCH] Created Role class and permission evaluation within Roles --- src/structures/Guild.js | 12 ++++- src/structures/{Member.js => GuildMember.js} | 4 +- src/structures/Role.js | 52 ++++++++++++++++++++ src/util/Constants.js | 28 +++++++++++ 4 files changed, 92 insertions(+), 4 deletions(-) rename src/structures/{Member.js => GuildMember.js} (91%) create mode 100644 src/structures/Role.js diff --git a/src/structures/Guild.js b/src/structures/Guild.js index f61b3c259..920dc64f7 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -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)); + } + }; } } diff --git a/src/structures/Member.js b/src/structures/GuildMember.js similarity index 91% rename from src/structures/Member.js rename to src/structures/GuildMember.js index 2a39e5481..1f6c10187 100644 --- a/src/structures/Member.js +++ b/src/structures/GuildMember.js @@ -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; diff --git a/src/structures/Role.js b/src/structures/Role.js new file mode 100644 index 000000000..30dc865a3 --- /dev/null +++ b/src/structures/Role.js @@ -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; diff --git a/src/util/Constants.js b/src/util/Constants.js index 5adc1636c..674ce7287 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -20,6 +20,7 @@ const Errors = exports.Errors = { NO_BOT_ACCOUNT: new Error('you should ideally be using a bot account!'), BAD_WS_MESSAGE: new Error('a bad message was received from the websocket - bad compression or not json'), TOOK_TOO_LONG: new Error('something took too long to do'), + NOT_A_PERMISSION: new Error('that is not a valid permission number'), }; const API = 'https://discordapp.com/api'; @@ -116,3 +117,30 @@ const WSEvents = exports.WSEvents = { FRIEND_ADD: 'RELATIONSHIP_ADD', FRIEND_REMOVE: 'RELATIONSHIP_REMOVE', }; + +const PermissionFlags = exports.PermissionFlags = { + CREATE_INSTANT_INVITE: 1 << 0, + KICK_MEMBERS: 1 << 1, + BAN_MEMBERS: 1 << 2, + MANAGE_ROLES: 1 << 3, + MANAGE_CHANNELS: 1 << 4, + MANAGE_GUILD: 1 << 5, + + READ_MESSAGES: 1 << 10, + SEND_MESSAGES: 1 << 11, + SEND_TTS_MESSAGES: 1 << 12, + MANAGE_MESSAGES: 1 << 13, + EMBED_LINKS: 1 << 14, + ATTACH_FILES: 1 << 15, + READ_MESSAGE_HISTORY: 1 << 16, + MENTION_EVERYONE: 1 << 17, + + CONNECT: 1 << 20, + SPEAK: 1 << 21, + MUTE_MEMBERS: 1 << 22, + DEAFEN_MEMBERS: 1 << 23, + MOVE_MEMBERS: 1 << 24, + USE_VAD: 1 << 25, +}; + +const DEFAULT_PERMISSIONS = exports.DEFAULT_PERMISSIONS = 36953089;