blob: 3c583f261acfb5522d383525fbe32e0233a3a880 [file] [log] [blame]
TheCodedProff86ba092023-01-27 17:10:07 -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> {
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()) {
Skyler Grey75ea9172022-08-06 10:22:23 +010015 attachments.push({
TheCodedProf4f79da12023-01-31 16:50:37 -050016 local: (await saveAttachment(attachment.url))[0],
Skyler Grey75ea9172022-08-06 10:22:23 +010017 url: attachment.url,
18 height: attachment.height,
19 width: attachment.width,
20 size: attachment.size
21 });
pineafan813bdf42022-07-24 10:39:10 +010022 }
Skyler Greyc634e2b2022-08-06 17:50:48 +010023 const links = message.content.match(/https?:\/\/\S+/gi) ?? [];
pineafan63fc5e22022-08-04 22:04:10 +010024 for (const link of links) {
Skyler Grey11236ba2022-08-08 21:13:33 +010025 if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010026 attachments.push({
TheCodedProf4f79da12023-01-31 16:50:37 -050027 local: (await saveAttachment(link))[0],
Skyler Grey75ea9172022-08-06 10:22:23 +010028 url: link,
29 height: null,
30 width: null
31 });
pineafan813bdf42022-07-24 10:39:10 +010032 }
33 }
Skyler Grey75ea9172022-08-06 10:22:23 +010034 if (attachments.length === 0) return { files: [] };
PineaFan538d3752023-01-12 21:48:23 +000035 if (await client.database.premium.hasPremium(message.guild.id)) {
Skyler Grey11236ba2022-08-08 21:13:33 +010036 const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel;
pineafan813bdf42022-07-24 10:39:10 +010037 if (!channel) {
Skyler Grey75ea9172022-08-06 10:22:23 +010038 singleNotify(
39 "noAttachmentLogChannel",
40 message.guild.id,
TheCodedProff86ba092023-01-27 17:10:07 -050041 `No channel set for attachment logging. You can set one with ${getCommandMentionByName("settings/logs/attachments")}`,
Skyler Grey75ea9172022-08-06 10:22:23 +010042 "Warning"
43 );
44 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010045 }
TheCodedProf1f675042023-02-16 17:01:29 -050046 const channelObj = await message.guild.channels.fetch(channel);
pineafan813bdf42022-07-24 10:39:10 +010047 if (!channelObj) {
Skyler Grey75ea9172022-08-06 10:22:23 +010048 singleNotify(
49 "attachmentLogChannelDeleted",
50 message.guild.id,
TheCodedProff86ba092023-01-27 17:10:07 -050051 `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 +010052 "Warning"
53 );
54 return { files: attachments };
pineafan813bdf42022-07-24 10:39:10 +010055 }
PineaFan538d3752023-01-12 21:48:23 +000056 const m = await (channelObj as GuildTextBasedChannel).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010057 embeds: [
58 new EmojiEmbed()
Skyler Grey11236ba2022-08-08 21:13:33 +010059 .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`)
Skyler Grey75ea9172022-08-06 10:22:23 +010060 .setDescription(
61 keyValueList({
62 messageId: `\`${message.id}\``,
63 sentBy: renderUser(message.author),
PineaFan538d3752023-01-12 21:48:23 +000064 sentIn: renderChannel(message.channel as GuildTextBasedChannel),
65 sent: renderDelta((new Date(message.createdTimestamp)).getTime())
Skyler Grey75ea9172022-08-06 10:22:23 +010066 }) + `\n[[Jump to message]](${message.url})`
67 )
68 .setEmoji("ICONS.ATTACHMENT")
69 .setStatus("Success")
70 ],
71 files: attachments.map((file) => file.local)
72 });
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}