From 943b946e5bf511ddbef462c6f43aa17d7688772c Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sun, 15 Nov 2020 14:53:24 +0530 Subject: [PATCH] feat(events): add messageUpdate --- src/gateway/handlers/index.ts | 3 ++- src/gateway/handlers/messageUpdate.ts | 25 +++++++++++++++++++++++++ src/test/cmd.ts | 10 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/gateway/handlers/messageUpdate.ts diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index fe7fdad..3236c5a 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -14,6 +14,7 @@ import { messageCreate } from './messageCreate.ts' import { resume } from './resume.ts' import { reconnect } from './reconnect.ts' import { messageDelete } from "./messageDelete.ts" +import { messageUpdate } from "./messageUpdate.ts" export const gatewayHandlers: { [eventCode in GatewayEvents]: GatewayEventHandler | undefined @@ -42,7 +43,7 @@ export const gatewayHandlers: { INVITE_CREATE: undefined, INVITE_DELETE: undefined, MESSAGE_CREATE: messageCreate, - MESSAGE_UPDATE: undefined, + MESSAGE_UPDATE: messageUpdate, MESSAGE_DELETE: messageDelete, MESSAGE_DELETE_BULK: undefined, MESSAGE_REACTION_ADD: undefined, diff --git a/src/gateway/handlers/messageUpdate.ts b/src/gateway/handlers/messageUpdate.ts new file mode 100644 index 0000000..a444d43 --- /dev/null +++ b/src/gateway/handlers/messageUpdate.ts @@ -0,0 +1,25 @@ +import { Message } from "../../structures/message.ts" +import { TextChannel } from '../../structures/textChannel.ts' +import { User } from "../../structures/user.ts" +import { Gateway, GatewayEventHandler } from '../index.ts' + +export const messageUpdate: GatewayEventHandler = async ( + gateway: Gateway, + d: any +) => { + let channel = await gateway.client.channels.get(d.channel_id) + // Fetch the channel if not cached + if (channel === undefined) + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + channel = (await gateway.client.channels.fetch(d.channel_id)) as TextChannel + + const message = await channel.messages.get(d.id) + const author = new User(gateway.client, d.author) + const newMsg = new Message(gateway.client, d, channel, author) + if (message === undefined) { + await channel.messages.set(d.id, d) + return gateway.client.emit('messageUpdateUncached', newMsg) + } + await channel.messages.set(d.id, d) + gateway.client.emit('messageUpdate', message, newMsg) +} diff --git a/src/test/cmd.ts b/src/test/cmd.ts index eb00df5..c62c7a8 100644 --- a/src/test/cmd.ts +++ b/src/test/cmd.ts @@ -26,6 +26,16 @@ client.on('messageDeleteUncached', (d: MessageDeletePayload) => { console.log(`Uncached Message Deleted: ${d.id} in ${d.channel_id}`) }) +client.on('messageUpdate', (before: Message, after: Message) => { + console.log('Message Update') + console.log(`Before: ${before.author.tag}: ${before.content}`) + console.log(`After: ${after.author.tag}: ${after.content}`) +}) + +client.on('messageUpdateUncached', (msg: Message) => { + console.log(`Message: ${msg.author.tag}: ${msg.content}`) +}) + // client.on('messageCreate', msg => console.log(`${msg.author.tag}: ${msg.content}`)) client.on("commandError", console.error)