diff --git a/src/managers/invites.ts b/src/managers/invites.ts index e5baf27..44ea8f9 100644 --- a/src/managers/invites.ts +++ b/src/managers/invites.ts @@ -1,10 +1,24 @@ +import { GuildTextChannel, User } from '../../mod.ts' import { Client } from '../models/client.ts' import { Guild } from '../structures/guild.ts' import { Invite } from '../structures/invite.ts' -import { INVITE } from '../types/endpoint.ts' +import { CHANNEL_INVITES, GUILD_INVITES, INVITE } from '../types/endpoint.ts' import { InvitePayload } from '../types/invite.ts' import { BaseManager } from './base.ts' +export enum InviteTargetUserType { + STREAM = 1 +} + +export interface CreateInviteOptions { + maxAge?: number + maxUses?: number + temporary?: boolean + unique?: boolean + targetUser?: string | User + targetUserType?: InviteTargetUserType +} + export class InviteManager extends BaseManager { guild: Guild @@ -20,10 +34,10 @@ export class InviteManager extends BaseManager { } /** Fetch an Invite */ - async fetch(id: string): Promise { + async fetch(id: string, withCounts: boolean = true): Promise { return await new Promise((resolve, reject) => { this.client.rest - .get(INVITE(id)) + .get(`${INVITE(id)}${withCounts ? '?with_counts=true' : ''}`) .then(async (data) => { this.set(id, data as InvitePayload) const newInvite = await this.get(data.code) @@ -33,6 +47,57 @@ export class InviteManager extends BaseManager { }) } + /** Fetch all Invites of a Guild or a specific Channel */ + async fetchAll(channel?: string | GuildTextChannel): Promise { + 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 { + 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 { + 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, + target_user_type: options?.targetUser + } + )) as unknown) as InvitePayload + + return new Invite(this.client, raw) + } + async fromPayload(invites: InvitePayload[]): Promise { for (const invite of invites) { await this.set(invite.code, invite) diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 398f2af..0546dcd 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -1,3 +1,4 @@ +import { CreateInviteOptions } from '../managers/invites.ts' import { MessagesManager } from '../managers/messages.ts' import { Client } from '../models/client.ts' import { @@ -22,6 +23,7 @@ import { Channel } from './channel.ts' import { Embed } from './embed.ts' import { Emoji } from './emoji.ts' import { Guild } from './guild.ts' +import { Invite } from './invite.ts' import { Member } from './member.ts' import { Message } from './message.ts' import { User } from './user.ts' @@ -319,4 +321,9 @@ export class GuildTextChannel extends TextChannel { return this } + + /** Create an Invite for this Channel */ + async createInvite(options?: CreateInviteOptions): Promise { + return this.guild.invites.create(this.id, options) + } }