harmony/src/gateway/handlers/inviteCreate.ts

43 lines
1.5 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 { InviteCreatePayload } from '../../types/gateway.ts'
import { ChannelPayload, GuildPayload, InvitePayload } from '../../../mod.ts'
2020-12-01 16:38:15 +00:00
import getChannelByType from '../../utils/getChannelByType.ts'
2020-12-01 14:15:43 +00:00
export const inviteCreate: GatewayEventHandler = async (
gateway: Gateway,
d: InviteCreatePayload
) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id!)
// Weird case, shouldn't happen
if (guild === undefined) return
2020-12-01 16:38:15 +00:00
/**
* TODO(DjDeveloperr): Add _get method in BaseChildManager
*/
const cachedChannel = await gateway.client.channels._get(d.channel_id)
2020-12-01 14:15:43 +00:00
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const cachedGuild: GuildPayload | undefined =
d.guild_id === undefined
? undefined
: await guild.client.guilds._get(d.guild_id)
2020-12-01 16:38:15 +00:00
2020-12-01 14:15:43 +00:00
const dataConverted: InvitePayload = {
code: d.code,
guild: cachedGuild,
2020-12-01 16:38:15 +00:00
// had to use `as ChannelPayload` because the _get method returned `ChannelPayload | undefined` which errored
2020-12-01 14:15:43 +00:00
channel: cachedChannel as ChannelPayload,
inviter: d.inviter,
target_user: d.target_user,
target_user_type: d.target_user_type,
}
2020-12-01 16:38:15 +00:00
2020-12-01 14:15:43 +00:00
// 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)
}