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

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