Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add modify function to the GuildMemberRoleManager for adding/removing roles at once. #10355

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
57 changes: 41 additions & 16 deletions packages/discord.js/src/managers/GuildMemberRoleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,7 @@ class GuildMemberRoleManager extends DataManager {
*/
async add(roleOrRoles, reason) {
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
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);

const newRoles = [...new Set(resolvedRoles.concat(...this.cache.keys()))];
return this.set(newRoles, reason);
Expand Down Expand Up @@ -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);

const newRoles = this.cache.filter(role => !resolvedRoles.includes(role.id));
return this.set(newRoles, reason);
Expand All @@ -174,6 +160,45 @@ class GuildMemberRoleManager extends DataManager {
}
}

/**
* Modifies the roles of the member.
* @param {RoleResolvable|RoleResolvable[]|Collection<Snowflake, Role>} rolesToAdd The roles to add
* @param {RoleResolvable|RoleResolvable[]|Collection<Snowflake, Role>} rolesToRemove The roles to remove
* @param {string} [reason] Reason for modifying the roles
* @returns {Promise<GuildMember>}
*/
async modify(rolesToAdd, rolesToRemove, reason) {
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
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);
}
for (const role of resolvedRolesToRemove) {
currentRoles.delete(role.id);
}

return this.member.roles.set([...currentRoles], reason);
}

/**
* Resolves roles from the input.
* @param {RoleResolvable[] | Collection<Snowflake, Role>} rolesToResolve The roles to resolve
* @returns {Array} The resolved roles
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
*/
resolveRoles(rolesToResolve) {
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
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);
}
return resolvedRoles;
}

/**
* Sets the roles applied to the member.
* @param {Collection<Snowflake, Role>|RoleResolvable[]} roles The roles or role ids to apply
Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4408,6 +4408,11 @@ export class GuildMemberRoleManager extends DataManager<Snowflake, Role, RoleRes
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
reason?: string,
): Promise<GuildMember>;
public modify(
rolesToAdd: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
rolesToRemove: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
reason?: any
scottbucher marked this conversation as resolved.
Show resolved Hide resolved
): Promise<GuildMember>;
public set(
roles: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>,
reason?: string,
Expand Down
Loading