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