update msg log paste
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 2m6s

This commit is contained in:
Lee 2024-08-25 01:58:07 +01:00
parent f291344c45
commit 892b85ccb4
3 changed files with 42 additions and 9 deletions

@ -3,12 +3,18 @@ package cc.fascinated.bat.common;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.Locale;
@UtilityClass @UtilityClass
public class DateUtils { 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 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. * Gets the date from a string.
@ -19,4 +25,14 @@ public class DateUtils {
public static Date getDateFromString(String date) { public static Date getDateFromString(String date) {
return Date.from(Instant.from(FORMATTER.parse(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());
}
} }

@ -1,10 +1,12 @@
package cc.fascinated.bat.features.logging; package cc.fascinated.bat.features.logging;
import cc.fascinated.bat.common.DateUtils;
import cc.fascinated.bat.common.PasteUtils; import cc.fascinated.bat.common.PasteUtils;
import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.FeatureProfile; import cc.fascinated.bat.features.FeatureProfile;
import cc.fascinated.bat.features.logging.command.LogsCommand; import cc.fascinated.bat.features.logging.command.LogsCommand;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.DiscordMessage;
import cc.fascinated.bat.service.CommandService; import cc.fascinated.bat.service.CommandService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.entities.MessageEmbed; 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.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@ -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 * @return the formatted content
*/ */
public String formatContent(String content) { public String formatContent(DiscordMessage message) {
if (content == null) { if (message == null || message.getContent() == null || message.getContent().isEmpty()) {
return "No content"; return "No content";
} }
String content = message.getContent();
if (content.contains("`")) { // Workaround for Markdown formatting if (content.contains("`")) { // Workaround for Markdown formatting
content = content.replace("`", "'"); content = content.replace("`", "'");
} }
// More than 512 characters or is more than 4 lines // More than 512 characters or is more than 4 lines
if (content.length() > 512 || content.chars().filter(ch -> ch == '\n').count() > 4) { 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<String> 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 // Less than or equal to 32 characters and no new lines
if (content.length() <= 32 && !content.contains("\n")) { if (content.length() <= 32 && !content.contains("\n")) {

@ -38,7 +38,7 @@ public class MessageListener implements EventListener {
.setDescription(new DescriptionBuilder("Message Deleted") .setDescription(new DescriptionBuilder("Message Deleted")
.appendLine("Author: %s".formatted(message.getAuthor().getAsMention()), true) .appendLine("Author: %s".formatted(message.getAuthor().getAsMention()), true)
.appendLine("Channel: %s".formatted(message.getChannel().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())
.build()); .build());
} }
@ -53,8 +53,8 @@ public class MessageListener implements EventListener {
.setDescription(new DescriptionBuilder("Message Edited") .setDescription(new DescriptionBuilder("Message Edited")
.appendLine("Author: %s".formatted(newMessage.getAuthor().getAsMention()), true) .appendLine("Author: %s".formatted(newMessage.getAuthor().getAsMention()), true)
.appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true) .appendLine("Channel: %s".formatted(newMessage.getChannel().getAsMention()), true)
.appendLine("Old Content: %s".formatted(logFeature.formatContent(oldMessage.getContent())), true) .appendLine("Old Content: %s".formatted(logFeature.formatContent(oldMessage)), true)
.appendLine("New Content: %s".formatted(logFeature.formatContent(newMessage.getContent())), true) .appendLine("New Content: %s".formatted(logFeature.formatContent(newMessage)), true)
.appendLine("*[Jump to Message](%s)*".formatted(newMessage.getMessageUrl()), false) .appendLine("*[Jump to Message](%s)*".formatted(newMessage.getMessageUrl()), false)
.build()) .build())
.build()); .build());