Files
discordeno/src/helpers/members/search_members.ts
T
rigormorrtiss 6f280e7781 change(types): move error & file_content to discordeno/ (#903)
* refactor(types): move errors, file_content module to types/discordeno

* refactor(types): move error & file_content module to discordeno/

* Update and fix import statements
2021-05-05 17:19:28 +01:00

52 lines
1.6 KiB
TypeScript

import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { DiscordenoMember } from "../../structures/member.ts";
import { structures } from "../../structures/mod.ts";
import type { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import type { SearchGuildMembers } from "../../types/members/search_guild_members.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/**
* ⚠️ BEGINNER DEVS!! YOU SHOULD ALMOST NEVER NEED THIS AND YOU CAN GET FROM cache.members.filter()
* @param query Query string to match username(s) and nickname(s) against
*/
export async function searchMembers(
guildId: bigint,
query: string,
options?: Omit<SearchGuildMembers, "query"> & { cache?: boolean },
) {
if (options?.limit) {
if (options.limit < 1) throw new Error(Errors.MEMBER_SEARCH_LIMIT_TOO_LOW);
if (options.limit > 1000) {
throw new Error(Errors.MEMBER_SEARCH_LIMIT_TOO_HIGH);
}
}
const result = await rest.runMethod<GuildMemberWithUser[]>(
"get",
endpoints.GUILD_MEMBERS_SEARCH(guildId),
{
...options,
query,
},
);
const members = await Promise.all(result.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
member,
guildId,
);
if (options?.cache) {
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
}
return discordenoMember;
}));
return new Collection<bigint, DiscordenoMember>(
members.map((member) => [member.id, member]),
);
}