blob: 899cd25fe9edac99ee2886b8389436b35c4fd609 [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 Grey75ea9172022-08-06 10:22:23 +01009export default async function logAttachment(
10 message: Message
11): Promise<AttachmentLogSchema> {
12 if (!message.guild)
13 throw new Error("Tried to log an attachment in a non-guild message");
pineafan813bdf42022-07-24 10:39:10 +010014 const { renderUser, renderChannel, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010015 const attachments = [];
16 for (const attachment of message.attachments.values()) {
Skyler Grey75ea9172022-08-06 10:22:23 +010017 attachments.push({
18 local: await saveAttachment(attachment.url),
19 url: attachment.url,
20 height: attachment.height,
21 width: attachment.width,
22 size: attachment.size
23 });
pineafan813bdf42022-07-24 10:39:10 +010024 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010025 const links = message.content.match(/https?:\/\/\S+/gi) ?? [];
pineafan63fc5e22022-08-04 22:04:10 +010026 for (const link of links) {
Skyler Grey75ea9172022-08-06 10:22:23 +010027 if (
28 link
29 .toLowerCase()
30 .match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)
31 ) {
32 attachments.push({
33 local: await saveAttachment(link),
34 url: link,
35 height: null,
36 width: null
37 });
pineafan813bdf42022-07-24 10:39:10 +010038 }
39 }
Skyler Grey75ea9172022-08-06 10:22:23 +010040 if (attachments.length === 0) return { files: [] };
pineafan813bdf42022-07-24 10:39:10 +010041 if (client.database.premium.hasPremium(message.guild.id)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010042 const channel = (await client.database.guilds.read(message.guild.id))
43 .logging.attachments.channel;
pineafan813bdf42022-07-24 10:39:10 +010044 if (!channel) {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 singleNotify(
46 "noAttachmentLogChannel",
47 message.guild.id,
48 "No channel set for attachment logging",
49 "Warning"
50 );
51 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010052 }
pineafan63fc5e22022-08-04 22:04:10 +010053 const channelObj = await client.channels.fetch(channel);
pineafan813bdf42022-07-24 10:39:10 +010054 if (!channelObj) {
Skyler Grey75ea9172022-08-06 10:22:23 +010055 singleNotify(
56 "attachmentLogChannelDeleted",
57 message.guild.id,
58 "Attachment history channel was deleted",
59 "Warning"
60 );
61 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010062 }
Skyler Grey75ea9172022-08-06 10:22:23 +010063 const m = await channelObj.send({
64 embeds: [
65 new EmojiEmbed()
66 .setTitle(
67 `${addPlural(attachments.length, "Attachment")} Sent`
68 )
69 .setDescription(
70 keyValueList({
71 messageId: `\`${message.id}\``,
72 sentBy: renderUser(message.author),
73 sentIn: renderChannel(message.channel),
74 sent: renderDelta(
75 new Date(message.createdTimestamp)
76 )
77 }) + `\n[[Jump to message]](${message.url})`
78 )
79 .setEmoji("ICONS.ATTACHMENT")
80 .setStatus("Success")
81 ],
82 files: attachments.map((file) => file.local)
83 });
pineafan813bdf42022-07-24 10:39:10 +010084 // await client.database.guilds.write(interaction.guild.id, {[`tags.${name}`]: value});
Skyler Grey75ea9172022-08-06 10:22:23 +010085 client.database.guilds.write(message.guild.id, {
86 [`logging.attachments.saved.${message.channel.id}${message.id}`]:
87 m.url
88 });
89 return { files: attachments, jump: m.url };
pineafan813bdf42022-07-24 10:39:10 +010090 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010091 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010092 }
93}
94
95export interface AttachmentLogSchema {
96 files: {
Skyler Grey75ea9172022-08-06 10:22:23 +010097 url: string;
98 local: string;
99 height: number | null;
100 width: number | null;
101 }[];
pineafan813bdf42022-07-24 10:39:10 +0100102 jump?: string;
Skyler Grey75ea9172022-08-06 10:22:23 +0100103}