mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
Add role position comparison methods
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -135,9 +135,7 @@ class GuildMember {
|
||||
* @readonly
|
||||
*/
|
||||
get highestRole() {
|
||||
return this.roles.reduce((prev, role) =>
|
||||
!prev || role.position > prev.position || (role.position === prev.position && role.id < prev.id) ? role : prev
|
||||
);
|
||||
return this.roles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,10 +202,7 @@ class GuildMember {
|
||||
if (this.user.id === this.client.user.id) return false;
|
||||
const clientMember = this.guild.member(this.client.user);
|
||||
if (!clientMember.hasPermission(Constants.PermissionFlags.KICK_MEMBERS)) return false;
|
||||
const clientRole = clientMember.highestRole;
|
||||
const thisRole = this.highestRole;
|
||||
return clientRole.position > thisRole.position ||
|
||||
(clientRole.position === thisRole.position && clientRole.id < thisRole.id);
|
||||
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -220,10 +215,7 @@ class GuildMember {
|
||||
if (this.user.id === this.client.user.id) return false;
|
||||
const clientMember = this.guild.member(this.client.user);
|
||||
if (!clientMember.hasPermission(Constants.PermissionFlags.BAN_MEMBERS)) return false;
|
||||
const clientRole = clientMember.highestRole;
|
||||
const thisRole = this.highestRole;
|
||||
return clientRole.position > thisRole.position ||
|
||||
(clientRole.position === thisRole.position && clientRole.id < thisRole.id);
|
||||
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -153,6 +153,16 @@ class Role {
|
||||
return permissions.map(p => this.hasPermission(p, explicit)).every(v => v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this role's position to another role's.
|
||||
* @param {Role} role Role to compare to this one
|
||||
* @returns {number} Negative number if the this role's position is lower (other role's is higher),
|
||||
* positive number if the this one is higher (other's is lower), 0 if equal
|
||||
*/
|
||||
comparePositionTo(role) {
|
||||
return this.constructor.comparePositions(this, role);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the role
|
||||
* @param {RoleData} data The new data for the role
|
||||
@@ -289,6 +299,18 @@ class Role {
|
||||
toString() {
|
||||
return `<@&${this.id}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the positions of two roles.
|
||||
* @param {Role} role1 First 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),
|
||||
* positive number if the first's is higher (second's is lower), 0 if equal
|
||||
*/
|
||||
static comparePositions(role1, role2) {
|
||||
if (role1.position === role2.position) return role1.id - role2.id;
|
||||
return role1.position - role2.position;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Role;
|
||||
|
||||
Reference in New Issue
Block a user