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