-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: Add modify function to the GuildMemberRoleManager for adding/removing roles at once. #10355
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
Changes from 11 commits
41a40a2
d2bebf4
0d4eb68
32ee89d
28230fd
1a63405
3ef7bde
344550f
cda0e26
a80d113
c5d66a1
24d3acf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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); | ||||||
|
||||||
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); | ||||||
|
||||||
const newRoles = this.cache.filter(role => !resolvedRoles.includes(role.id)); | ||||||
return this.set(newRoles, reason); | ||||||
|
@@ -174,6 +160,52 @@ class GuildMemberRoleManager extends DataManager { | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* @typedef {Object} ModifyGuildMemberRolesOptions | ||||||
* @property {Readonly<RoleResolvable[]> | ReadonlyCollection<Snowflake, Role>} [rolesToAdd] The roles to add | ||||||
* @property {Readonly<RoleResolvable[]> | ReadonlyCollection<Snowflake, Role>} [rolesToRemove] The roles to remove | ||||||
* @property {Readonly<RoleResolvable[]> | ReadonlyCollection<Snowflake, Role>} [reason] Reason for modifying | ||||||
* the roles | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have a typedef for ReadonlyCollection and Readonly |
||||||
*/ | ||||||
|
||||||
/** | ||||||
* Modifies the roles of the member. | ||||||
* @param {ModifyGuildMemberRolesOptions} [options] Options for modifying the roles | ||||||
* @returns {GuildMember} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
modify(options) { | ||||||
const resolvedRolesToAdd = this.resolveRoles(options.rolesToAdd); | ||||||
const resolvedRolesToRemove = this.resolveRoles(options.rolesToRemove); | ||||||
scottbucher marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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], options.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
|
||||||
* @private | ||||||
*/ | ||||||
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 | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4393,6 +4393,12 @@ export class GuildStickerManager extends CachedManager<Snowflake, Sticker, Stick | |
public fetchUser(sticker: StickerResolvable): Promise<User | null>; | ||
} | ||
|
||
export interface ModifyGuildMemberRolesOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the right place to put this? What is the preference for ordering in the this typings file? |
||
rolesToAdd?: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>; | ||
rolesToRemove?: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>; | ||
reason?: string; | ||
} | ||
|
||
export class GuildMemberRoleManager extends DataManager<Snowflake, Role, RoleResolvable> { | ||
private constructor(member: GuildMember); | ||
public get hoist(): Role | null; | ||
|
@@ -4408,6 +4414,7 @@ export class GuildMemberRoleManager extends DataManager<Snowflake, Role, RoleRes | |
roleOrRoles: RoleResolvable | readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>, | ||
reason?: string, | ||
): Promise<GuildMember>; | ||
public modify(options: ModifyGuildMemberRolesOptions): Promise<GuildMember>; | ||
scottbucher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public set( | ||
roles: readonly RoleResolvable[] | ReadonlyCollection<Snowflake, Role>, | ||
reason?: string, | ||
|
Uh oh!
There was an error while loading. Please reload this page.