harmony/src/gateway/handlers/inviteDelete.ts

34 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-12-01 14:15:43 +00:00
import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { InviteDeletePayload } from '../../types/gateway.ts'
2020-12-02 07:20:45 +00:00
import { PartialInvitePayload } from '../../types/invite.ts'
2020-12-02 12:29:52 +00:00
import { Channel } from '../../structures/channel.ts'
2020-12-01 14:15:43 +00:00
export const inviteDelete: GatewayEventHandler = async (
gateway: Gateway,
d: InviteDeletePayload
) => {
2020-12-02 07:49:58 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2020-12-01 14:15:43 +00:00
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id!)
// Weird case, shouldn't happen
if (guild === undefined) return
const cachedInvite = await guild.invites.get(d.code)
2020-12-02 07:20:45 +00:00
const cachedChannel = await gateway.client.channels.get(d.channel_id)
2020-12-02 07:49:58 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2020-12-02 07:20:45 +00:00
const cachedGuild = await gateway.client.guilds.get(d.guild_id!)
2020-12-01 14:15:43 +00:00
2020-12-01 16:38:15 +00:00
if (cachedInvite === undefined) {
2020-12-02 07:20:45 +00:00
const uncachedInvite: PartialInvitePayload = {
guild: (cachedGuild as unknown) as Guild,
channel: (cachedChannel as unknown) as Channel,
2020-12-02 12:29:52 +00:00
code: d.code
2020-12-02 07:20:45 +00:00
}
return gateway.client.emit('inviteDeleteUncached', uncachedInvite)
2020-12-01 16:38:15 +00:00
} else {
await guild.invites._delete(d.code)
2020-12-01 16:38:15 +00:00
gateway.client.emit('inviteDelete', cachedInvite)
}
2020-12-01 14:15:43 +00:00
}