diff --git a/src/managers/GuildMemberManager.js b/src/managers/GuildMemberManager.js index 612c3dcaafce..7e4104524f24 100644 --- a/src/managers/GuildMemberManager.js +++ b/src/managers/GuildMemberManager.js @@ -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>} + */ + 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. * It's recommended to set options.count to `false` for large guilds. diff --git a/typings/index.d.ts b/typings/index.d.ts index 9a089a2dded1..0b24d56ebd77 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1984,6 +1984,7 @@ declare module 'discord.js' { public fetch(options?: FetchMembersOptions): Promise>; public prune(options: GuildPruneMembersOptions & { dry?: false; count: false }): Promise; public prune(options?: GuildPruneMembersOptions): Promise; + public search(options: GuildSearchMembersOptions): Promise>; public unban(user: UserResolvable, reason?: string): Promise; } @@ -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;