Skip to content

Commit

Permalink
feat(GuildMemberManager): add 'search' method (#4154)
Browse files Browse the repository at this point in the history
Co-authored-by: Sugden <[email protected]>
  • Loading branch information
izexi and NotSugden authored Apr 14, 2021
1 parent eec7cf7 commit 0ba2bcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ class GuildMemberManager extends BaseManager {
return this._fetchMany(options);
}

/**
* Search for members in the guild based on a query.
* @param {Object} options Search options
* @property {string} options.query Filter members whose username or nickname start with this query
* @property {number} [options.limit=1] Maximum number of members to search
* @property {boolean} [options.cache=true] Whether or not to cache the fetched member(s)
* @returns {Promise<Collection<Snowflake, GuildMember>>}
*/
async search({ query, limit = 1, cache = true } = {}) {
const data = await this.client.api.guilds(this.guild.id).members.search.get({ query: { query, limit } });
return data.reduce((col, member) => col.set(member.user.id, this.add(member, cache)), new Collection());
}

/**
* Prunes members from the guild based on how long they have been inactive.
* <info>It's recommended to set options.count to `false` for large guilds.</info>
Expand Down
7 changes: 7 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,7 @@ declare module 'discord.js' {
public fetch(options?: FetchMembersOptions): Promise<Collection<Snowflake, GuildMember>>;
public prune(options: GuildPruneMembersOptions & { dry?: false; count: false }): Promise<null>;
public prune(options?: GuildPruneMembersOptions): Promise<number>;
public search(options: GuildSearchMembersOptions): Promise<Collection<Snowflake, GuildMember>>;
public unban(user: UserResolvable, reason?: string): Promise<User>;
}

Expand Down Expand Up @@ -2860,6 +2861,12 @@ declare module 'discord.js' {
channel: GuildChannelResolvable | null;
}

interface GuildSearchMembersOptions {
query: string;
limit?: number;
cache?: boolean;
}

interface HTTPOptions {
api?: string;
version?: number;
Expand Down

0 comments on commit 0ba2bcb

Please sign in to comment.