blob: 9bb216d832d28c557416447f16b42c125ecfb34e [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import { getCommandMentionByName } from "./../utils/getCommandDataByName.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import client from "../utils/client.js";
3import keyValueList from "../utils/generateKeyValueList.js";
4import singleNotify from "../utils/singleNotify.js";
Skyler Grey32179982023-03-07 23:59:06 +00005import { saveAttachment } from "../reflex/scanners.js";
pineafan63fc5e22022-08-04 22:04:10 +01006import EmojiEmbed from "../utils/generateEmojiEmbed.js";
7import addPlural from "../utils/plurals.js";
PineaFan538d3752023-01-12 21:48:23 +00008import type { GuildTextBasedChannel, Message } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01009
Skyler Grey11236ba2022-08-08 21:13:33 +010010export default async function logAttachment(message: Message): Promise<AttachmentLogSchema> {
TheCodedProf9c51a7e2023-02-27 17:11:13 -050011 if (message.guild) client.database.premium.hasPremium(message.guild.id).finally(() => {});
Skyler Grey11236ba2022-08-08 21:13:33 +010012 if (!message.guild) throw new Error("Tried to log an attachment in a non-guild message");
pineafan813bdf42022-07-24 10:39:10 +010013 const { renderUser, renderChannel, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010014 const attachments = [];
15 for (const attachment of message.attachments.values()) {
Skyler Grey75ea9172022-08-06 10:22:23 +010016 attachments.push({
Skyler Grey32179982023-03-07 23:59:06 +000017 local: (await saveAttachment(attachment.url))[0],
Skyler Grey75ea9172022-08-06 10:22:23 +010018 url: attachment.url,
19 height: attachment.height,
20 width: attachment.width,
21 size: attachment.size
22 });
pineafan813bdf42022-07-24 10:39:10 +010023 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010024 const links = message.content.match(/https?:\/\/\S+/gi) ?? [];
pineafan63fc5e22022-08-04 22:04:10 +010025 for (const link of links) {
Skyler Grey11236ba2022-08-08 21:13:33 +010026 if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010027 attachments.push({
Skyler Grey32179982023-03-07 23:59:06 +000028 local: (await saveAttachment(link))[0],
Skyler Grey75ea9172022-08-06 10:22:23 +010029 url: link,
30 height: null,
31 width: null
32 });
pineafan813bdf42022-07-24 10:39:10 +010033 }
34 }
Skyler Grey75ea9172022-08-06 10:22:23 +010035 if (attachments.length === 0) return { files: [] };
PineaFan538d3752023-01-12 21:48:23 +000036 if (await client.database.premium.hasPremium(message.guild.id)) {
Skyler Grey11236ba2022-08-08 21:13:33 +010037 const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel;
pineafan813bdf42022-07-24 10:39:10 +010038 if (!channel) {
Skyler Greyf4f21c42023-03-08 14:36:29 +000039 await singleNotify(
Skyler Grey75ea9172022-08-06 10:22:23 +010040 "noAttachmentLogChannel",
41 message.guild.id,
Skyler Greyda16adf2023-03-05 10:22:12 +000042 `No channel set for attachment logging. You can set one with ${getCommandMentionByName(
43 "settings/logs/attachments"
44 )}`,
Skyler Grey75ea9172022-08-06 10:22:23 +010045 "Warning"
46 );
47 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010048 }
TheCodedProf1f675042023-02-16 17:01:29 -050049 const channelObj = await message.guild.channels.fetch(channel);
pineafan813bdf42022-07-24 10:39:10 +010050 if (!channelObj) {
Skyler Greyf4f21c42023-03-08 14:36:29 +000051 await singleNotify(
Skyler Grey75ea9172022-08-06 10:22:23 +010052 "attachmentLogChannelDeleted",
53 message.guild.id,
Skyler Greyda16adf2023-03-05 10:22:12 +000054 `Your attachment history channel was deleted or is not longer accessible. You can set a new one with ${getCommandMentionByName(
55 "settings/logs/attachments"
56 )}`,
Skyler Grey75ea9172022-08-06 10:22:23 +010057 "Warning"
58 );
59 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010060 }
PineaFan538d3752023-01-12 21:48:23 +000061 const m = await (channelObj as GuildTextBasedChannel).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010062 embeds: [
63 new EmojiEmbed()
Skyler Grey11236ba2022-08-08 21:13:33 +010064 .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`)
Skyler Grey75ea9172022-08-06 10:22:23 +010065 .setDescription(
66 keyValueList({
67 messageId: `\`${message.id}\``,
68 sentBy: renderUser(message.author),
PineaFan538d3752023-01-12 21:48:23 +000069 sentIn: renderChannel(message.channel as GuildTextBasedChannel),
Skyler Greyda16adf2023-03-05 10:22:12 +000070 sent: renderDelta(new Date(message.createdTimestamp).getTime())
Skyler Grey75ea9172022-08-06 10:22:23 +010071 }) + `\n[[Jump to message]](${message.url})`
72 )
73 .setEmoji("ICONS.ATTACHMENT")
74 .setStatus("Success")
75 ],
76 files: attachments.map((file) => file.local)
77 });
Skyler Greyf4f21c42023-03-08 14:36:29 +000078 await client.database.guilds.write(message.guild.id, {
Skyler Grey11236ba2022-08-08 21:13:33 +010079 [`logging.attachments.saved.${message.channel.id}${message.id}`]: m.url
Skyler Grey75ea9172022-08-06 10:22:23 +010080 });
81 return { files: attachments, jump: m.url };
pineafan813bdf42022-07-24 10:39:10 +010082 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010084 }
85}
86
87export interface AttachmentLogSchema {
88 files: {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 url: string;
90 local: string;
91 height: number | null;
92 width: number | null;
93 }[];
pineafan813bdf42022-07-24 10:39:10 +010094 jump?: string;
Skyler Grey75ea9172022-08-06 10:22:23 +010095}