Skip to content

Commit

Permalink
feat(RoleManager): added edit method, alias Role#edit (#5983)
Browse files Browse the repository at this point in the history
Co-authored-by: monbrey <rsm999@uowmail.edu.au>
  • Loading branch information
kyranet and monbrey committed Jul 1, 2021
1 parent d742814 commit 1e73c25
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
50 changes: 49 additions & 1 deletion 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.
Expand Down Expand Up @@ -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<Role>}
* @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
* <info>Only ever available for bots</info>
Expand Down
34 changes: 2 additions & 32 deletions src/structures/Role.js
Expand Up @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Expand Up @@ -2633,6 +2633,7 @@ declare module 'discord.js' {
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<Role | null>;
public fetch(id?: Snowflake, options?: BaseFetchOptions): Promise<Collection<Snowflake, Role>>;
public create(options?: CreateRoleOptions): Promise<Role>;
public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise<Role>;
}

export class StageInstanceManager extends BaseManager<Snowflake, StageInstance, StageInstanceResolvable> {
Expand Down Expand Up @@ -4212,7 +4213,7 @@ declare module 'discord.js' {
position: number;
}

type RoleResolvable = Role | string;
type RoleResolvable = Role | Snowflake;

interface RoleTagData {
botID?: Snowflake;
Expand Down

0 comments on commit 1e73c25

Please sign in to comment.