clean up positions (#1919)

* clean up positions

* perf i guess
This commit is contained in:
Gus Caplan
2017-09-09 15:40:32 -05:00
committed by Crawl
parent 98582cd1b7
commit 2ffe3048ba
6 changed files with 78 additions and 131 deletions

View File

@@ -47,10 +47,10 @@ class Role extends Base {
this.hoist = data.hoist;
/**
* The position of the role from the API
* The raw position of the role from the API
* @type {number}
*/
this.position = data.position;
this.rawPosition = data.position;
/**
* The permissions of the role
@@ -126,8 +126,8 @@ class Role extends Base {
* @type {number}
* @readonly
*/
get calculatedPosition() {
const sorted = this.guild._sortedRoles;
get position() {
const sorted = this.guild._sortedRoles();
return sorted.array().indexOf(sorted.get(this.id));
}
@@ -264,21 +264,6 @@ class Role extends Base {
return this.edit({ hoist }, reason);
}
/**
* Set the position of the role.
* @param {number} position The position of the role
* @param {boolean} [relative=false] Move the position relative to its current value
* @returns {Promise<Role>}
* @example
* // Set the position of the role
* role.setPosition(1)
* .then(r => console.log(`Role position: ${r.position}`))
* .catch(console.error);
*/
setPosition(position, relative) {
return this.guild.setRolePosition(this, position, relative).then(() => this);
}
/**
* Set the permissions of the role.
* @param {PermissionResolvable[]} permissions The permissions of the role
@@ -309,6 +294,31 @@ class Role extends Base {
return this.edit({ mentionable }, reason);
}
/**
* Set the position of the role.
* @param {number} position The position of the role
* @param {Object} [options] Options for setting position
* @param {boolean} [options.relative=false] Change the position relative to its current value
* @param {boolean} [options.reason] Reasion for changing the position
* @returns {Promise<Role>}
* @example
* // Set the position of the role
* role.setPosition(1)
* .then(r => console.log(`Role position: ${r.position}`))
* .catch(console.error);
*/
setPosition(position, { relative, reason } = {}) {
return Util.setPosition(this, position, relative,
this.guild._sortedRoles(), this.client.api.guilds(this.guild.id).roles, reason)
.then(updatedRoles => {
this.client.actions.GuildRolesPositionUpdate.handle({
guild_id: this.id,
channels: updatedRoles,
});
return this;
});
}
/**
* Deletes the role.
* @param {string} [reason] Reason for deleting this role