Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RaiVaibhav committed May 16, 2024
1 parent f3eba3a commit 0c5433e
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions src/utils/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AnyThreadChannel, Attachment, HexColorString } from "discord.js";
import {
AnyThreadChannel,
Attachment,
HexColorString,
Message,
} from "discord.js";

type TransformPost = {
id: string;
Expand Down Expand Up @@ -70,38 +75,47 @@ 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 {
const threadData = thread.toJSON() as TransformPost;
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;
Expand Down

0 comments on commit 0c5433e

Please sign in to comment.