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> { |
| 11 | 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] | 12 | const { renderUser, renderChannel, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | const attachments = []; |
| 14 | for (const attachment of message.attachments.values()) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | attachments.push({ |
| 16 | local: await saveAttachment(attachment.url), |
| 17 | url: attachment.url, |
| 18 | height: attachment.height, |
| 19 | width: attachment.width, |
| 20 | size: attachment.size |
| 21 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 22 | } |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 23 | const links = message.content.match(/https?:\/\/\S+/gi) ?? []; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | for (const link of links) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 25 | 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] | 26 | attachments.push({ |
| 27 | local: await saveAttachment(link), |
| 28 | url: link, |
| 29 | height: null, |
| 30 | width: null |
| 31 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | if (attachments.length === 0) return { files: [] }; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 35 | if (await client.database.premium.hasPremium(message.guild.id)) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 36 | const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 37 | if (!channel) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | singleNotify( |
| 39 | "noAttachmentLogChannel", |
| 40 | message.guild.id, |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame^] | 41 | `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] | 42 | "Warning" |
| 43 | ); |
| 44 | return { files: attachments }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 45 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | const channelObj = await client.channels.fetch(channel); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 47 | if (!channelObj) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | singleNotify( |
| 49 | "attachmentLogChannelDeleted", |
| 50 | message.guild.id, |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame^] | 51 | `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] | 52 | "Warning" |
| 53 | ); |
| 54 | return { files: attachments }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 55 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 56 | const m = await (channelObj as GuildTextBasedChannel).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 57 | embeds: [ |
| 58 | new EmojiEmbed() |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 59 | .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | .setDescription( |
| 61 | keyValueList({ |
| 62 | messageId: `\`${message.id}\``, |
| 63 | sentBy: renderUser(message.author), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 64 | sentIn: renderChannel(message.channel as GuildTextBasedChannel), |
| 65 | sent: renderDelta((new Date(message.createdTimestamp)).getTime()) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | }) + `\n[[Jump to message]](${message.url})` |
| 67 | ) |
| 68 | .setEmoji("ICONS.ATTACHMENT") |
| 69 | .setStatus("Success") |
| 70 | ], |
| 71 | files: attachments.map((file) => file.local) |
| 72 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 73 | // await client.database.guilds.write(interaction.guild.id, {[`tags.${name}`]: value}); |
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 | } |