diff --git a/src/utils/transformers.ts b/src/utils/transformers.ts index 125766e..1d3a460 100644 --- a/src/utils/transformers.ts +++ b/src/utils/transformers.ts @@ -1,4 +1,9 @@ -import { AnyThreadChannel, Attachment, HexColorString } from "discord.js"; +import { + AnyThreadChannel, + Attachment, + HexColorString, + Message, +} from "discord.js"; type TransformPost = { id: string; @@ -70,7 +75,39 @@ type TransformMessageReaction = { /** * - * @param {AnyThreadChannel} thread - Help channel thread. + * @param {Message} message - Transform Message to JSON. + * @returns {TransformMessage} - Returns transformed Message. + */ +export const messageTransform = async (message: Message) => { + const messageJson = message.toJSON() as TransformMessage; + // mSs.push(messageJson); + messageJson.attachments = []; + for (const [, attachmentValue] of message.attachments) { + const attachmentJson = attachmentValue; + messageJson.attachments.push(attachmentJson); + } + const messageAuthor = message.author; + const messageAuthorJson = messageAuthor.toJSON() as TransformUser; + const messageAuthorValue = { + ...messageAuthorJson, + avatarDecorationURL: messageAuthor.avatarDecorationURL(), + avatarURL: messageAuthor.avatarURL(), + bannerUrl: messageAuthor.bannerURL(), + }; + messageJson.author = messageAuthorValue; + for (const [, messageReactionValue] of message.reactions.cache) { + const messageReactionJson = + messageReactionValue.toJSON() as TransformMessageReaction; + const messageReactionJsonEmoji = messageReactionJson["emoji"] || {}; + messageReactionJsonEmoji.imageURL = messageReactionValue.emoji.imageURL(); + messageJson.reactions.push(messageReactionJson); + } + return messageJson; +}; + +/** + * + * @param {AnyThreadChannel} thread - Transform ThreadChannel to JSON. */ export const threadTransform = async (thread: AnyThreadChannel) => { try { @@ -78,30 +115,7 @@ export const threadTransform = async (thread: AnyThreadChannel) => { const customMessages = []; const messages = await thread.messages.fetch(); for (const [, messageValue] of messages.entries()) { - const messageJson = messageValue.toJSON() as TransformMessage; - // mSs.push(messageJson); - messageJson.attachments = []; - for (const [, attachmentValue] of messageValue.attachments) { - const attachmentJson = attachmentValue; - messageJson.attachments.push(attachmentJson); - } - const messageAuthor = messageValue.author; - const messageAuthorJson = messageAuthor.toJSON() as TransformUser; - const messageAuthorValue = { - ...messageAuthorJson, - avatarDecorationURL: messageAuthor.avatarDecorationURL(), - avatarURL: messageAuthor.avatarURL(), - bannerUrl: messageAuthor.bannerURL(), - }; - messageJson.author = messageAuthorValue; - for (const [, messageReactionValue] of messageValue.reactions.cache) { - const messageReactionJson = - messageReactionValue.toJSON() as TransformMessageReaction; - const messageReactionJsonEmoji = messageReactionJson["emoji"] || {}; - messageReactionJsonEmoji.imageURL = - messageReactionValue.emoji.imageURL(); - messageJson.reactions.push(messageReactionJson); - } + const messageJson = await messageTransform(messageValue); customMessages.push(messageJson); } threadData.messages = customMessages;