refactor: Move member role-related functions to a special store (#2242)

* Ignore this I need a patch branch for Git

* Move all member role related stuff to a new DataStore
GuildMemberRoleStore is a new store that holds all member role related stuffs
Because its consistent!

* Minorest doc fix ever
To whoever did this object, they forgot a `{`

* Fix the spacing in the docs

* Resue the stores resolve rather than copy paste
Cause I'm dum and it overwrite resolve to the guild role stores resolve soo

* Fix some requests
- Removed the bs private functions
- Set the roles in the constructor

But, I need feedback. There is no way, that I saw, to make a member have roles whenever GuildMmber#_patch is called,
due to the roles being null on the guild. So the only way might be the for loop and getter.

* Fix an issue that I caused in #add
I was testing some other things, and changed that to test. Forgot to change it back

* Actually make the store generate just once when the member is created by first initializing the roles in the guild
Also replaces GuildMember#_roles with GuildMemberRoleStore#_roles
Tested all functions to make sure the expected result happens.

* I missed this from moving remove from GuildMember to GuildMemberRoleStore

* Fix RoleStore#create docs
For real this time

* Do all the requested changes
- Rename all `somethingRole` to `something` (hoistRole => hoist)
- Refactor add and remove to be cleaner and re-use set

* Fix a bug where the store would loose some roles due to null roles that can throw an error in the for loop after they've been deleted

* Remove the `role.id || role` part of the add and remove functions as Appel suggested

* Replace roles.resolve with roles.resolveID for GuildMemberRoleStore#remove

* Don't use Array.isArray in checks
Use instanceof instead

* Woops, I forgot to change this
Renamed colorRole to color

* The docs have dots, so we place the dots back
This commit is contained in:
Frangu Vlad
2018-01-24 09:12:58 +02:00
committed by SpaceEEC
parent 5352e28700
commit 00172e6c7d
5 changed files with 189 additions and 181 deletions

View File

@@ -1,10 +1,10 @@
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Role = require('./Role');
const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection');
const GuildMemberRoleStore = require('../stores/GuildMemberRoleStore');
const Base = require('./Base');
const { Presence } = require('./Presence');
const { Error, TypeError } = require('../errors');
const { Error } = require('../errors');
/**
* Represents a member of a guild on Discord.
@@ -27,7 +27,12 @@ class GuildMember extends Base {
*/
this.user = {};
this._roles = [];
/**
* A list of roles that are applied to this GuildMember, mapped by the role ID
* @type {GuildMemberRoleStore<Snowflake, Role>}
*/
this.roles = new GuildMemberRoleStore(this);
if (data) this._patch(data);
@@ -67,7 +72,7 @@ class GuildMember extends Base {
if (data.joined_at) this.joinedTimestamp = new Date(data.joined_at).getTime();
this.user = this.guild.client.users.add(data.user);
if (data.roles) this._roles = data.roles;
if (data.roles) this.roles._patch(data.roles);
}
get voiceState() {
@@ -134,52 +139,13 @@ class GuildMember extends Base {
return this.frozenPresence || this.guild.presences.get(this.id) || new Presence(this.client);
}
/**
* A list of roles that are applied to this GuildMember, mapped by the role ID
* @type {Collection<Snowflake, Role>}
* @readonly
*/
get roles() {
const list = new Collection();
const everyoneRole = this.guild.roles.get(this.guild.id);
if (everyoneRole) list.set(everyoneRole.id, everyoneRole);
for (const roleID of this._roles) {
const role = this.guild.roles.get(roleID);
if (role) list.set(role.id, role);
}
return list;
}
/**
* The role of the member with the highest position
* @type {Role}
* @readonly
*/
get highestRole() {
return this.roles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
}
/**
* The role of the member used to set their color
* @type {?Role}
* @readonly
*/
get colorRole() {
const coloredRoles = this.roles.filter(role => role.color);
if (!coloredRoles.size) return null;
return coloredRoles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
}
/**
* The displayed color of the member in base 10
* @type {number}
* @readonly
*/
get displayColor() {
const role = this.colorRole;
const role = this.roles.color;
return (role && role.color) || 0;
}
@@ -189,21 +155,10 @@ class GuildMember extends Base {
* @readonly
*/
get displayHexColor() {
const role = this.colorRole;
const role = this.roles.color;
return (role && role.hexColor) || '#000000';
}
/**
* The role of the member used to hoist them in a separate category in the users list
* @type {?Role}
* @readonly
*/
get hoistRole() {
const hoistedRoles = this.roles.filter(role => role.hoist);
if (!hoistedRoles.size) return null;
return hoistedRoles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
}
/**
* Whether this member is muted in any way
* @type {boolean}
@@ -395,104 +350,6 @@ class GuildMember extends Base {
return this.edit({ channel });
}
/**
* Sets the roles applied to the member.
* @param {Collection<Snowflake, Role>|RoleResolvable[]} roles The roles or role IDs to apply
* @param {string} [reason] Reason for applying the roles
* @returns {Promise<GuildMember>}
* @example
* // Set the member's roles to a single role
* guildMember.setRoles(['391156570408615936'])
* .then(console.log)
* .catch(console.error);
* @example
* // Remove all the roles from a member
* guildMember.setRoles([])
* .then(member => console.log(`Member roles is now of ${member.roles.size} size`))
* .catch(console.error);
*/
setRoles(roles, reason) {
return this.edit({ roles }, reason);
}
/**
* Adds a single role to the member.
* @param {RoleResolvable} role The role or ID of the role to add
* @param {string} [reason] Reason for adding the role
* @returns {Promise<GuildMember>}
*/
addRole(role, reason) {
role = this.guild.roles.resolve(role);
if (!role) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
if (this._roles.includes(role.id)) return Promise.resolve(this);
return this.client.api.guilds(this.guild.id).members(this.user.id).roles(role.id)
.put({ reason })
.then(() => {
const clone = this._clone();
if (!clone._roles.includes(role.id)) clone._roles.push(role.id);
return clone;
});
}
/**
* Adds multiple roles to the member.
* @param {Collection<Snowflake, Role>|RoleResolvable[]} roles The roles or role IDs to add
* @param {string} [reason] Reason for adding the roles
* @returns {Promise<GuildMember>}
*/
addRoles(roles, reason) {
let allRoles = this._roles.slice();
for (let role of roles instanceof Collection ? roles.values() : roles) {
role = this.guild.roles.resolve(role);
if (!role) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles',
'Array or Collection of Roles or Snowflakes', true));
}
allRoles.push(role.id);
}
return this.edit({ roles: allRoles }, reason);
}
/**
* Removes a single role from the member.
* @param {RoleResolvable} role The role or ID of the role to remove
* @param {string} [reason] Reason for removing the role
* @returns {Promise<GuildMember>}
*/
removeRole(role, reason) {
role = this.guild.roles.resolve(role);
if (!role) return Promise.reject(new TypeError('INVALID_TYPE', 'role', 'Role nor a Snowflake'));
if (!this._roles.includes(role.id)) return Promise.resolve(this);
return this.client.api.guilds(this.guild.id).members(this.user.id).roles(role.id)
.delete({ reason })
.then(() => {
const clone = this._clone();
const index = clone._roles.indexOf(role.id);
if (~index) clone._roles.splice(index, 1);
return clone;
});
}
/**
* Removes multiple roles from the member.
* @param {Collection<Snowflake, Role>|RoleResolvable[]} roles The roles or role IDs to remove
* @param {string} [reason] Reason for removing the roles
* @returns {Promise<GuildMember>}
*/
removeRoles(roles, reason) {
const allRoles = this._roles.slice();
for (let role of roles instanceof Collection ? roles.values() : roles) {
role = this.guild.roles.resolve(role);
if (!role) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles',
'Array or Collection of Roles or Snowflakes', true));
}
const index = allRoles.indexOf(role.id);
if (index >= 0) allRoles.splice(index, 1);
}
return this.edit({ roles: allRoles }, reason);
}
/**
* Sets the nickname for the guild member.
* @param {string} nick The nickname for the guild member