feat: full invite support
This commit is contained in:
		
							parent
							
								
									417854b1bb
								
							
						
					
					
						commit
						7a2b71b648
					
				
					 2 changed files with 75 additions and 3 deletions
				
			
		|  | @ -1,10 +1,24 @@ | ||||||
|  | import { GuildTextChannel, User } from '../../mod.ts' | ||||||
| import { Client } from '../models/client.ts' | import { Client } from '../models/client.ts' | ||||||
| import { Guild } from '../structures/guild.ts' | import { Guild } from '../structures/guild.ts' | ||||||
| import { Invite } from '../structures/invite.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 { InvitePayload } from '../types/invite.ts' | ||||||
| import { BaseManager } from './base.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<InvitePayload, Invite> { | export class InviteManager extends BaseManager<InvitePayload, Invite> { | ||||||
|   guild: Guild |   guild: Guild | ||||||
| 
 | 
 | ||||||
|  | @ -20,10 +34,10 @@ export class InviteManager extends BaseManager<InvitePayload, Invite> { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** Fetch an Invite */ |   /** Fetch an Invite */ | ||||||
|   async fetch(id: string): Promise<Invite> { |   async fetch(id: string, withCounts: boolean = true): Promise<Invite> { | ||||||
|     return await new Promise((resolve, reject) => { |     return await new Promise((resolve, reject) => { | ||||||
|       this.client.rest |       this.client.rest | ||||||
|         .get(INVITE(id)) |         .get(`${INVITE(id)}${withCounts ? '?with_counts=true' : ''}`) | ||||||
|         .then(async (data) => { |         .then(async (data) => { | ||||||
|           this.set(id, data as InvitePayload) |           this.set(id, data as InvitePayload) | ||||||
|           const newInvite = await this.get(data.code) |           const newInvite = await this.get(data.code) | ||||||
|  | @ -33,6 +47,57 @@ export class InviteManager extends BaseManager<InvitePayload, Invite> { | ||||||
|     }) |     }) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   /** 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, | ||||||
|  |         target_user_type: options?.targetUser | ||||||
|  |       } | ||||||
|  |     )) as unknown) as InvitePayload | ||||||
|  | 
 | ||||||
|  |     return new Invite(this.client, raw) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   async fromPayload(invites: InvitePayload[]): Promise<boolean> { |   async fromPayload(invites: InvitePayload[]): Promise<boolean> { | ||||||
|     for (const invite of invites) { |     for (const invite of invites) { | ||||||
|       await this.set(invite.code, invite) |       await this.set(invite.code, invite) | ||||||
|  |  | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
|  | import { CreateInviteOptions } from '../managers/invites.ts' | ||||||
| import { MessagesManager } from '../managers/messages.ts' | import { MessagesManager } from '../managers/messages.ts' | ||||||
| import { Client } from '../models/client.ts' | import { Client } from '../models/client.ts' | ||||||
| import { | import { | ||||||
|  | @ -22,6 +23,7 @@ import { Channel } from './channel.ts' | ||||||
| import { Embed } from './embed.ts' | import { Embed } from './embed.ts' | ||||||
| import { Emoji } from './emoji.ts' | import { Emoji } from './emoji.ts' | ||||||
| import { Guild } from './guild.ts' | import { Guild } from './guild.ts' | ||||||
|  | import { Invite } from './invite.ts' | ||||||
| import { Member } from './member.ts' | import { Member } from './member.ts' | ||||||
| import { Message } from './message.ts' | import { Message } from './message.ts' | ||||||
| import { User } from './user.ts' | import { User } from './user.ts' | ||||||
|  | @ -319,4 +321,9 @@ export class GuildTextChannel extends TextChannel { | ||||||
| 
 | 
 | ||||||
|     return this |     return this | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   /** Create an Invite for this Channel */ | ||||||
|  |   async createInvite(options?: CreateInviteOptions): Promise<Invite> { | ||||||
|  |     return this.guild.invites.create(this.id, options) | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue