mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
6f280e7781
* refactor(types): move errors, file_content module to types/discordeno * refactor(types): move error & file_content module to discordeno/ * Update and fix import statements
52 lines
1.6 KiB
TypeScript
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]),
|
|
);
|
|
}
|