Skip to content

Commit

Permalink
feat: Log deleted images and attachments in messages. (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern authored Jul 24, 2024
2 parents 8b1884e + 5f15cbd commit 4540711
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
9 changes: 8 additions & 1 deletion source/constants/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const properties = {
content: true,
embeds: true,
author: true,
attachments: true,
interaction: {},
messageReference: {},
},
Expand All @@ -67,7 +68,13 @@ const properties = {
roles: true,
user: true,
},
attachment: {},
attachment: {
id: true,
filename: true,
contentType: true,
url: true,
proxyUrl: true,
},
interaction: {
id: true,
applicationId: true,
Expand Down
31 changes: 31 additions & 0 deletions source/library/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DiscordConnection {
readonly messages: {
readonly latest: Map<bigint, Logos.Message>;
readonly previous: Map<bigint, Logos.Message>;
readonly fileContents: Map<bigint, Discord.FileContent>;
};
readonly roles: Map<bigint, Logos.Role>;
};
Expand All @@ -31,6 +32,7 @@ class DiscordConnection {
messages: {
latest: new Map(),
previous: new Map(),
fileContents: new Map(),
},
roles: new Map(),
};
Expand Down Expand Up @@ -151,9 +153,38 @@ class DiscordConnection {
}
}

const uncachedAttachments = result.attachments?.filter(
(attachment) => !this.cache.messages.fileContents.has(attachment.id),
);

DiscordConnection.#downloadAttachments(uncachedAttachments ?? []).then((downloaded) => {
for (const [attachment, fileContent] of downloaded) {
this.cache.messages.fileContents.set(attachment.id, fileContent);
}
});

return result;
}

static async #downloadAttachments(
attachments: Discord.Attachment[],
): Promise<[attachment: Discord.Attachment, fileContent: Discord.FileContent][]> {
if (attachments.length === 0) {
return [];
}

return Promise.all(
attachments.map((attachment) =>
fetch(attachment.url)
.then((response) => response.blob())
.then<[attachment: Discord.Attachment, fileContent: Discord.FileContent]>((blob) => [
attachment,
{ name: attachment.filename, blob } as Discord.FileContent,
]),
),
);
}

#transformRole(_: Discord.Bot, payload: Parameters<Discord.Transformers["role"]>[1]): Discord.Role {
const result = Discord.transformRole(this.bot, payload);

Expand Down
26 changes: 18 additions & 8 deletions source/library/stores/journalling/discord/message-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ const logger: EventLogger<"messageDelete"> = (client, [payload, _], { guildLocal
return undefined;
}

if (message.content === undefined) {
return undefined;
const fileContents: Discord.FileContent[] = [];
for (const attachment of message.attachments ?? []) {
const fileContent = client.entities.messages.fileContents.get(attachment.id);
if (fileContent === undefined) {
continue;
}

fileContents.push(fileContent);
}

const strings = constants.contexts.messageDelete({ localise: client.localise, locale: guildLocale });
Expand All @@ -21,14 +27,18 @@ const logger: EventLogger<"messageDelete"> = (client, [payload, _], { guildLocal
user: client.diagnostics.user(message.author),
channel: mention(message.channelId, { type: "channel" }),
}),
fields: [
{
name: strings.fields.content,
value: codeMultiline(message.content),
},
],
fields:
message.content !== undefined
? [
{
name: strings.fields.content,
value: codeMultiline(message.content),
},
]
: undefined,
},
],
files: fileContents.length > 0 ? fileContents : undefined,
};
};

Expand Down

0 comments on commit 4540711

Please sign in to comment.