mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13: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
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get highestRole() {
|
get highestRole() {
|
||||||
return this.roles.reduce((prev, role) =>
|
return this.roles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
|
||||||
!prev || role.position > prev.position || (role.position === prev.position && role.id < prev.id) ? role : prev
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,10 +202,7 @@ class GuildMember {
|
|||||||
if (this.user.id === this.client.user.id) return false;
|
if (this.user.id === this.client.user.id) return false;
|
||||||
const clientMember = this.guild.member(this.client.user);
|
const clientMember = this.guild.member(this.client.user);
|
||||||
if (!clientMember.hasPermission(Constants.PermissionFlags.KICK_MEMBERS)) return false;
|
if (!clientMember.hasPermission(Constants.PermissionFlags.KICK_MEMBERS)) return false;
|
||||||
const clientRole = clientMember.highestRole;
|
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
|
||||||
const thisRole = this.highestRole;
|
|
||||||
return clientRole.position > thisRole.position ||
|
|
||||||
(clientRole.position === thisRole.position && clientRole.id < thisRole.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -220,10 +215,7 @@ class GuildMember {
|
|||||||
if (this.user.id === this.client.user.id) return false;
|
if (this.user.id === this.client.user.id) return false;
|
||||||
const clientMember = this.guild.member(this.client.user);
|
const clientMember = this.guild.member(this.client.user);
|
||||||
if (!clientMember.hasPermission(Constants.PermissionFlags.BAN_MEMBERS)) return false;
|
if (!clientMember.hasPermission(Constants.PermissionFlags.BAN_MEMBERS)) return false;
|
||||||
const clientRole = clientMember.highestRole;
|
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
|
||||||
const thisRole = this.highestRole;
|
|
||||||
return clientRole.position > thisRole.position ||
|
|
||||||
(clientRole.position === thisRole.position && clientRole.id < thisRole.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -153,6 +153,16 @@ class Role {
|
|||||||
return permissions.map(p => this.hasPermission(p, explicit)).every(v => v);
|
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
|
* Edits the role
|
||||||
* @param {RoleData} data The new data for the role
|
* @param {RoleData} data The new data for the role
|
||||||
@@ -289,6 +299,18 @@ class Role {
|
|||||||
toString() {
|
toString() {
|
||||||
return `<@&${this.id}>`;
|
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;
|
module.exports = Role;
|
||||||
|
|||||||
Reference in New Issue
Block a user