harmony/src/managers/invites.ts

108 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2021-04-04 09:29:56 +00:00
import type { GuildTextChannel, User } from '../../mod.ts'
import type { Client } from '../client/mod.ts'
import type { Guild } from '../structures/guild.ts'
2020-12-01 14:15:43 +00:00
import { Invite } from '../structures/invite.ts'
2020-12-31 05:07:40 +00:00
import { CHANNEL_INVITES, GUILD_INVITES, INVITE } from '../types/endpoint.ts'
2021-04-04 09:29:56 +00:00
import type { InvitePayload } from '../types/invite.ts'
2020-12-01 14:15:43 +00:00
import { BaseManager } from './base.ts'
2020-12-31 05:07:40 +00:00
export enum InviteTargetUserType {
STREAM = 1
}
export interface CreateInviteOptions {
maxAge?: number
maxUses?: number
temporary?: boolean
unique?: boolean
targetUser?: string | User
targetUserType?: InviteTargetUserType
}
2020-12-01 14:15:43 +00:00
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-28 12:35:08 +00:00
/** Fetch an Invite */
2020-12-31 05:07:40 +00:00
async fetch(id: string, withCounts: boolean = true): Promise<Invite> {
2020-12-01 14:15:43 +00:00
return await new Promise((resolve, reject) => {
this.client.rest
2020-12-31 05:07:40 +00:00
.get(`${INVITE(id)}${withCounts ? '?with_counts=true' : ''}`)
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)
2020-12-28 12:35:08 +00:00
resolve(newInvite as Invite)
2020-12-01 14:15:43 +00:00
})
.catch((e) => reject(e))
})
}
2020-12-31 05:07:40 +00:00
/** Fetch all Invites of a Guild or a specific Channel */
async fetchAll(channel?: string | GuildTextChannel): Promise<Invite[]> {
const rawInvites = (await this.client.rest.get(
channel === undefined
? GUILD_INVITES(this.guild.id)
: CHANNEL_INVITES(typeof channel === 'string' ? channel : channel.id)
)) as InvitePayload[]
const res: Invite[] = []
for (const raw of rawInvites) {
await this.set(raw.code, raw)
res.push(new Invite(this.client, raw))
}
return res
}
/** Delete an Invite */
async delete(invite: string | Invite): Promise<boolean> {
await this.client.rest.delete(
INVITE(typeof invite === 'string' ? invite : invite.code)
)
return true
}
/** Create an Invite */
async create(
channel: string | GuildTextChannel,
options?: CreateInviteOptions
): Promise<Invite> {
const raw = ((await this.client.rest.post(
CHANNEL_INVITES(typeof channel === 'string' ? channel : channel.id),
{
max_age: options?.maxAge,
max_uses: options?.maxUses,
temporary: options?.temporary,
unique: options?.unique,
target_user:
options?.targetUser === undefined
? undefined
: typeof options.targetUser === 'string'
? options.targetUser
: options.targetUser.id,
2020-12-31 05:12:13 +00:00
target_user_type: options?.targetUserType
2020-12-31 05:07:40 +00:00
}
)) as unknown) as InvitePayload
return new Invite(this.client, raw)
}
2020-12-01 14:15:43 +00:00
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
}