blob: 8cfe080869551a3d72fe0786fa1d08588fb9bdc9 [file] [log] [blame]
PineaFane6ba7882023-01-18 20:41:16 +00001import { getCommandMentionByName } from './../utils/getCommandMentionByName';
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({
16 local: await saveAttachment(attachment.url),
17 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({
27 local: await saveAttachment(link),
28 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,
PineaFane6ba7882023-01-18 20:41:16 +000041 `No channel set for attachment logging. You can set one with ${await 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 }
pineafan63fc5e22022-08-04 22:04:10 +010046 const channelObj = await client.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,
PineaFane6ba7882023-01-18 20:41:16 +000051 `Your attachment history channel was deleted or is not longer accessible. You can set a new one with ${await 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 });
pineafan813bdf42022-07-24 10:39:10 +010073 // await client.database.guilds.write(interaction.guild.id, {[`tags.${name}`]: value});
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}