PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import type Discord from 'discord.js'; |
| 2 | |
| 3 | export interface JSONTranscriptSchema { |
| 4 | messages: { |
| 5 | content: string | null; |
| 6 | attachments: { |
| 7 | url: string; |
| 8 | name: string; |
| 9 | size: number; |
| 10 | }[]; |
| 11 | authorID: string; |
| 12 | authorUsername: string; |
| 13 | authorUsernameColor: string; |
| 14 | timestamp: string; |
| 15 | id: string; |
| 16 | edited: boolean; |
| 17 | }[]; |
| 18 | channel: string; |
| 19 | guild: string; |
| 20 | timestamp: string; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | export const JSONTranscriptFromMessageArray = (messages: Discord.Message[]): JSONTranscriptSchema | null => { |
| 25 | if (messages.length === 0) return null; |
| 26 | return { |
| 27 | guild: messages[0]!.guild!.id, |
| 28 | channel: messages[0]!.channel.id, |
| 29 | timestamp: Date.now().toString(), |
| 30 | messages: messages.map((message: Discord.Message) => { |
| 31 | return { |
| 32 | content: message.content, |
| 33 | attachments: message.attachments.map((attachment: Discord.Attachment) => { |
| 34 | return { |
| 35 | url: attachment.url, |
| 36 | name: attachment.name!, |
| 37 | size: attachment.size, |
| 38 | }; |
| 39 | }), |
| 40 | authorID: message.author.id, |
| 41 | authorUsername: message.author.username + "#" + message.author.discriminator, |
| 42 | authorUsernameColor: message.member!.displayHexColor.toString(), |
| 43 | timestamp: message.createdTimestamp.toString(), |
| 44 | id: message.id, |
| 45 | edited: message.editedTimestamp ? true : false, |
| 46 | }; |
| 47 | }) |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | export const JSONTranscriptToHumanReadable = (data: JSONTranscriptSchema): string => { |
| 52 | let out = ""; |
| 53 | |
| 54 | for (const message of data.messages) { |
| 55 | const date = new Date(parseInt(message.timestamp)); |
| 56 | out += `${message.authorUsername} (${message.authorID}) [${date}]`; |
| 57 | if (message.edited) out += " (edited)"; |
| 58 | if (message.content) out += "\nContent:\n" + message.content.split("\n").map((line: string) => `\n> ${line}`).join(""); |
| 59 | if (message.attachments.length > 0) out += "\nAttachments:\n" + message.attachments.map((attachment: { url: string; name: string; size: number; }) => `\n> [${attachment.name}](${attachment.url}) (${attachment.size} bytes)`).join("\n"); |
| 60 | |
| 61 | out += "\n\n"; |
| 62 | } |
| 63 | return out; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 64 | } |