From 1e73c25fbfc9b3cb62bed719dc79de25f67707ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Rom=C3=A1n?= Date: Thu, 1 Jul 2021 23:32:03 +0200 Subject: [PATCH] feat(RoleManager): added `edit` method, alias `Role#edit` (#5983) Co-authored-by: monbrey --- src/managers/RoleManager.js | 50 ++++++++++++++++++++++++++++++++++++- src/structures/Role.js | 34 ++----------------------- typings/index.d.ts | 3 ++- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/src/managers/RoleManager.js b/src/managers/RoleManager.js index 98135cb84..42074aa32 100644 --- a/src/managers/RoleManager.js +++ b/src/managers/RoleManager.js @@ -1,10 +1,11 @@ 'use strict'; const BaseManager = require('./BaseManager'); +const { TypeError } = require('../errors'); const Role = require('../structures/Role'); const Collection = require('../util/Collection'); const Permissions = require('../util/Permissions'); -const { resolveColor } = require('../util/Util'); +const { resolveColor, setPosition } = require('../util/Util'); /** * Manages API methods for roles and stores their cache. @@ -143,6 +144,53 @@ class RoleManager extends BaseManager { }); } + /** + * Edits a role of the guild. + * @param {RoleResolvable} role The role to edit + * @param {RoleData} data The new data for the role + * @param {string} [reason] Reason for editing this role + * @returns {Promise} + * @example + * // Edit a role + * guild.roles.edit('222079219327434752', { name: 'buddies' }) + * .then(updated => console.log(`Edited role name to ${updated.name}`)) + * .catch(console.error); + */ + async edit(role, data, reason) { + role = this.resolve(role); + if (!role) throw new TypeError('INVALID_TYPE', 'role', 'RoleResolvable'); + + if (typeof data.position === 'number') { + const updatedRoles = await setPosition( + role, + data.position, + false, + this.guild._sortedRoles(), + this.client.api.guilds(this.guild.id).roles, + reason, + ); + + this.client.actions.GuildRolesPositionUpdate.handle({ + guild_id: this.guild.id, + roles: updatedRoles, + }); + } + + const _data = { + name: data.name, + color: typeof data.color === 'undefined' ? undefined : resolveColor(data.color), + hoist: data.hoist, + permissions: typeof data.permissions === 'undefined' ? undefined : new Permissions(data.permissions), + mentionable: data.mentionable, + }; + + const d = await this.client.api.guilds(this.guild.id).roles(role.id).patch({ data: _data, reason }); + + const clone = role._clone(); + clone._patch(d); + return clone; + } + /** * Gets the managed role a user created when joining the guild, if any * Only ever available for bots diff --git a/src/structures/Role.js b/src/structures/Role.js index d932246cf..c38ece9ef 100644 --- a/src/structures/Role.js +++ b/src/structures/Role.js @@ -196,38 +196,8 @@ class Role extends Base { * .then(updated => console.log(`Edited role name to ${updated.name}`)) * .catch(console.error); */ - async edit(data, reason) { - if (typeof data.position !== 'undefined') { - await Util.setPosition( - this, - data.position, - false, - this.guild._sortedRoles(), - this.client.api.guilds(this.guild.id).roles, - reason, - ).then(updatedRoles => { - this.client.actions.GuildRolesPositionUpdate.handle({ - guild_id: this.guild.id, - roles: updatedRoles, - }); - }); - } - return this.client.api.guilds[this.guild.id].roles[this.id] - .patch({ - data: { - name: data.name ?? this.name, - color: data.color !== null ? Util.resolveColor(data.color ?? this.color) : null, - hoist: data.hoist ?? this.hoist, - permissions: typeof data.permissions !== 'undefined' ? new Permissions(data.permissions) : this.permissions, - mentionable: data.mentionable ?? this.mentionable, - }, - reason, - }) - .then(role => { - const clone = this._clone(); - clone._patch(role); - return clone; - }); + edit(data, reason) { + return this.guild.roles.edit(this, data, reason); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index d744849af..50cacee83 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2633,6 +2633,7 @@ declare module 'discord.js' { public fetch(id: Snowflake, options?: BaseFetchOptions): Promise; public fetch(id?: Snowflake, options?: BaseFetchOptions): Promise>; public create(options?: CreateRoleOptions): Promise; + public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise; } export class StageInstanceManager extends BaseManager { @@ -4212,7 +4213,7 @@ declare module 'discord.js' { position: number; } - type RoleResolvable = Role | string; + type RoleResolvable = Role | Snowflake; interface RoleTagData { botID?: Snowflake;