feat(RoleManager): added edit method, alias Role#edit (#5983)

Co-authored-by: monbrey <rsm999@uowmail.edu.au>
This commit is contained in:
Antonio Román
2021-07-01 23:32:03 +02:00
committed by GitHub
parent d742814686
commit 1e73c25fbf
3 changed files with 53 additions and 34 deletions

View File

@@ -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<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>