From 892b85ccb41e47c50de1a225b03e620062cb0553 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 25 Aug 2024 01:58:07 +0100 Subject: [PATCH] update msg log paste --- .../cc/fascinated/bat/common/DateUtils.java | 16 ++++++++++ .../bat/features/logging/LogFeature.java | 29 +++++++++++++++---- .../logging/listeners/MessageListener.java | 6 ++-- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/common/DateUtils.java b/src/main/java/cc/fascinated/bat/common/DateUtils.java index 4ab4a17..55095b3 100644 --- a/src/main/java/cc/fascinated/bat/common/DateUtils.java +++ b/src/main/java/cc/fascinated/bat/common/DateUtils.java @@ -3,12 +3,18 @@ package cc.fascinated.bat.common; import lombok.experimental.UtilityClass; import java.time.Instant; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; +import java.util.Locale; @UtilityClass public class DateUtils { + private static final ZoneId ZONE_ID = ZoneId.of("Europe/London"); private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_INSTANT; + private static final DateTimeFormatter SIMPLE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z") + .withLocale(Locale.UK) + .withZone(ZONE_ID); /** * Gets the date from a string. @@ -19,4 +25,14 @@ public class DateUtils { public static Date getDateFromString(String date) { return Date.from(Instant.from(FORMATTER.parse(date))); } + + /** + * Formats a date to a string. + * + * @param date The date to format. + * @return The formatted date. + */ + public String formatDate(Date date) { + return SIMPLE_FORMATTER.format(date.toInstant()); + } } diff --git a/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java b/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java index c2b13e1..71d2eef 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java +++ b/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java @@ -1,10 +1,12 @@ package cc.fascinated.bat.features.logging; +import cc.fascinated.bat.common.DateUtils; import cc.fascinated.bat.common.PasteUtils; import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.FeatureProfile; import cc.fascinated.bat.features.logging.command.LogsCommand; import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.DiscordMessage; import cc.fascinated.bat.service.CommandService; import lombok.NonNull; import net.dv8tion.jda.api.entities.MessageEmbed; @@ -13,6 +15,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; +import java.util.Date; +import java.util.List; + /** * @author Fascinated (fascinated7) */ @@ -32,7 +37,7 @@ public class LogFeature extends Feature { * Sends a log to the log channel * * @param guild the guild to send the log in - * @param type the type of log + * @param type the type of log * @param embed the embed to send */ public void sendLog(BatGuild guild, LogType type, MessageEmbed embed) { @@ -52,21 +57,33 @@ public class LogFeature extends Feature { } /** - * Formats the content to be sent in the log + * Formats the message content to be sent in the log * - * @param content the content to format + * @param message the message to format * @return the formatted content */ - public String formatContent(String content) { - if (content == null) { + public String formatContent(DiscordMessage message) { + if (message == null || message.getContent() == null || message.getContent().isEmpty()) { return "No content"; } + String content = message.getContent(); if (content.contains("`")) { // Workaround for Markdown formatting content = content.replace("`", "'"); } // More than 512 characters or is more than 4 lines if (content.length() > 512 || content.chars().filter(ch -> ch == '\n').count() > 4) { - return "*Content too long, [click here to view]("+PasteUtils.uploadPaste(content).getUrl()+")*"; + StringBuilder builder = new StringBuilder(); + builder.append("%s (%s) - %s | %s".formatted( + message.getAuthor().getGlobalName(), + message.getAuthor().getName(), + message.getAuthor().getId(), + DateUtils.formatDate(new Date(message.getTimestamp())) + )); + List contents = content.contains("\n") ? List.of(content.split("\n")) : List.of(content); + for (String line : contents) { + builder.append("\n %s".formatted(line)); + } + return "*Content too long, [click here to view](" + PasteUtils.uploadPaste(builder.toString()).getUrl() + ")*"; } // Less than or equal to 32 characters and no new lines if (content.length() <= 32 && !content.contains("\n")) { diff --git a/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java b/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java index a1578df..ecd2808 100644 --- a/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java +++ b/src/main/java/cc/fascinated/bat/features/logging/listeners/MessageListener.java @@ -38,7 +38,7 @@ public class MessageListener implements EventListener { .setDescription(new DescriptionBuilder("Message Deleted") .appendLine("Author: %s".formatted(message.getAuthor().getAsMention()), true) .appendLine("Channel: %s".formatted(message.getChannel().getAsMention()), true) - .appendLine("Content: %s".formatted(logFeature.formatContent(message.getContent())), true) + .appendLine("Content: %s".formatted(logFeature.formatContent(message)), true) .build()) .build()); } @@ -53,8 +53,8 @@ public class MessageListener implements EventListener { .setDescription(new DescriptionBuilder("Message Edited") .appendLine("Author: %s".formatted(newMessage.getAuthor().getAsMention()), true) .appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true) - .appendLine("Old Content: %s".formatted(logFeature.formatContent(oldMessage.getContent())), true) - .appendLine("New Content: %s".formatted(logFeature.formatContent(newMessage.getContent())), true) + .appendLine("Old Content: %s".formatted(logFeature.formatContent(oldMessage)), true) + .appendLine("New Content: %s".formatted(logFeature.formatContent(newMessage)), true) .appendLine("*[Jump to Message](%s)*".formatted(newMessage.getMessageUrl()), false) .build()) .build());