harmony/src/gateway/handlers/guildMemberUpdate.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
2020-11-25 11:53:40 +00:00
import { GuildMemberUpdatePayload } from '../../types/gateway.ts'
import { MemberPayload } from '../../types/guild.ts'
2020-11-30 11:11:49 +00:00
import { Member } from '../../structures/member.ts'
export const guildMemberUpdate: GatewayEventHandler = async (
gateway: Gateway,
2020-11-25 11:53:40 +00:00
d: GuildMemberUpdatePayload
) => {
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
// Weird case, shouldn't happen
if (guild === undefined) return
2020-11-25 11:53:40 +00:00
const member = await guild.members.get(d.user.id)
const newMemberPayload: MemberPayload = {
user: d.user,
roles: d.roles,
joined_at: d.joined_at,
nick: d.nick,
premium_since: d.premium_since,
deaf: member?.deaf ?? false,
mute: member?.mute ?? false,
2020-11-25 11:53:40 +00:00
}
await guild.members.set(d.user.id, newMemberPayload)
const newMember = await guild.members.get(d.user.id)
2020-11-25 11:53:40 +00:00
if (member !== undefined)
2020-11-30 11:11:49 +00:00
gateway.client.emit('guildMemberUpdate', member, newMember as Member)
else {
2020-11-25 11:53:40 +00:00
gateway.client.emit('guildMemberUpdateUncached', newMember)
}
2020-11-25 11:53:40 +00:00
}