made requested changes

This commit is contained in:
CyberKnight007 2020-12-02 12:50:45 +05:30
parent 4fc5734ccc
commit e01d10dd66
5 changed files with 23 additions and 8 deletions

View file

@ -2,7 +2,6 @@ import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { InviteCreatePayload } from '../../types/gateway.ts'
import { ChannelPayload, GuildPayload, InvitePayload } from '../../../mod.ts'
import getChannelByType from '../../utils/getChannelByType.ts'
export const inviteCreate: GatewayEventHandler = async (
gateway: Gateway,
@ -29,13 +28,12 @@ export const inviteCreate: GatewayEventHandler = async (
code: d.code,
guild: cachedGuild,
// had to use `as ChannelPayload` because the _get method returned `ChannelPayload | undefined` which errored
channel: cachedChannel as ChannelPayload,
channel: (cachedChannel as unknown) as ChannelPayload,
inviter: d.inviter,
target_user: d.target_user,
target_user_type: d.target_user_type,
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
await guild.invites.set(d.code, dataConverted)
const invite = await guild.invites.get(d.code)
gateway.client.emit('inviteCreate', invite)

View file

@ -1,6 +1,8 @@
import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { InviteDeletePayload } from '../../types/gateway.ts'
import { PartialInvitePayload } from '../../types/invite.ts'
import { Channel } from '../../../mod.ts'
export const inviteDelete: GatewayEventHandler = async (
gateway: Gateway,
@ -13,10 +15,17 @@ export const inviteDelete: GatewayEventHandler = async (
if (guild === undefined) return
const cachedInvite = await guild.invites.get(d.code)
const cachedChannel = await gateway.client.channels.get(d.channel_id)
const cachedGuild = await gateway.client.guilds.get(d.guild_id!)
// Should not happen but here we go
// TODO(DjDeveloperr): Make it support self-bots and make Guild not always defined
if (cachedInvite === undefined) {
return gateway.client.emit('inviteDeleteUncached', cachedInvite)
const uncachedInvite: PartialInvitePayload = {
guild: (cachedGuild as unknown) as Guild,
channel: (cachedChannel as unknown) as Channel,
code: d.code,
}
return gateway.client.emit('inviteDeleteUncached', uncachedInvite)
} else {
await guild.invites.delete(d.code)
gateway.client.emit('inviteDelete', cachedInvite)

View file

@ -23,9 +23,10 @@ export class InviteManager extends BaseManager<InvitePayload, Invite> {
return await new Promise((resolve, reject) => {
this.client.rest
.get(GUILD_INVITES(this.guild.id))
.then((data) => {
.then(async (data) => {
this.set(id, data as InvitePayload)
resolve(new Invite(this.client, data as InvitePayload))
const newInvite = await this.get(data.code)
resolve(newInvite)
})
.catch((e) => reject(e))
})

View file

@ -72,7 +72,7 @@ client.on('inviteDelete', (invite: Invite) => {
})
client.on('inviteDeleteUncached', (invite: Invite) => {
console.log(`Invite deleted uncached: ${invite}`)
console.log(invite)
})
client.on('commandError', console.error)

View file

@ -1,3 +1,4 @@
import { Channel, Guild } from "../../mod.ts"
import { ChannelPayload } from './channel.ts'
import { GuildPayload } from './guild.ts'
import { UserPayload } from './user.ts'
@ -12,3 +13,9 @@ export interface InvitePayload {
approximate_presence_count?: number
approximate_member_count?: number
}
export interface PartialInvitePayload {
code: string
channel: Channel
guild?: Guild
}