harmony/src/gateway/handlers/inviteCreate.ts

44 lines
1.6 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'
2020-12-02 12:29:52 +00:00
import { ChannelPayload } from '../../types/channel.ts'
import { GuildPayload } from '../../types/guild.ts'
import { InvitePayload } from '../../types/invite.ts'
2020-12-02 10:26:20 +00:00
import { Invite } from '../../structures/invite.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-02 07:20:45 +00:00
channel: (cachedChannel as unknown) as ChannelPayload,
2020-12-01 14:15:43 +00:00
inviter: d.inviter,
target_user: d.target_user,
2020-12-02 12:29:52 +00:00
target_user_type: d.target_user_type
2020-12-01 14:15:43 +00:00
}
2020-12-01 16:38:15 +00:00
2020-12-01 14:15:43 +00:00
await guild.invites.set(d.code, dataConverted)
const invite = await guild.invites.get(d.code)
2020-12-02 10:26:20 +00:00
gateway.client.emit('inviteCreate', (invite as unknown) as Invite)
2020-12-01 14:15:43 +00:00
}