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(GuildMemberManager): add 'search' method #4154

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1984,6 +1984,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 @@ -2832,6 +2833,12 @@ declare module 'discord.js' {
channel: GuildChannelResolvable | null;
}

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

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