|
| 1 | +import type { GuildMemberPayload } from "../../../types/mod.ts"; |
| 2 | +import { Collection } from "../cache/mod.ts"; |
| 3 | +import type { Client } from "../client/mod.ts"; |
| 4 | +import { BaseManager } from "./base.ts"; |
| 5 | +import { GuildMembersManager } from "./guildMembers.ts"; |
| 6 | + |
| 7 | +export class MembersManager extends BaseManager< |
| 8 | + Collection<string, GuildMemberPayload>, |
| 9 | + GuildMembersManager |
| 10 | +> { |
| 11 | + constructor(client: Client) { |
| 12 | + super(client); |
| 13 | + } |
| 14 | + |
| 15 | + _fill(guildID: string, payloads: GuildMemberPayload[]) { |
| 16 | + if (!this.has(guildID)) { |
| 17 | + this.set(guildID, new Collection()); |
| 18 | + } |
| 19 | + for (const payload of payloads) { |
| 20 | + if (payload.user === undefined) continue; |
| 21 | + this.get(guildID)!.set(payload.user.id, payload); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + async _fetch( |
| 26 | + guildID: string, |
| 27 | + options?: { |
| 28 | + limit?: number; |
| 29 | + after?: string; |
| 30 | + }, |
| 31 | + ): Promise<Collection<string, GuildMemberPayload> | undefined> { |
| 32 | + try { |
| 33 | + const resp: GuildMemberPayload[] | undefined = await this.client.rest.get( |
| 34 | + `/guilds/${guildID}/members`, |
| 35 | + { |
| 36 | + query: options |
| 37 | + ? { limit: options.limit?.toString(), after: options.after } |
| 38 | + : {}, |
| 39 | + }, |
| 40 | + ); |
| 41 | + if (!resp) return; |
| 42 | + this._fill(guildID, resp); |
| 43 | + return this._get(guildID)!; |
| 44 | + } catch (_err) { |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + get( |
| 50 | + id: string, |
| 51 | + ) { |
| 52 | + if (!this.has(id)) { |
| 53 | + this.set(id, new Collection()); |
| 54 | + } |
| 55 | + const cached = this._get(id)!; |
| 56 | + return new GuildMembersManager(this.client, id, cached); |
| 57 | + } |
| 58 | + |
| 59 | + async fetch( |
| 60 | + guildID: string, |
| 61 | + options?: { |
| 62 | + limit?: number; |
| 63 | + after?: string; |
| 64 | + }, |
| 65 | + ) { |
| 66 | + try { |
| 67 | + const collection = await this._fetch(guildID, options); |
| 68 | + if (!collection) return; |
| 69 | + return new GuildMembersManager(this.client, guildID, collection); |
| 70 | + } catch (_err) { |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments