blob: 5a771bc0ee2a662a9a978cc01906e31dcbbae50b [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04: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";
7import type { Message } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01008
9
pineafan63fc5e22022-08-04 22:04:10 +010010export 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");
pineafan813bdf42022-07-24 10:39:10 +010012 const { renderUser, renderChannel, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010013 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});
pineafan813bdf42022-07-24 10:39:10 +010016 }
pineafan63fc5e22022-08-04 22:04:10 +010017 const links = message.content.match(/https?:\/\/\S+/gi) || [];
18 for (const link of links) {
pineafan813bdf42022-07-24 10:39:10 +010019 if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) {
pineafan63fc5e22022-08-04 22:04:10 +010020 attachments.push({local: await saveAttachment(link), url: link, height: null, width: null});
pineafan813bdf42022-07-24 10:39:10 +010021 }
22 }
pineafan63fc5e22022-08-04 22:04:10 +010023 if (attachments.length === 0) return {files: []};
pineafan813bdf42022-07-24 10:39:10 +010024 if (client.database.premium.hasPremium(message.guild.id)) {
pineafan63fc5e22022-08-04 22:04:10 +010025 const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel;
pineafan813bdf42022-07-24 10:39:10 +010026 if (!channel) {
27 singleNotify("noAttachmentLogChannel", message.guild.id, "No channel set for attachment logging", "Warning");
28 return {files: attachments};
29 }
pineafan63fc5e22022-08-04 22:04:10 +010030 const channelObj = await client.channels.fetch(channel);
pineafan813bdf42022-07-24 10:39:10 +010031 if (!channelObj) {
32 singleNotify("attachmentLogChannelDeleted", message.guild.id, "Attachment history channel was deleted", "Warning");
33 return {files: attachments};
34 }
pineafan63fc5e22022-08-04 22:04:10 +010035 const m = await channelObj.send({embeds: [new EmojiEmbed()
pineafan813bdf42022-07-24 10:39:10 +010036 .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`)
37 .setDescription(keyValueList({
38 "messageId": `\`${message.id}\``,
39 "sentBy": renderUser(message.author),
40 "sentIn": renderChannel(message.channel),
pineafan63fc5e22022-08-04 22:04:10 +010041 "sent": renderDelta(new Date(message.createdTimestamp))
pineafan813bdf42022-07-24 10:39:10 +010042 }) + `\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,
pineafan63fc5e22022-08-04 22:04:10 +010049 {[`logging.attachments.saved.${message.channel.id}${message.id}`]: m.url}
pineafan813bdf42022-07-24 10:39:10 +010050 );
51 return {files: attachments, jump: m.url};
52 } else {
53 return {files: attachments};
54 }
55}
56
57export interface AttachmentLogSchema {
58 files: {
59 url: string,
pineafan63fc5e22022-08-04 22:04:10 +010060 local: string,
61 height: number | null,
62 width: number | null
pineafan813bdf42022-07-24 10:39:10 +010063 }[],
64 jump?: string;
65}