From 41a40a2b2516194f5f4a71e527cc5c4a6cb84a87 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 19:11:56 -0400 Subject: [PATCH 01/12] initial work for adding the modify roles function --- .../src/managers/GuildMemberRoleManager.js | 63 ++++++++++++++----- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index e5302683d0f9..b9128818e652 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -107,14 +107,7 @@ class GuildMemberRoleManager extends DataManager { */ async add(roleOrRoles, reason) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { - const resolvedRoles = []; - for (const role of roleOrRoles.values()) { - const resolvedRole = this.guild.roles.resolveId(role); - if (!resolvedRole) { - throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role); - } - resolvedRoles.push(resolvedRole); - } + const resolvedRoles = this.resolveRoles(roleOrRoles, this.guild); const newRoles = [...new Set(resolvedRoles.concat(...this.cache.keys()))]; return this.set(newRoles, reason); @@ -144,14 +137,7 @@ class GuildMemberRoleManager extends DataManager { */ async remove(roleOrRoles, reason) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { - const resolvedRoles = []; - for (const role of roleOrRoles.values()) { - const resolvedRole = this.guild.roles.resolveId(role); - if (!resolvedRole) { - throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role); - } - resolvedRoles.push(resolvedRole); - } + const resolvedRoles = this.resolveRoles(roleOrRoles, this.guild); const newRoles = this.cache.filter(role => !resolvedRoles.includes(role.id)); return this.set(newRoles, reason); @@ -173,6 +159,51 @@ class GuildMemberRoleManager extends DataManager { return clone; } } + + /** + * Modifies the roles of the member. + * @param {RoleResolvable[]} roleIdsToAdd The role ids to add + * @param {RoleResolvable[]} roleIdsToRemove The role ids to remove + * @param {string} [reason] Reason for modifying the roles + * @returns {Promise} + */ + public static async modify( + roleIdsToAdd: RoleResolvable[], + roleIdsToRemove: RoleResolvable[], + member: GuildMember, + reason?: any +): Promise { + const resolvedRolesToAdd = this.resolveRoles(roleIdsToAdd, member.guild) + const resolvedRolesToRemove = this.resolveRoles(roleIdsToRemove, member.guild); + + const currentRoles = new Set(member.roles.cache.keys()); + for (const role of resolvedRolesToAdd) { + currentRoles.add(role.id); + } + for (const role of resolvedRolesToRemove) { + currentRoles.delete(role.id); + } + + return await member.roles.set([...currentRoles], reason); +} + +/** + * Resolves roles from the input. + * @param {RoleResolvable[]} rolesToResolve The roles to resolve + * @param {Guild} guild The guild to resolve the roles in + * @returns {Array} The resolved roles + */ +private static resolveRoles(rolesToResolve: RoleResolvable[], guild: Guild): Role[] { + const resolvedRoles = []; + for (const role of rolesToResolve) { + const resolvedRole = guild.roles.resolve(role); + if (!resolvedRole) { + throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role); + } + resolvedRoles.push(resolvedRole); + } + return resolvedRoles; +} /** * Sets the roles applied to the member. From d2bebf418cc40c00c69acfce5ee538cb292b1e2b Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 19:34:07 -0400 Subject: [PATCH 02/12] add types and fixes --- .../src/managers/GuildMemberRoleManager.js | 28 ++++++++----------- packages/discord.js/typings/index.d.ts | 6 ++++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index b9128818e652..63ae8c779dfb 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -107,7 +107,7 @@ class GuildMemberRoleManager extends DataManager { */ async add(roleOrRoles, reason) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { - const resolvedRoles = this.resolveRoles(roleOrRoles, this.guild); + const resolvedRoles = this.resolveRoles(roleOrRoles); const newRoles = [...new Set(resolvedRoles.concat(...this.cache.keys()))]; return this.set(newRoles, reason); @@ -137,7 +137,7 @@ class GuildMemberRoleManager extends DataManager { */ async remove(roleOrRoles, reason) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { - const resolvedRoles = this.resolveRoles(roleOrRoles, this.guild); + const resolvedRoles = this.resolveRoles(roleOrRoles); const newRoles = this.cache.filter(role => !resolvedRoles.includes(role.id)); return this.set(newRoles, reason); @@ -162,19 +162,14 @@ class GuildMemberRoleManager extends DataManager { /** * Modifies the roles of the member. - * @param {RoleResolvable[]} roleIdsToAdd The role ids to add - * @param {RoleResolvable[]} roleIdsToRemove The role ids to remove + * @param {RoleResolvable|RoleResolvable[]|Collection} roleIdsToAdd The roles to add + * @param {RoleResolvable|RoleResolvable[]|Collection} roleIdsToRemove The roles to remove * @param {string} [reason] Reason for modifying the roles * @returns {Promise} */ - public static async modify( - roleIdsToAdd: RoleResolvable[], - roleIdsToRemove: RoleResolvable[], - member: GuildMember, - reason?: any -): Promise { - const resolvedRolesToAdd = this.resolveRoles(roleIdsToAdd, member.guild) - const resolvedRolesToRemove = this.resolveRoles(roleIdsToRemove, member.guild); + public static async modify(roleIdsToAdd, roleIdsToRemove, member, reason) { + const resolvedRolesToAdd = this.resolveRoles(roleIdsToAdd) + const resolvedRolesToRemove = this.resolveRoles(roleIdsToRemove); const currentRoles = new Set(member.roles.cache.keys()); for (const role of resolvedRolesToAdd) { @@ -189,14 +184,13 @@ class GuildMemberRoleManager extends DataManager { /** * Resolves roles from the input. - * @param {RoleResolvable[]} rolesToResolve The roles to resolve - * @param {Guild} guild The guild to resolve the roles in + * @param {RoleResolvable[] | Collection} rolesToResolve The roles to resolve * @returns {Array} The resolved roles */ -private static resolveRoles(rolesToResolve: RoleResolvable[], guild: Guild): Role[] { +private static resolveRoles(rolesToResolve, guild) { const resolvedRoles = []; - for (const role of rolesToResolve) { - const resolvedRole = guild.roles.resolve(role); + for (const role of rolesToResolve.values()) { + const resolvedRole = this.guild.roles.resolveId(role); if (!resolvedRole) { throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role); } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index ae236f758fb6..b96b2f4f6c6e 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4408,6 +4408,12 @@ export class GuildMemberRoleManager extends DataManager, reason?: string, ): Promise; + public remove( + roleIdsToAdd: readonly RoleResolvable[] | ReadonlyCollection, + roleIdsToRemove: readonly RoleResolvable[] | ReadonlyCollection, + member: GuildMember, + reason?: any + ): Promise; public set( roles: readonly RoleResolvable[] | ReadonlyCollection, reason?: string, From 0d4eb68ee60d49fb7d0e17ed99b098dc1631ebf3 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 19:34:54 -0400 Subject: [PATCH 03/12] use correct function name --- packages/discord.js/typings/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index b96b2f4f6c6e..5afb70fe2820 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4408,7 +4408,7 @@ export class GuildMemberRoleManager extends DataManager, reason?: string, ): Promise; - public remove( + public modify( roleIdsToAdd: readonly RoleResolvable[] | ReadonlyCollection, roleIdsToRemove: readonly RoleResolvable[] | ReadonlyCollection, member: GuildMember, From 32ee89d1f148c1b354db3a55ea9acace158ec297 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 19:35:46 -0400 Subject: [PATCH 04/12] we don't need to pass in member since this is the GuildMemberRoleManager --- packages/discord.js/src/managers/GuildMemberRoleManager.js | 6 +++--- packages/discord.js/typings/index.d.ts | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index 63ae8c779dfb..c1f49b9b3c2d 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -167,11 +167,11 @@ class GuildMemberRoleManager extends DataManager { * @param {string} [reason] Reason for modifying the roles * @returns {Promise} */ - public static async modify(roleIdsToAdd, roleIdsToRemove, member, reason) { + public static async modify(roleIdsToAdd, roleIdsToRemove, reason) { const resolvedRolesToAdd = this.resolveRoles(roleIdsToAdd) const resolvedRolesToRemove = this.resolveRoles(roleIdsToRemove); - const currentRoles = new Set(member.roles.cache.keys()); + const currentRoles = new Set(this.member.roles.cache.keys()); for (const role of resolvedRolesToAdd) { currentRoles.add(role.id); } @@ -179,7 +179,7 @@ class GuildMemberRoleManager extends DataManager { currentRoles.delete(role.id); } - return await member.roles.set([...currentRoles], reason); + return await this.member.roles.set([...currentRoles], reason); } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 5afb70fe2820..09988e6641ab 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4411,7 +4411,6 @@ export class GuildMemberRoleManager extends DataManager, roleIdsToRemove: readonly RoleResolvable[] | ReadonlyCollection, - member: GuildMember, reason?: any ): Promise; public set( From 28230fd2e4bb77adf0b2622fdfdb71a549baf13b Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 19:36:59 -0400 Subject: [PATCH 05/12] name shouldn't include id --- .../discord.js/src/managers/GuildMemberRoleManager.js | 10 +++++----- packages/discord.js/typings/index.d.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index c1f49b9b3c2d..82512859536c 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -162,14 +162,14 @@ class GuildMemberRoleManager extends DataManager { /** * Modifies the roles of the member. - * @param {RoleResolvable|RoleResolvable[]|Collection} roleIdsToAdd The roles to add - * @param {RoleResolvable|RoleResolvable[]|Collection} roleIdsToRemove The roles to remove + * @param {RoleResolvable|RoleResolvable[]|Collection} rolesToAdd The roles to add + * @param {RoleResolvable|RoleResolvable[]|Collection} rolesToRemove The roles to remove * @param {string} [reason] Reason for modifying the roles * @returns {Promise} */ - public static async modify(roleIdsToAdd, roleIdsToRemove, reason) { - const resolvedRolesToAdd = this.resolveRoles(roleIdsToAdd) - const resolvedRolesToRemove = this.resolveRoles(roleIdsToRemove); + public static async modify(rolesToAdd, rolesToRemove, reason) { + const resolvedRolesToAdd = this.resolveRoles(rolesToAdd) + const resolvedRolesToRemove = this.resolveRoles(rolesToRemove); const currentRoles = new Set(this.member.roles.cache.keys()); for (const role of resolvedRolesToAdd) { diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 09988e6641ab..636a547681f5 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4409,8 +4409,8 @@ export class GuildMemberRoleManager extends DataManager; public modify( - roleIdsToAdd: readonly RoleResolvable[] | ReadonlyCollection, - roleIdsToRemove: readonly RoleResolvable[] | ReadonlyCollection, + rolesToAdd: readonly RoleResolvable[] | ReadonlyCollection, + rolesToRemove: readonly RoleResolvable[] | ReadonlyCollection, reason?: any ): Promise; public set( From 1a63405127db7aa55f216e4468c766665d08552a Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 19:53:27 -0400 Subject: [PATCH 06/12] adjust typings --- .../src/managers/GuildMemberRoleManager.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index 82512859536c..a9f688de6989 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -167,7 +167,7 @@ class GuildMemberRoleManager extends DataManager { * @param {string} [reason] Reason for modifying the roles * @returns {Promise} */ - public static async modify(rolesToAdd, rolesToRemove, reason) { + async modify(rolesToAdd, rolesToRemove, reason) { const resolvedRolesToAdd = this.resolveRoles(rolesToAdd) const resolvedRolesToRemove = this.resolveRoles(rolesToRemove); @@ -182,12 +182,12 @@ class GuildMemberRoleManager extends DataManager { return await this.member.roles.set([...currentRoles], reason); } -/** - * Resolves roles from the input. - * @param {RoleResolvable[] | Collection} rolesToResolve The roles to resolve - * @returns {Array} The resolved roles - */ -private static resolveRoles(rolesToResolve, guild) { + /** + * Resolves roles from the input. + * @param {RoleResolvable[] | Collection} rolesToResolve The roles to resolve + * @returns {Array} The resolved roles + */ + resolveRoles(rolesToResolve, guild) { const resolvedRoles = []; for (const role of rolesToResolve.values()) { const resolvedRole = this.guild.roles.resolveId(role); From 3ef7bde37292a9381798af82bf20e625a9db4d39 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Tue, 18 Jun 2024 21:13:53 -0400 Subject: [PATCH 07/12] style: fix formatting --- .../src/managers/GuildMemberRoleManager.js | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index a9f688de6989..24b7e924be87 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -159,7 +159,7 @@ class GuildMemberRoleManager extends DataManager { return clone; } } - + /** * Modifies the roles of the member. * @param {RoleResolvable|RoleResolvable[]|Collection} rolesToAdd The roles to add @@ -168,36 +168,36 @@ class GuildMemberRoleManager extends DataManager { * @returns {Promise} */ async modify(rolesToAdd, rolesToRemove, reason) { - const resolvedRolesToAdd = this.resolveRoles(rolesToAdd) + const resolvedRolesToAdd = this.resolveRoles(rolesToAdd); const resolvedRolesToRemove = this.resolveRoles(rolesToRemove); const currentRoles = new Set(this.member.roles.cache.keys()); for (const role of resolvedRolesToAdd) { - currentRoles.add(role.id); + currentRoles.add(role.id); } for (const role of resolvedRolesToRemove) { - currentRoles.delete(role.id); + currentRoles.delete(role.id); } - return await this.member.roles.set([...currentRoles], reason); -} + return this.member.roles.set([...currentRoles], reason); + } /** * Resolves roles from the input. * @param {RoleResolvable[] | Collection} rolesToResolve The roles to resolve * @returns {Array} The resolved roles */ - resolveRoles(rolesToResolve, guild) { + resolveRoles(rolesToResolve) { const resolvedRoles = []; for (const role of rolesToResolve.values()) { - const resolvedRole = this.guild.roles.resolveId(role); - if (!resolvedRole) { - throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role); - } - resolvedRoles.push(resolvedRole); + const resolvedRole = this.guild.roles.resolveId(role); + if (!resolvedRole) { + throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array or Collection', 'roles', role); + } + resolvedRoles.push(resolvedRole); } return resolvedRoles; -} + } /** * Sets the roles applied to the member. From 344550f5553ff4aa2e19a2b3c1aeb57397a468d6 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Wed, 19 Jun 2024 18:22:02 -0400 Subject: [PATCH 08/12] fix(): address some of the comments --- packages/discord.js/src/managers/GuildMemberRoleManager.js | 5 +++-- packages/discord.js/typings/index.d.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index 24b7e924be87..ba3355a8e0c4 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -162,8 +162,8 @@ class GuildMemberRoleManager extends DataManager { /** * Modifies the roles of the member. - * @param {RoleResolvable|RoleResolvable[]|Collection} rolesToAdd The roles to add - * @param {RoleResolvable|RoleResolvable[]|Collection} rolesToRemove The roles to remove + * @param {RoleResolvable[]|Collection} rolesToAdd The roles to add + * @param {RoleResolvable[]|Collection} rolesToRemove The roles to remove * @param {string} [reason] Reason for modifying the roles * @returns {Promise} */ @@ -186,6 +186,7 @@ class GuildMemberRoleManager extends DataManager { * Resolves roles from the input. * @param {RoleResolvable[] | Collection} rolesToResolve The roles to resolve * @returns {Array} The resolved roles + * @private */ resolveRoles(rolesToResolve) { const resolvedRoles = []; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 636a547681f5..22aefa0c5102 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4411,7 +4411,7 @@ export class GuildMemberRoleManager extends DataManager, rolesToRemove: readonly RoleResolvable[] | ReadonlyCollection, - reason?: any + reason?: any, ): Promise; public set( roles: readonly RoleResolvable[] | ReadonlyCollection, From cda0e26771e4e3063edc430f57f3ba7d363af450 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Wed, 19 Jun 2024 18:33:14 -0400 Subject: [PATCH 09/12] refactor(): add options object for modify function --- .../src/managers/GuildMemberRoleManager.js | 12 +++++------- packages/discord.js/typings/index.d.ts | 12 +++++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index ba3355a8e0c4..7673cbd92bbd 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -162,14 +162,12 @@ class GuildMemberRoleManager extends DataManager { /** * Modifies the roles of the member. - * @param {RoleResolvable[]|Collection} rolesToAdd The roles to add - * @param {RoleResolvable[]|Collection} rolesToRemove The roles to remove - * @param {string} [reason] Reason for modifying the roles + * @param {ModifyGuildMemberRolesOptions} [options] Options for modifying the roles * @returns {Promise} */ - async modify(rolesToAdd, rolesToRemove, reason) { - const resolvedRolesToAdd = this.resolveRoles(rolesToAdd); - const resolvedRolesToRemove = this.resolveRoles(rolesToRemove); + async modify(options) { + const resolvedRolesToAdd = this.resolveRoles(options.rolesToAdd); + const resolvedRolesToRemove = this.resolveRoles(options.rolesToRemove); const currentRoles = new Set(this.member.roles.cache.keys()); for (const role of resolvedRolesToAdd) { @@ -179,7 +177,7 @@ class GuildMemberRoleManager extends DataManager { currentRoles.delete(role.id); } - return this.member.roles.set([...currentRoles], reason); + return this.member.roles.set([...currentRoles], options.reason); } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 22aefa0c5102..5abddafc48a0 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4393,6 +4393,12 @@ export class GuildStickerManager extends CachedManager; } +export interface ModifyGuildMemberRolesOptions { + rolesToAdd?: readonly RoleResolvable[] | ReadonlyCollection; + rolesToRemove?: readonly RoleResolvable[] | ReadonlyCollection; + reason?: string; +} + export class GuildMemberRoleManager extends DataManager { private constructor(member: GuildMember); public get hoist(): Role | null; @@ -4408,11 +4414,7 @@ export class GuildMemberRoleManager extends DataManager, reason?: string, ): Promise; - public modify( - rolesToAdd: readonly RoleResolvable[] | ReadonlyCollection, - rolesToRemove: readonly RoleResolvable[] | ReadonlyCollection, - reason?: any, - ): Promise; + public modify(options: ModifyGuildMemberRolesOptions): Promise; public set( roles: readonly RoleResolvable[] | ReadonlyCollection, reason?: string, From a80d113928c656036c5fac4a9eeee3aeba8bf5b7 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Wed, 19 Jun 2024 18:38:51 -0400 Subject: [PATCH 10/12] docs(): added docs for the options object --- packages/discord.js/src/managers/GuildMemberRoleManager.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index 7673cbd92bbd..4646013ec105 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -160,6 +160,13 @@ class GuildMemberRoleManager extends DataManager { } } + /** + * @typedef {Object} ModifyGuildMemberRolesOptions + * @property {readonly RoleResolvable[] | ReadonlyCollection} [rolesToAdd] The roles to add + * @property {readonly RoleResolvable[] | ReadonlyCollection} [rolesToRemove] The roles to remove + * @property {readonly RoleResolvable[] | ReadonlyCollection} [reason] Reason for modifying the roles + */ + /** * Modifies the roles of the member. * @param {ModifyGuildMemberRolesOptions} [options] Options for modifying the roles From c5d66a1c5222960da64164ebff6e45fcb0d38d36 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Wed, 19 Jun 2024 18:45:43 -0400 Subject: [PATCH 11/12] docs(): use correct readonly in docs --- .../discord.js/src/managers/GuildMemberRoleManager.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index 4646013ec105..f5f99be45b76 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -162,17 +162,18 @@ class GuildMemberRoleManager extends DataManager { /** * @typedef {Object} ModifyGuildMemberRolesOptions - * @property {readonly RoleResolvable[] | ReadonlyCollection} [rolesToAdd] The roles to add - * @property {readonly RoleResolvable[] | ReadonlyCollection} [rolesToRemove] The roles to remove - * @property {readonly RoleResolvable[] | ReadonlyCollection} [reason] Reason for modifying the roles + * @property {Readonly | ReadonlyCollection} [rolesToAdd] The roles to add + * @property {Readonly | ReadonlyCollection} [rolesToRemove] The roles to remove + * @property {Readonly | ReadonlyCollection} [reason] Reason for modifying + * the roles */ /** * Modifies the roles of the member. * @param {ModifyGuildMemberRolesOptions} [options] Options for modifying the roles - * @returns {Promise} + * @returns {GuildMember} */ - async modify(options) { + modify(options) { const resolvedRolesToAdd = this.resolveRoles(options.rolesToAdd); const resolvedRolesToRemove = this.resolveRoles(options.rolesToRemove); From 24d3acfd13a93d25e93d7bac4ccdbfdc8c0dc148 Mon Sep 17 00:00:00 2001 From: Scott Bucher Date: Fri, 21 Jun 2024 19:52:33 -0400 Subject: [PATCH 12/12] fix(): address comments around typigns and cleaning up existing code --- .../src/managers/GuildMemberRoleManager.js | 15 +++++---------- packages/discord.js/typings/index.d.ts | 3 ++- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/packages/discord.js/src/managers/GuildMemberRoleManager.js b/packages/discord.js/src/managers/GuildMemberRoleManager.js index f5f99be45b76..1c86d464d74a 100644 --- a/packages/discord.js/src/managers/GuildMemberRoleManager.js +++ b/packages/discord.js/src/managers/GuildMemberRoleManager.js @@ -107,10 +107,7 @@ class GuildMemberRoleManager extends DataManager { */ async add(roleOrRoles, reason) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { - const resolvedRoles = this.resolveRoles(roleOrRoles); - - const newRoles = [...new Set(resolvedRoles.concat(...this.cache.keys()))]; - return this.set(newRoles, reason); + return this.modify({ rolesToAdd: roleOrRoles, reason }); } else { roleOrRoles = this.guild.roles.resolveId(roleOrRoles); if (roleOrRoles === null) { @@ -137,10 +134,7 @@ class GuildMemberRoleManager extends DataManager { */ async remove(roleOrRoles, reason) { if (roleOrRoles instanceof Collection || Array.isArray(roleOrRoles)) { - const resolvedRoles = this.resolveRoles(roleOrRoles); - - const newRoles = this.cache.filter(role => !resolvedRoles.includes(role.id)); - return this.set(newRoles, reason); + return this.modify({ rolesToRemove: roleOrRoles, reason }); } else { roleOrRoles = this.guild.roles.resolveId(roleOrRoles); if (roleOrRoles === null) { @@ -190,11 +184,12 @@ class GuildMemberRoleManager extends DataManager { /** * Resolves roles from the input. - * @param {RoleResolvable[] | Collection} rolesToResolve The roles to resolve - * @returns {Array} The resolved roles + * @param {Readonly | ReadonlyCollection} rolesToResolve The roles to resolve + * @returns {Role[]} The resolved roles * @private */ resolveRoles(rolesToResolve) { + if (!rolesToResolve) return []; const resolvedRoles = []; for (const role of rolesToResolve.values()) { const resolvedRole = this.guild.roles.resolveId(role); diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 5abddafc48a0..e231a7a00fc1 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -4414,7 +4414,7 @@ export class GuildMemberRoleManager extends DataManager, reason?: string, ): Promise; - public modify(options: ModifyGuildMemberRolesOptions): Promise; + public modify(options: ModifyGuildMemberRolesOptions): GuildMember; public set( roles: readonly RoleResolvable[] | ReadonlyCollection, reason?: string, @@ -4423,6 +4423,7 @@ export class GuildMemberRoleManager extends DataManager, reason?: string, ): Promise; + private resolveRoles(rolesToResolve: readonly RoleResolvable[] | ReadonlyCollection): Role[]; } export interface FetchPollAnswerVotersOptions extends BaseFetchPollAnswerVotersOptions {