From 5f1a97fd104fe12687c5c9d27fae9bd5ce4238f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Oszcz=C4=99da?= Date: Sat, 6 Jul 2024 12:27:22 +0100 Subject: [PATCH] refactor: Move code to generate log of messages to `JournallingStore`. --- source/library/commands/handlers/purge.ts | 15 ++------------- source/library/stores/journalling.ts | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/source/library/commands/handlers/purge.ts b/source/library/commands/handlers/purge.ts index 78aa4de21..bd0f44aba 100644 --- a/source/library/commands/handlers/purge.ts +++ b/source/library/commands/handlers/purge.ts @@ -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, @@ -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, diff --git a/source/library/stores/journalling.ts b/source/library/stores/journalling.ts index 3f7e55acb..1699c7d9f 100644 --- a/source/library/stores/journalling.ts +++ b/source/library/stores/journalling.ts @@ -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"; @@ -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 };