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 | |
| 9 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +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()) { |
| 15 | attachments.push({local: await saveAttachment(attachment.url), url: attachment.url, height: attachment.height, width: attachment.width, size: attachment.size}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 16 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | const links = message.content.match(/https?:\/\/\S+/gi) || []; |
| 18 | for (const link of links) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 19 | if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | attachments.push({local: await saveAttachment(link), url: link, height: null, width: null}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 21 | } |
| 22 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | if (attachments.length === 0) return {files: []}; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 24 | if (client.database.premium.hasPremium(message.guild.id)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 26 | if (!channel) { |
| 27 | singleNotify("noAttachmentLogChannel", message.guild.id, "No channel set for attachment logging", "Warning"); |
| 28 | return {files: attachments}; |
| 29 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | const channelObj = await client.channels.fetch(channel); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 31 | if (!channelObj) { |
| 32 | singleNotify("attachmentLogChannelDeleted", message.guild.id, "Attachment history channel was deleted", "Warning"); |
| 33 | return {files: attachments}; |
| 34 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | const m = await channelObj.send({embeds: [new EmojiEmbed() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 36 | .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`) |
| 37 | .setDescription(keyValueList({ |
| 38 | "messageId": `\`${message.id}\``, |
| 39 | "sentBy": renderUser(message.author), |
| 40 | "sentIn": renderChannel(message.channel), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | "sent": renderDelta(new Date(message.createdTimestamp)) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 42 | }) + `\n[[Jump to message]](${message.url})`) |
| 43 | .setEmoji("ICONS.ATTACHMENT") |
| 44 | .setStatus("Success") |
| 45 | ], files: attachments.map(file => file.local)}); |
| 46 | // await client.database.guilds.write(interaction.guild.id, {[`tags.${name}`]: value}); |
| 47 | client.database.guilds.write( |
| 48 | message.guild.id, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | {[`logging.attachments.saved.${message.channel.id}${message.id}`]: m.url} |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 50 | ); |
| 51 | return {files: attachments, jump: m.url}; |
| 52 | } else { |
| 53 | return {files: attachments}; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | export interface AttachmentLogSchema { |
| 58 | files: { |
| 59 | url: string, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | local: string, |
| 61 | height: number | null, |
| 62 | width: number | null |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 63 | }[], |
| 64 | jump?: string; |
| 65 | } |