Skip to content

Commit

Permalink
feat: Log bulk message deletes. (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern authored Aug 18, 2024
2 parents 49c7930 + 2687929 commit 5b97379
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 18 deletions.
5 changes: 5 additions & 0 deletions assets/localisations/events/eng-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"events.messageDelete.title": "Message deleted",
"events.messageDelete.description": "{user} has deleted their message in {channel}.",
"events.messageDelete.fields.content": "Content",
"events.messageDeleteBulk.title": "Messages deleted",
"events.messageDeleteBulk.description": "{messages} in {channel}.",
"events.messageDeleteBulk.description.messages.one": "{one} message has been deleted",
"events.messageDeleteBulk.description.messages.two": "{two} messages have been deleted",
"events.messageDeleteBulk.description.messages.many": "{many} messages have been deleted",
"events.messageUpdate.title": "Message updated",
"events.messageUpdate.description": "{user} has updated their message in {channel}.",
"events.messageUpdate.fields.before": "Before",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "logos",
"description": "A multi-purpose community bot built to cater to language-learning communities on Discord.",
"license": "Apache-2.0",
"version": "4.20.3",
"version": "4.21.0",
"type": "module",
"keywords": [
"discord",
Expand Down
4 changes: 4 additions & 0 deletions source/constants/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,10 @@ export default Object.freeze({
content: localise("events.messageDelete.fields.content", locale)(),
},
}),
messageDeleteBulk: ({ localise, locale }) => ({
title: localise("events.messageDeleteBulk.title", locale)(),
description: localise("events.messageDeleteBulk.description", locale),
}),
messageUpdate: ({ localise, locale }) => ({
title: localise("events.messageUpdate.title", locale)(),
description: localise("events.messageUpdate.description", locale),
Expand Down
4 changes: 1 addition & 3 deletions source/library/commands/handlers/purge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mention, timestamp, trim } from "logos:core/formatting";
import type { Client } from "logos/client";
import { InteractionCollector } from "logos/collectors";
import { Guild } from "logos/models/guild";
import { JournallingStore } from "logos/stores/journalling";

async function handlePurgeMessagesAutocomplete(
client: Client,
Expand Down Expand Up @@ -553,11 +552,10 @@ async function handlePurgeMessages(
)} as requested by ${client.diagnostics.user(interaction.user)}.`,
);

const messageLog = JournallingStore.generateMessageLog(client, { messages });
await client.tryLog("purgeEnd", {
guildId: guild.id,
journalling: configuration.journaling,
args: [member, channel, deletedCount, messageLog],
args: [member, channel, deletedCount],
});

clearTimeout(responseDeletionTimeoutId);
Expand Down
14 changes: 14 additions & 0 deletions source/library/stores/journalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class JournallingStore {
readonly #guildMemberAddCollector: Collector<"guildMemberAdd">;
readonly #guildMemberRemoveCollector: Collector<"guildMemberRemove">;
readonly #messageDeleteCollector: Collector<"messageDelete">;
readonly #messageDeleteBulkCollector: Collector<"messageDeleteBulk">;
readonly #messageUpdateCollector: Collector<"messageUpdate">;

constructor(client: Client) {
Expand All @@ -26,6 +27,7 @@ class JournallingStore {
this.#guildMemberAddCollector = new Collector();
this.#guildMemberRemoveCollector = new Collector();
this.#messageDeleteCollector = new Collector();
this.#messageDeleteBulkCollector = new Collector();
this.#messageUpdateCollector = new Collector();
}

Expand All @@ -37,13 +39,15 @@ class JournallingStore {
this.#guildMemberAddCollector.onCollect(this.#guildMemberAdd.bind(this));
this.#guildMemberRemoveCollector.onCollect(this.#guildMemberRemove.bind(this));
this.#messageDeleteCollector.onCollect(this.#messageDelete.bind(this));
this.#messageDeleteBulkCollector.onCollect(this.#messageDeleteBulk.bind(this));
this.#messageUpdateCollector.onCollect(this.#messageUpdate.bind(this));

await this.#client.registerCollector("guildBanAdd", this.#guildBanAddCollector);
await this.#client.registerCollector("guildBanRemove", this.#guildBanRemoveCollector);
await this.#client.registerCollector("guildMemberAdd", this.#guildMemberAddCollector);
await this.#client.registerCollector("guildMemberRemove", this.#guildMemberRemoveCollector);
await this.#client.registerCollector("messageDelete", this.#messageDeleteCollector);
await this.#client.registerCollector("messageDeleteBulk", this.#messageDeleteBulkCollector);
await this.#client.registerCollector("messageUpdate", this.#messageUpdateCollector);

this.log.info("Journalling store set up.");
Expand All @@ -57,6 +61,7 @@ class JournallingStore {
this.#guildMemberAddCollector.close();
this.#guildMemberRemoveCollector.close();
this.#messageDeleteCollector.close();
this.#messageDeleteBulkCollector.close();
this.#messageUpdateCollector.close();

this.log.info("Journalling store torn down.");
Expand Down Expand Up @@ -183,6 +188,15 @@ class JournallingStore {
await this.tryLog("messageDelete", { guildId, args: [payload, message] });
}

async #messageDeleteBulk(payload: Discord.Events["messageDeleteBulk"][0]): Promise<void> {
const guildId = payload.guildId;
if (guildId === undefined) {
return;
}

await this.tryLog("messageDeleteBulk", { guildId, args: [payload] });
}

async #messageUpdate(message: Discord.Message): Promise<void> {
const guildId = message.guildId;
if (guildId === undefined) {
Expand Down
31 changes: 31 additions & 0 deletions source/library/stores/journalling/discord/message-delete-bulk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mention } from "logos:core/formatting";
import type { EventLogger } from "logos/stores/journalling/loggers";
import { JournallingStore } from "logos/stores/journalling.ts";
import { isDefined } from "logos:core/utilities.ts";

const logger: EventLogger<"messageDeleteBulk"> = (client, [payload], { guildLocale }) => {
const messages = payload.ids
.toReversed()
.map((messageId) => client.entities.messages.latest.get(messageId))
.filter(isDefined);
const messageLog = JournallingStore.generateMessageLog(client, { messages });

const strings = constants.contexts.messageDeleteBulk({ localise: client.localise, locale: guildLocale });
return {
embeds: [
{
title: `${constants.emojis.events.message.deleted} ${strings.title}`,
color: constants.colours.failure,
description: strings.description({
messages: client.pluralise("events.messageDeleteBulk.description.messages", guildLocale, {
quantity: messages.length,
}),
channel: mention(payload.channelId, { type: "channel" }),
}),
},
],
files: [{ name: "log.txt", blob: new Blob([messageLog]) } as Discord.FileContent],
};
};

export default logger;
2 changes: 2 additions & 0 deletions source/library/stores/journalling/loggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import guildBanRemove from "logos/stores/journalling/discord/guild-ban-remove";
import guildMemberAdd from "logos/stores/journalling/discord/guild-member-add";
import guildMemberRemove from "logos/stores/journalling/discord/guild-member-remove";
import messageDelete from "logos/stores/journalling/discord/message-delete";
import messageDeleteBulk from "logos/stores/journalling/discord/message-delete-bulk";
import messageUpdate from "logos/stores/journalling/discord/message-update";
import entryRequestAccept from "logos/stores/journalling/logos/entry-request-accept";
import entryRequestReject from "logos/stores/journalling/logos/entry-request-reject";
Expand Down Expand Up @@ -37,6 +38,7 @@ const loggers = Object.freeze({
guildMemberAdd,
guildMemberRemove,
messageDelete,
messageDeleteBulk,
messageUpdate,
guildMemberKick,
entryRequestSubmit,
Expand Down
9 changes: 2 additions & 7 deletions source/library/stores/journalling/logos/purge-end.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { mention } from "logos:core/formatting";
import type { EventLogger } from "logos/stores/journalling/loggers";

const logger: EventLogger<"purgeEnd"> = (
client,
[member, channel, messageCount, messageLog, author],
{ guildLocale },
) => {
const logger: EventLogger<"purgeEnd"> = (client, [member, channel, messageCount, author], { guildLocale }) => {
const strings = constants.contexts.purgeEnd({ localise: client.localise, locale: guildLocale });
return {
embeds: [
Expand All @@ -14,7 +10,7 @@ const logger: EventLogger<"purgeEnd"> = (
color: constants.colours.success,
description: strings.description({
moderator: client.diagnostics.member(member),
messages: client.pluralise("events.purgeBegin.description.messages", guildLocale, {
messages: client.pluralise("events.purgeEnd.description.messages", guildLocale, {
quantity: messageCount,
}),
channel: mention(channel.id, { type: "channel" }),
Expand All @@ -30,7 +26,6 @@ const logger: EventLogger<"purgeEnd"> = (
: undefined,
},
],
files: [{ name: "log.txt", blob: new Blob([messageLog]) } as Discord.FileContent],
};
};

Expand Down
8 changes: 1 addition & 7 deletions source/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,7 @@ declare global {
purgeBegin: [member: Logos.Member, channel: Logos.Channel, messageCount: number, author?: Logos.User];

/** A purging of messages is complete. */
purgeEnd: [
member: Logos.Member,
channel: Logos.Channel,
messageCount: number,
messageLog: string,
author?: Logos.User,
];
purgeEnd: [member: Logos.Member, channel: Logos.Channel, messageCount: number, author?: Logos.User];

/** A user has enabled slowmode in a channel. */
slowmodeEnable: [user: Logos.User, channel: Logos.Channel, level: SlowmodeLevel];
Expand Down

0 comments on commit 5b97379

Please sign in to comment.