pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import client from "../utils/client.js"; |
| 2 | import keyValueList from "../utils/generateKeyValueList.js"; |
| 3 | import singleNotify from "../utils/singleNotify.js"; |
| 4 | import { saveAttachment } from "../reflex/scanners.js"; |
| 5 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 6 | import addPlural from "../utils/plurals.js"; |
| 7 | import type { Message } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 8 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 9 | export default async function logAttachment( |
| 10 | message: Message |
| 11 | ): Promise<AttachmentLogSchema> { |
| 12 | if (!message.guild) |
| 13 | throw new Error("Tried to log an attachment in a non-guild message"); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 14 | const { renderUser, renderChannel, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | const attachments = []; |
| 16 | for (const attachment of message.attachments.values()) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 17 | attachments.push({ |
| 18 | local: await saveAttachment(attachment.url), |
| 19 | url: attachment.url, |
| 20 | height: attachment.height, |
| 21 | width: attachment.width, |
| 22 | size: attachment.size |
| 23 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 24 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | const links = message.content.match(/https?:\/\/\S+/gi) || []; |
| 26 | for (const link of links) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | if ( |
| 28 | link |
| 29 | .toLowerCase() |
| 30 | .match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi) |
| 31 | ) { |
| 32 | attachments.push({ |
| 33 | local: await saveAttachment(link), |
| 34 | url: link, |
| 35 | height: null, |
| 36 | width: null |
| 37 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 38 | } |
| 39 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 40 | if (attachments.length === 0) return { files: [] }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 41 | if (client.database.premium.hasPremium(message.guild.id)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 42 | const channel = (await client.database.guilds.read(message.guild.id)) |
| 43 | .logging.attachments.channel; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 44 | if (!channel) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | singleNotify( |
| 46 | "noAttachmentLogChannel", |
| 47 | message.guild.id, |
| 48 | "No channel set for attachment logging", |
| 49 | "Warning" |
| 50 | ); |
| 51 | return { files: attachments }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 52 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | const channelObj = await client.channels.fetch(channel); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 54 | if (!channelObj) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | singleNotify( |
| 56 | "attachmentLogChannelDeleted", |
| 57 | message.guild.id, |
| 58 | "Attachment history channel was deleted", |
| 59 | "Warning" |
| 60 | ); |
| 61 | return { files: attachments }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 62 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | const m = await channelObj.send({ |
| 64 | embeds: [ |
| 65 | new EmojiEmbed() |
| 66 | .setTitle( |
| 67 | `${addPlural(attachments.length, "Attachment")} Sent` |
| 68 | ) |
| 69 | .setDescription( |
| 70 | keyValueList({ |
| 71 | messageId: `\`${message.id}\``, |
| 72 | sentBy: renderUser(message.author), |
| 73 | sentIn: renderChannel(message.channel), |
| 74 | sent: renderDelta( |
| 75 | new Date(message.createdTimestamp) |
| 76 | ) |
| 77 | }) + `\n[[Jump to message]](${message.url})` |
| 78 | ) |
| 79 | .setEmoji("ICONS.ATTACHMENT") |
| 80 | .setStatus("Success") |
| 81 | ], |
| 82 | files: attachments.map((file) => file.local) |
| 83 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 84 | // await client.database.guilds.write(interaction.guild.id, {[`tags.${name}`]: value}); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | client.database.guilds.write(message.guild.id, { |
| 86 | [`logging.attachments.saved.${message.channel.id}${message.id}`]: |
| 87 | m.url |
| 88 | }); |
| 89 | return { files: attachments, jump: m.url }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 90 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | return { files: attachments }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | export interface AttachmentLogSchema { |
| 96 | files: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | url: string; |
| 98 | local: string; |
| 99 | height: number | null; |
| 100 | width: number | null; |
| 101 | }[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 102 | jump?: string; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | } |