From a6c91d5e27469f27c936c817f0ede8e09ae6f100 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Sat, 28 Apr 2018 00:15:17 +0100 Subject: [PATCH] =?UTF-8?q?Add=20updating=20a=20user=E2=80=99s=20roles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/structs/discord/user.js | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/client/src/structs/discord/user.js b/client/src/structs/discord/user.js index 295c88a2..2a75f481 100644 --- a/client/src/structs/discord/user.js +++ b/client/src/structs/discord/user.js @@ -10,6 +10,7 @@ import { DiscordApi, DiscordApiModules as Modules } from 'modules'; import { List, InsufficientPermissions } from 'structs'; +import { Utils } from 'common'; import { Guild } from './guild'; import { Channel } from './channel'; @@ -277,4 +278,51 @@ export class GuildMember { undeafen() { this.deafen(false); } + + /** + * Gives this user a role. + * @param {Role} role The role to add + * @return {Promise} + */ + addRole(...roles) { + const newRoles = this.roleIds.concat([]); + let changed = false; + for (let role of roles) { + if (newRoles.includes(role.id || role)) continue; + newRoles.push(role.id || role); + changed = true; + } + if (!changed) return; + return this.updateRoles(newRoles); + } + + /** + * Removes a role from this user. + * @param {Role} role The role to remove + * @return {Promise} + */ + removeRole(...roles) { + const newRoles = this.roleIds.concat([]); + let changed = false; + for (let role of roles) { + if (!newRoles.includes(role.id || role)) continue; + Utils.removeFromArray(newRoles, role.id || role); + changed = true; + } + if (!changed) return; + return this.updateRoles(newRoles); + } + + /** + * Updates this user's roles. + * @param {Array} roles An array of Role objects or role IDs + * @return {Promise} + */ + updateRoles(roles) { + roles = roles.map(r => r.id || r); + return Modules.APIModule.patch({ + url: `${Modules.DiscordConstants.Endpoints.GUILD_MEMBERS(this.guildId)}/${this.userId}`, + body: { roles } + }); + } }