2020-11-15 09:48:18 +00:00
|
|
|
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'
|
2020-11-15 09:48:18 +00:00
|
|
|
|
|
|
|
export const guildMemberUpdate: GatewayEventHandler = async (
|
|
|
|
gateway: Gateway,
|
2020-11-25 11:53:40 +00:00
|
|
|
d: GuildMemberUpdatePayload
|
2020-11-15 09:48:18 +00:00
|
|
|
) => {
|
|
|
|
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,
|
2020-12-02 08:02:00 +00:00
|
|
|
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-15 09:48:18 +00:00
|
|
|
|
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)
|
2020-11-15 09:48:18 +00:00
|
|
|
else {
|
2020-11-25 11:53:40 +00:00
|
|
|
gateway.client.emit('guildMemberUpdateUncached', newMember)
|
2020-11-15 09:48:18 +00:00
|
|
|
}
|
2020-11-25 11:53:40 +00:00
|
|
|
}
|