blob: b2c8391e4653f528ca58cc5957ea8b88617d55a1 [file] [log] [blame]
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05001import { 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";
5import { saveAttachment } from "../reflex/scanners.js";
6import 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> {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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({
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050017 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({
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050028 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 Grey75ea9172022-08-06 10:22:23 +010039 singleNotify(
40 "noAttachmentLogChannel",
41 message.guild.id,
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050042 `No channel set for attachment logging. You can set one with ${getCommandMentionByName("settings/logs/attachments")}`,
Skyler Grey75ea9172022-08-06 10:22:23 +010043 "Warning"
44 );
45 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010046 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050047 const channelObj = await message.guild.channels.fetch(channel);
pineafan813bdf42022-07-24 10:39:10 +010048 if (!channelObj) {
Skyler Grey75ea9172022-08-06 10:22:23 +010049 singleNotify(
50 "attachmentLogChannelDeleted",
51 message.guild.id,
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050052 `Your attachment history channel was deleted or is not longer accessible. You can set a new one with ${getCommandMentionByName("settings/logs/attachments")}`,
Skyler Grey75ea9172022-08-06 10:22:23 +010053 "Warning"
54 );
55 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010056 }
PineaFan538d3752023-01-12 21:48:23 +000057 const m = await (channelObj as GuildTextBasedChannel).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010058 embeds: [
59 new EmojiEmbed()
Skyler Grey11236ba2022-08-08 21:13:33 +010060 .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`)
Skyler Grey75ea9172022-08-06 10:22:23 +010061 .setDescription(
62 keyValueList({
63 messageId: `\`${message.id}\``,
64 sentBy: renderUser(message.author),
PineaFan538d3752023-01-12 21:48:23 +000065 sentIn: renderChannel(message.channel as GuildTextBasedChannel),
66 sent: renderDelta((new Date(message.createdTimestamp)).getTime())
Skyler Grey75ea9172022-08-06 10:22:23 +010067 }) + `\n[[Jump to message]](${message.url})`
68 )
69 .setEmoji("ICONS.ATTACHMENT")
70 .setStatus("Success")
71 ],
72 files: attachments.map((file) => file.local)
73 });
Skyler Grey75ea9172022-08-06 10:22:23 +010074 client.database.guilds.write(message.guild.id, {
Skyler Grey11236ba2022-08-08 21:13:33 +010075 [`logging.attachments.saved.${message.channel.id}${message.id}`]: m.url
Skyler Grey75ea9172022-08-06 10:22:23 +010076 });
77 return { files: attachments, jump: m.url };
pineafan813bdf42022-07-24 10:39:10 +010078 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010080 }
81}
82
83export interface AttachmentLogSchema {
84 files: {
Skyler Grey75ea9172022-08-06 10:22:23 +010085 url: string;
86 local: string;
87 height: number | null;
88 width: number | null;
89 }[];
pineafan813bdf42022-07-24 10:39:10 +010090 jump?: string;
Skyler Grey75ea9172022-08-06 10:22:23 +010091}