refactor(events:guildEmojiUpdate): split up into add, delete, update events

This commit is contained in:
DjDeveloperr 2020-11-15 13:29:07 +05:30
parent e72e702764
commit 45673d8197
2 changed files with 44 additions and 5 deletions

View file

@ -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)
}
}
}

View file

@ -186,7 +186,7 @@ export interface GuildBanRemovePayload {
export interface GuildEmojiUpdatePayload {
guild_id: string
emojis: []
emojis: EmojiPayload[]
}
export interface GuildIntegrationsUpdatePayload {