From e061d95ae67db4bc7a95dc59e0a8b59dbce2a823 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Sat, 14 Apr 2018 14:02:42 +0100 Subject: [PATCH] Add roles --- client/src/structs/discord/guild.js | 40 ++++++++++++++++++++++++++++- client/src/structs/discord/user.js | 13 ++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/client/src/structs/discord/guild.js b/client/src/structs/discord/guild.js index b6c4bf9e..0948e9ce 100644 --- a/client/src/structs/discord/guild.js +++ b/client/src/structs/discord/guild.js @@ -4,6 +4,37 @@ import { List } from 'structs'; import { Channel } from './channel'; import { GuildMember } from './user'; +const roles = new WeakMap(); + +export class Role { + constructor(data, guild_id) { + if (roles.has(data)) return roles.get(data); + roles.set(data, this); + + this.discordObject = data; + this.guild_id = guild_id; + } + + get id() { return this.discordObject.id } + get name() { return this.discordObject.name } + get position() { return this.discordObject.position } + get original_position() { return this.discordObject.originalPosition } + get permissions() { return this.discordObject.permissions } + get managed() { return this.discordObject.managed } + get mentionable() { return this.discordObject.mentionable } + get hoist() { return this.discordObject.hoist } + get colour() { return this.discordObject.color } + get colour_string() { return this.discordObject.colorString } + + get guild() { + return Guild.fromId(this.guild_id); + } + + get members() { + return this.guild.members.filter(m => m.roles.includes(this)); + } +} + const guilds = new WeakMap(); export class Guild { @@ -20,8 +51,10 @@ export class Guild { if (guild) return new Guild(guild); } + static get Role() { return Role } + get id() { return this.discordObject.id } - get owner_id() { return this.discordObject.owner_id } + get owner_id() { return this.discordObject.ownerId } get application_id() { return this.discordObject.application_id } get system_channel_id() { return this.discordObject.systemChannelId } get name() { return this.discordObject.name } @@ -44,6 +77,11 @@ export class Guild { return this.members.find(m => m.id === this.owner_id); } + get roles() { + return List.from(Object.entries(this.discordObject.roles), ([i, r]) => new Role(r, this.id)) + .sort((r1, r2) => r1.position === r2.position ? 0 : r1.position > r2.position ? 1 : -1); + } + get channels() { const channels = Modules.GuildChannelsStore.getChannels(this.id); const returnChannels = new List(); diff --git a/client/src/structs/discord/user.js b/client/src/structs/discord/user.js index b7d21251..210c7066 100644 --- a/client/src/structs/discord/user.js +++ b/client/src/structs/discord/user.js @@ -43,6 +43,10 @@ export class User { get is_local_bot() { return this.discordObject.isLocalBot() } get is_phone_verified() { return this.discordObject.isPhoneVerified() } + get guilds() { + return DiscordApi.guilds.filter(g => g.members.find(m => m.id === this.id)); + } + async ensurePrivateChannel() { if (DiscordApi.currentUser.id === this.id) throw new Error('Cannot create a direct message channel to the current user.'); @@ -105,6 +109,15 @@ export class GuildMember extends User { return Guild.fromId(this.guild_id); } + get roles() { + return List.from(this.member_data.roles, id => this.guild.roles.find(r => r.id === id)) + .sort((r1, r2) => r1.position === r2.position ? 0 : r1.position > r2.position ? 1 : -1); + } + + get hoist_role() { + return this.guild.roles.find(r => r.id === this.hoist_role_id); + } + checkPermissions(perms) { return Modules.PermissionUtils.can(perms, DiscordApi.currentUser, Modules.GuildStore.getGuild(this.guild_id)); }