harmony/src/managers/invites.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-12-01 14:15:43 +00:00
import { Client } from '../models/client.ts'
import { Guild } from '../structures/guild.ts'
import { Invite } from '../structures/invite.ts'
import { GUILD_INVITES } from '../types/endpoint.ts'
import { InvitePayload } from '../types/invite.ts'
import { BaseManager } from './base.ts'
export class InviteManager extends BaseManager<InvitePayload, Invite> {
guild: Guild
constructor(client: Client, guild: Guild) {
super(client, `invites:${guild.id}`, Invite)
this.guild = guild
}
2020-12-01 16:38:15 +00:00
async get(key: string): Promise<Invite | undefined> {
const raw = await this._get(key)
if (raw === undefined) return
return new Invite(this.client, raw)
}
2020-12-02 07:29:13 +00:00
async fetch(id: string): Promise<Invite | undefined> {
2020-12-01 14:15:43 +00:00
return await new Promise((resolve, reject) => {
this.client.rest
.get(GUILD_INVITES(this.guild.id))
2020-12-02 07:20:45 +00:00
.then(async (data) => {
2020-12-01 14:15:43 +00:00
this.set(id, data as InvitePayload)
2020-12-02 07:20:45 +00:00
const newInvite = await this.get(data.code)
resolve(newInvite)
2020-12-01 14:15:43 +00:00
})
.catch((e) => reject(e))
})
}
async fromPayload(invites: InvitePayload[]): Promise<boolean> {
for (const invite of invites) {
await this.set(invite.code, invite)
}
return true
}
2020-12-01 16:38:15 +00:00
}