Added the ability to fetch role member counts or ids for single roles. (RoleManager.fetchMemberCounts, RoleManager.fetchMemberIds, Role.fetchMemberIds)

This commit is contained in:
Lixqa
2025-05-12 21:05:55 +02:00
parent 3d90afd3e5
commit e1fad0c6d3
3 changed files with 34 additions and 0 deletions

View File

@@ -73,6 +73,28 @@ class RoleManager extends CachedManager {
return id ? roles.get(id) ?? null : roles; return id ? roles.get(id) ?? null : roles;
} }
/**
* Fetches the member counts for each role in the guild.
* @returns {Promise<Record<Snowflake, number>>}
*/
async fetchMemberCounts() {
const data = await this.client.api.guilds(this.guild.id).roles('member-counts').get();
return data;
}
/**
* Fetches the member ids for a role in the guild.
* <info>This only returns 100 member ids</info>
* @param {RoleResolvable} id The role to fetch member ids for
* @returns {Promise<Snowflake[]>}
*/
async fetchMemberIds(id) {
const data = await this.client.api.guilds(this.guild.id).roles(id, 'member-ids').get();
return data;
}
/** /**
* Data that can be resolved to a Role object. This can be: * Data that can be resolved to a Role object. This can be:
* * A Role * * A Role

View File

@@ -449,6 +449,15 @@ class Role extends Base {
return this; return this;
} }
/**
* Fetches the member ids for this role in the guild.
* <info>This only returns 100 member ids</info>
* @returns {Promise<Snowflake[]>}
*/
fetchMemberIds() {
return this.guild.roles.fetchMemberIds(this.id);
}
/** /**
* A link to the role's icon * A link to the role's icon
* @param {StaticImageURLOptions} [options={}] Options for the image URL * @param {StaticImageURLOptions} [options={}] Options for the image URL

3
typings/index.d.ts vendored
View File

@@ -2912,6 +2912,7 @@ export class Role extends Base {
public setIcon(icon: BufferResolvable | Base64Resolvable | EmojiResolvable | null, reason?: string): Promise<Role>; public setIcon(icon: BufferResolvable | Base64Resolvable | EmojiResolvable | null, reason?: string): Promise<Role>;
public setPosition(position: number, options?: SetRolePositionOptions): Promise<Role>; public setPosition(position: number, options?: SetRolePositionOptions): Promise<Role>;
public setUnicodeEmoji(unicodeEmoji: string | null, reason?: string): Promise<Role>; public setUnicodeEmoji(unicodeEmoji: string | null, reason?: string): Promise<Role>;
public fetchMemberIds(): Promise<Snowflake[]>;
public toJSON(): unknown; public toJSON(): unknown;
public toString(): RoleMention; public toString(): RoleMention;
@@ -4709,6 +4710,8 @@ export class RoleManager extends CachedManager<Snowflake, Role, RoleResolvable>
public setPosition(role: RoleResolvable, position: number, options?: SetRolePositionOptions): Promise<Role>; public setPosition(role: RoleResolvable, position: number, options?: SetRolePositionOptions): Promise<Role>;
public setPositions(rolePositions: readonly RolePosition[]): Promise<Guild>; public setPositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
public comparePositions(role1: RoleResolvable, role2: RoleResolvable): number; public comparePositions(role1: RoleResolvable, role2: RoleResolvable): number;
public fetchMemberCounts(): Promise<Record<Snowflake, number>>;
public fetchMemberIds(role: RoleResolvable): Promise<Snowflake[]>;
} }
export class StageInstanceManager extends CachedManager<Snowflake, StageInstance, StageInstanceResolvable> { export class StageInstanceManager extends CachedManager<Snowflake, StageInstance, StageInstanceResolvable> {