refactor(RoleManager): Move some methods over from Role (#7096)

This commit is contained in:
Jiralite
2021-12-14 18:04:07 +00:00
committed by GitHub
parent 49f9a18020
commit 1e00fc2001
3 changed files with 55 additions and 8 deletions

View File

@@ -218,6 +218,23 @@ class RoleManager extends CachedManager {
return clone; return clone;
} }
/**
* Deletes a role.
* @param {RoleResolvable} role The role to delete
* @param {string} [reason] Reason for deleting the role
* @returns {Promise<void>}
* @example
* // Delete a role
* guild.roles.delete('222079219327434752', 'The role needed to go')
* .then(deleted => console.log(`Deleted role ${deleted.name}`))
* .catch(console.error);
*/
async delete(role, reason) {
const id = this.resolveId(role);
await this.client.api.guilds[this.guild.id].roles[id].delete({ reason });
this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: id });
}
/** /**
* Batch-updates the guild's role positions * Batch-updates the guild's role positions
* @param {GuildRolePosition[]} rolePositions Role positions to update * @param {GuildRolePosition[]} rolePositions Role positions to update
@@ -244,6 +261,25 @@ class RoleManager extends CachedManager {
}).guild; }).guild;
} }
/**
* Compares the positions of two roles.
* @param {RoleResolvable} role1 First role to compare
* @param {RoleResolvable} role2 Second role to compare
* @returns {number} Negative number if the first role's position is lower (second role's is higher),
* positive number if the first's is higher (second's is lower), 0 if equal
*/
comparePositions(role1, role2) {
const resolvedRole1 = this.resolve(role1);
const resolvedRole2 = this.resolve(role2);
if (!resolvedRole1 || !resolvedRole2) throw new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake');
if (resolvedRole1.position === resolvedRole2.position) {
return Number(BigInt(resolvedRole2.id) - BigInt(resolvedRole1.id));
}
return resolvedRole1.position - resolvedRole2.position;
}
/** /**
* Gets the managed role a user created when joining the guild, if any * Gets the managed role a user created when joining the guild, if any
* <info>Only ever available for bots</info> * <info>Only ever available for bots</info>

View File

@@ -1,11 +1,13 @@
'use strict'; 'use strict';
const Base = require('./Base'); const Base = require('./Base');
const { Error, TypeError } = require('../errors'); const { Error } = require('../errors');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const SnowflakeUtil = require('../util/SnowflakeUtil'); const SnowflakeUtil = require('../util/SnowflakeUtil');
const Util = require('../util/Util'); const Util = require('../util/Util');
let deprecationEmittedForComparePositions = false;
/** /**
* @type {WeakSet<Role>} * @type {WeakSet<Role>}
* @private * @private
@@ -207,9 +209,7 @@ class Role extends Base {
* positive number if this one is higher (other's is lower), 0 if equal * positive number if this one is higher (other's is lower), 0 if equal
*/ */
comparePositionTo(role) { comparePositionTo(role) {
role = this.guild.roles.resolve(role); return this.guild.roles.comparePositions(this, role);
if (!role) throw new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake');
return this.constructor.comparePositions(this, role);
} }
/** /**
@@ -407,8 +407,7 @@ class Role extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
async delete(reason) { async delete(reason) {
await this.client.api.guilds[this.guild.id].roles[this.id].delete({ reason }); await this.guild.roles.delete(this.id, reason);
this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id });
return this; return this;
} }
@@ -469,10 +468,19 @@ class Role extends Base {
* @param {Role} role2 Second role to compare * @param {Role} role2 Second role to compare
* @returns {number} Negative number if the first role's position is lower (second role's is higher), * @returns {number} Negative number if the first role's position is lower (second role's is higher),
* positive number if the first's is higher (second's is lower), 0 if equal * positive number if the first's is higher (second's is lower), 0 if equal
* @deprecated Use {@link RoleManager#comparePositions} instead.
*/ */
static comparePositions(role1, role2) { static comparePositions(role1, role2) {
if (role1.position === role2.position) return role2.id - role1.id; if (!deprecationEmittedForComparePositions) {
return role1.position - role2.position; process.emitWarning(
'The Role.comparePositions method is deprecated. Use RoleManager#comparePositions instead.',
'DeprecationWarning',
);
deprecationEmittedForComparePositions = true;
}
return role1.guild.roles.comparePositions(role1, role2);
} }
} }

3
typings/index.d.ts vendored
View File

@@ -1895,6 +1895,7 @@ export class Role extends Base {
public toJSON(): unknown; public toJSON(): unknown;
public toString(): RoleMention; public toString(): RoleMention;
/** @deprecated Use {@link RoleManager.comparePositions} instead. */
public static comparePositions(role1: Role, role2: Role): number; public static comparePositions(role1: Role, role2: Role): number;
} }
@@ -3074,7 +3075,9 @@ export class RoleManager extends CachedManager<Snowflake, Role, RoleResolvable>
public fetch(id?: undefined, options?: BaseFetchOptions): Promise<Collection<Snowflake, Role>>; public fetch(id?: undefined, options?: BaseFetchOptions): Promise<Collection<Snowflake, Role>>;
public create(options?: CreateRoleOptions): Promise<Role>; public create(options?: CreateRoleOptions): Promise<Role>;
public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise<Role>; public edit(role: RoleResolvable, options: RoleData, reason?: string): Promise<Role>;
public delete(role: RoleResolvable, reason?: string): Promise<void>;
public setPositions(rolePositions: readonly RolePosition[]): Promise<Guild>; public setPositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
public comparePositions(role1: RoleResolvable, role2: RoleResolvable): number;
} }
export class StageInstanceManager extends CachedManager<Snowflake, StageInstance, StageInstanceResolvable> { export class StageInstanceManager extends CachedManager<Snowflake, StageInstance, StageInstanceResolvable> {