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