blob: ed9a4ab95a1bee04aa7580be19f349a4eebeb997 [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import client from '../utils/client.js';
2import keyValueList from '../utils/generateKeyValueList.js';
3import singleNotify from '../utils/singleNotify.js';
4import { saveAttachment } from '../reflex/scanners.js';
5import EmojiEmbed from '../utils/generateEmojiEmbed.js';
6import addPlural from '../utils/plurals.js';
7
8
9export 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 }
pineafane23c4ec2022-07-27 21:56:27 +010021 if (attachments.length === 0) return {files: []}
pineafan813bdf42022-07-24 10:39:10 +010022 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
55export interface AttachmentLogSchema {
56 files: {
57 url: string,
58 local: string;
59 }[],
60 jump?: string;
61}