blob: 0c491b77fce9fc8d63d03f80974082fbefe7a985 [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
Skyler Grey11236ba2022-08-08 21:13:33 +01009export default async function logAttachment(message: Message): Promise<AttachmentLogSchema> {
10 if (!message.guild) throw new Error("Tried to log an attachment in a non-guild message");
pineafan813bdf42022-07-24 10:39:10 +010011 const { renderUser, renderChannel, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010012 const attachments = [];
13 for (const attachment of message.attachments.values()) {
Skyler Grey75ea9172022-08-06 10:22:23 +010014 attachments.push({
15 local: await saveAttachment(attachment.url),
16 url: attachment.url,
17 height: attachment.height,
18 width: attachment.width,
19 size: attachment.size
20 });
pineafan813bdf42022-07-24 10:39:10 +010021 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010022 const links = message.content.match(/https?:\/\/\S+/gi) ?? [];
pineafan63fc5e22022-08-04 22:04:10 +010023 for (const link of links) {
Skyler Grey11236ba2022-08-08 21:13:33 +010024 if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010025 attachments.push({
26 local: await saveAttachment(link),
27 url: link,
28 height: null,
29 width: null
30 });
pineafan813bdf42022-07-24 10:39:10 +010031 }
32 }
Skyler Grey75ea9172022-08-06 10:22:23 +010033 if (attachments.length === 0) return { files: [] };
pineafan813bdf42022-07-24 10:39:10 +010034 if (client.database.premium.hasPremium(message.guild.id)) {
Skyler Grey11236ba2022-08-08 21:13:33 +010035 const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel;
pineafan813bdf42022-07-24 10:39:10 +010036 if (!channel) {
Skyler Grey75ea9172022-08-06 10:22:23 +010037 singleNotify(
38 "noAttachmentLogChannel",
39 message.guild.id,
40 "No channel set for attachment logging",
41 "Warning"
42 );
43 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010044 }
pineafan63fc5e22022-08-04 22:04:10 +010045 const channelObj = await client.channels.fetch(channel);
pineafan813bdf42022-07-24 10:39:10 +010046 if (!channelObj) {
Skyler Grey75ea9172022-08-06 10:22:23 +010047 singleNotify(
48 "attachmentLogChannelDeleted",
49 message.guild.id,
50 "Attachment history channel was deleted",
51 "Warning"
52 );
53 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010054 }
Skyler Grey75ea9172022-08-06 10:22:23 +010055 const m = await channelObj.send({
56 embeds: [
57 new EmojiEmbed()
Skyler Grey11236ba2022-08-08 21:13:33 +010058 .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`)
Skyler Grey75ea9172022-08-06 10:22:23 +010059 .setDescription(
60 keyValueList({
61 messageId: `\`${message.id}\``,
62 sentBy: renderUser(message.author),
63 sentIn: renderChannel(message.channel),
Skyler Grey11236ba2022-08-08 21:13:33 +010064 sent: renderDelta(new Date(message.createdTimestamp))
Skyler Grey75ea9172022-08-06 10:22:23 +010065 }) + `\n[[Jump to message]](${message.url})`
66 )
67 .setEmoji("ICONS.ATTACHMENT")
68 .setStatus("Success")
69 ],
70 files: attachments.map((file) => file.local)
71 });
pineafan813bdf42022-07-24 10:39:10 +010072 // await client.database.guilds.write(interaction.guild.id, {[`tags.${name}`]: value});
Skyler Grey75ea9172022-08-06 10:22:23 +010073 client.database.guilds.write(message.guild.id, {
Skyler Grey11236ba2022-08-08 21:13:33 +010074 [`logging.attachments.saved.${message.channel.id}${message.id}`]: m.url
Skyler Grey75ea9172022-08-06 10:22:23 +010075 });
76 return { files: attachments, jump: m.url };
pineafan813bdf42022-07-24 10:39:10 +010077 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010079 }
80}
81
82export interface AttachmentLogSchema {
83 files: {
Skyler Grey75ea9172022-08-06 10:22:23 +010084 url: string;
85 local: string;
86 height: number | null;
87 width: number | null;
88 }[];
pineafan813bdf42022-07-24 10:39:10 +010089 jump?: string;
Skyler Grey75ea9172022-08-06 10:22:23 +010090}