mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
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:
150
src/stores/GuildMemberRoleStore.js
Normal file
150
src/stores/GuildMemberRoleStore.js
Normal file
@@ -0,0 +1,150 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const Role = require('../structures/Role');
|
||||
const Collection = require('../util/Collection');
|
||||
const { TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Stores member roles
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class GuildMemberRoleStore extends DataStore {
|
||||
constructor(member) {
|
||||
super(member.client, null, Role);
|
||||
this.member = member;
|
||||
this.guild = member.guild;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a role (or multiple roles) to the member.
|
||||
* @param {RoleResolvable|RoleResolvable[]|Collection<Snowflake, Role>} roleOrRoles The role or roles to add
|
||||
* @param {string} [reason] Reason for adding the role(s)
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
add(roleOrRoles, reason) {
|
||||
if (roleOrRoles instanceof Collection) return this.add(roleOrRoles.keyArray(), reason);
|
||||
if (!(roleOrRoles instanceof Array)) return this.add([roleOrRoles], reason);
|
||||
|
||||
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolve(r));
|
||||
|
||||
if (roleOrRoles.includes(null)) {
|
||||
return Promise.reject(new TypeError('INVALID_TYPE', 'roles',
|
||||
'Array or Collection of Roles or Snowflakes', true));
|
||||
} else {
|
||||
for (const role of roleOrRoles) super.set(role.id, role);
|
||||
}
|
||||
|
||||
return this.set(this, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.roles.set(['391156570408615936'])
|
||||
* .then(console.log)
|
||||
* .catch(console.error);
|
||||
* @example
|
||||
* // Remove all the roles from a member
|
||||
* guildMember.roles.set([])
|
||||
* .then(member => console.log(`Member roles is now of ${member.roles.size} size`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
set(roles, reason) {
|
||||
return this.member.edit({ roles }, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a role (or multiple roles) from the member.
|
||||
* @param {RoleResolvable|RoleResolvable[]|Collection<Snowflake, Role>} roleOrRoles The role or roles to remove
|
||||
* @param {string} [reason] Reason for removing the role(s)
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
remove(roleOrRoles, reason) {
|
||||
if (roleOrRoles instanceof Collection) return this.remove(roleOrRoles.keyArray(), reason);
|
||||
if (!(roleOrRoles instanceof Array)) return this.remove([roleOrRoles], reason);
|
||||
|
||||
roleOrRoles = roleOrRoles.map(r => this.guild.roles.resolveID(r));
|
||||
|
||||
if (roleOrRoles.includes(null)) {
|
||||
return Promise.reject(new TypeError('INVALID_TYPE', 'roles',
|
||||
'Array or Collection of Roles or Snowflakes', true));
|
||||
} else {
|
||||
for (const role of roleOrRoles) super.remove(role);
|
||||
}
|
||||
|
||||
return this.set(this, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* The role of the member used to hoist them in a separate category in the users list
|
||||
* @type {?Role}
|
||||
* @readonly
|
||||
*/
|
||||
get hoist() {
|
||||
const hoistedRoles = this.filter(role => role.hoist);
|
||||
if (!hoistedRoles.size) return null;
|
||||
return hoistedRoles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
|
||||
}
|
||||
|
||||
/**
|
||||
* The role of the member used to set their color
|
||||
* @type {?Role}
|
||||
* @readonly
|
||||
*/
|
||||
get color() {
|
||||
const coloredRoles = this.filter(role => role.color);
|
||||
if (!coloredRoles.size) return null;
|
||||
return coloredRoles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
|
||||
}
|
||||
|
||||
/**
|
||||
* The role of the member with the highest position
|
||||
* @type {Role}
|
||||
* @readonly
|
||||
*/
|
||||
get highest() {
|
||||
return this.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches the roles for this store
|
||||
* @param {Snowflake[]} roles The new roles
|
||||
* @private
|
||||
*/
|
||||
_patch(roles) {
|
||||
this.clear();
|
||||
|
||||
const everyoneRole = this.guild.roles.get(this.guild.id);
|
||||
if (everyoneRole) super.set(everyoneRole.id, everyoneRole);
|
||||
|
||||
if (roles) {
|
||||
for (const roleID of roles) {
|
||||
const role = this.guild.roles.resolve(roleID);
|
||||
if (role) super.set(role.id, role);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a RoleResolvable to a Role object.
|
||||
* @method resolve
|
||||
* @memberof GuildMemberRoleStore
|
||||
* @instance
|
||||
* @param {RoleResolvable} role The role resolvable to resolve
|
||||
* @returns {?Role}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resolves a RoleResolvable to a role ID string.
|
||||
* @method resolveID
|
||||
* @memberof GuildMemberRoleStore
|
||||
* @instance
|
||||
* @param {RoleResolvable} role The role resolvable to resolve
|
||||
* @returns {?Snowflake}
|
||||
*/
|
||||
}
|
||||
|
||||
module.exports = GuildMemberRoleStore;
|
||||
Reference in New Issue
Block a user