Skip to content

Commit

Permalink
refactor: Move code to generate log of messages to JournallingStore.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Jul 6, 2024
1 parent 08d5cf8 commit 5f1a97f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
15 changes: 2 additions & 13 deletions source/library/commands/handlers/purge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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.ts";

async function handlePurgeMessagesAutocomplete(
client: Client,
Expand Down Expand Up @@ -564,19 +565,7 @@ async function handlePurgeMessages(
)} as requested by ${client.diagnostics.user(interaction.user)}.`,
);

const messageLog = messages
.map((message) => {
const postingTime = new Date(Discord.snowflakeToTimestamp(message.id)).toLocaleString();
const username = client.diagnostics.user(message.author);
const content = message.content
.split("\n")
.map((line) => ` ${line}`)
.join("\n");

return `[${postingTime}] ${username}:\n\n${content}`;
})
.join("\n\n");

const messageLog = JournallingStore.generateMessageLog(client, { messages });
await client.tryLog("purgeEnd", {
guildId: guild.id,
journalling: configuration.journaling,
Expand Down
16 changes: 15 additions & 1 deletion source/library/stores/journalling.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getLocaleByLocalisationLanguage } from "logos:constants/languages";
import type * as Discord from "@discordeno/bot";
import type { Client } from "logos/client";
import { Collector } from "logos/collectors";
import { Logger } from "logos/logger";
Expand Down Expand Up @@ -139,6 +138,21 @@ class JournallingStore {
this.log.warn(`Failed to log '${event}' event on ${this.#client.diagnostics.guild(guildId)}:`, reason),
);
}

static generateMessageLog(client: Client, { messages }: { messages: Logos.Message[] }): string {
return messages.map((message) => JournallingStore.#messageLogEntry(client, message)).join("\n\n");
}

static #messageLogEntry(client: Client, message: Logos.Message): string {
const postingTime = new Date(Discord.snowflakeToTimestamp(message.id)).toLocaleString();
const username = client.diagnostics.user(message.author);
const content = message.content
.split("\n")
.map((line) => ` ${line}`)
.join("\n");

return `[${postingTime}] ${username}:\n\n${content}`;
}
}

export { JournallingStore };

0 comments on commit 5f1a97f

Please sign in to comment.