harmony/src/gateway/handlers/messageUpdate.ts

29 lines
976 B
TypeScript
Raw Normal View History

2020-11-25 11:53:40 +00:00
import { Message } from '../../structures/message.ts'
2020-11-15 09:23:24 +00:00
import { TextChannel } from '../../structures/textChannel.ts'
2020-11-25 11:53:40 +00:00
import { MessagePayload } from '../../types/channel.ts'
2020-11-15 09:23:24 +00:00
import { Gateway, GatewayEventHandler } from '../index.ts'
export const messageUpdate: GatewayEventHandler = async (
gateway: Gateway,
2020-11-25 11:53:40 +00:00
d: MessagePayload
2020-11-15 09:23:24 +00:00
) => {
let channel = await gateway.client.channels.get<TextChannel>(d.channel_id)
// Fetch the channel if not cached
if (channel === undefined)
channel = await gateway.client.channels.fetch(d.channel_id)
if (channel === undefined) return
2020-11-15 09:23:24 +00:00
const message = await channel.messages.get(d.id)
if (message === undefined) return
const raw = await channel.messages._get(d.id)
if (raw === undefined) return
const newRaw = raw
Object.assign(newRaw, d)
await channel.messages.set(d.id, newRaw)
const newMsg = ((await channel.messages.get(d.id)) as unknown) as Message
2020-11-15 09:23:24 +00:00
gateway.client.emit('messageUpdate', message, newMsg)
}