refactor(events:guildEmojiUpdate): split up into add, delete, update events
This commit is contained in:
parent
e72e702764
commit
45673d8197
2 changed files with 44 additions and 5 deletions
|
@ -1,14 +1,53 @@
|
|||
import cache from '../../models/cache.ts'
|
||||
import { Emoji } from "../../structures/emoji.ts"
|
||||
import { Guild } from '../../structures/guild.ts'
|
||||
import { EmojiPayload } from "../../types/emoji.ts"
|
||||
import { GuildEmojiUpdatePayload } from '../../types/gateway.ts'
|
||||
import { Gateway, GatewayEventHandler } from '../index.ts'
|
||||
|
||||
export const guildEmojiUpdate: GatewayEventHandler = (
|
||||
export const guildEmojiUpdate: GatewayEventHandler = async (
|
||||
gateway: Gateway,
|
||||
d: GuildEmojiUpdatePayload
|
||||
) => {
|
||||
const guild: Guild = cache.get('guild', d.guild_id)
|
||||
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id)
|
||||
if (guild !== undefined) {
|
||||
// const emojis = guild.emojis
|
||||
const emojis = await guild.emojis.collection()
|
||||
const deleted: Emoji[] = []
|
||||
const added: Emoji[] = []
|
||||
const updated: Array<{ before: Emoji, after: Emoji }> = []
|
||||
const _updated: EmojiPayload[] = []
|
||||
|
||||
for (const raw of d.emojis) {
|
||||
const has = emojis.get(raw.id)
|
||||
if (has === undefined) {
|
||||
await guild.emojis.set(raw.id, raw)
|
||||
const emoji = await guild.emojis.get(raw.id) as Emoji
|
||||
added.push(emoji)
|
||||
} else _updated.push(raw)
|
||||
}
|
||||
|
||||
for (const emoji of emojis.values()) {
|
||||
const find = _updated.find(e => emoji.id === e.id)
|
||||
if (find === undefined) {
|
||||
await guild.emojis.delete(emoji.id)
|
||||
deleted.push(emoji)
|
||||
} else {
|
||||
const before = await guild.emojis.get(find.id) as Emoji
|
||||
await guild.emojis.set(find.id, find)
|
||||
const after = await guild.emojis.get(find.id) as Emoji
|
||||
updated.push({ before, after })
|
||||
}
|
||||
}
|
||||
|
||||
for (const emoji of deleted) {
|
||||
gateway.client.emit('guildEmojiDelete', emoji)
|
||||
}
|
||||
|
||||
for (const emoji of added) {
|
||||
gateway.client.emit('guildEmojiAdd', emoji)
|
||||
}
|
||||
|
||||
for (const emoji of updated) {
|
||||
gateway.client.emit('guildEmojiUpdate', emoji.before, emoji.after)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,7 +186,7 @@ export interface GuildBanRemovePayload {
|
|||
|
||||
export interface GuildEmojiUpdatePayload {
|
||||
guild_id: string
|
||||
emojis: []
|
||||
emojis: EmojiPayload[]
|
||||
}
|
||||
|
||||
export interface GuildIntegrationsUpdatePayload {
|
||||
|
|
Loading…
Reference in a new issue