From 4f975ab07a81ad7a4f6e592841e377784e82d784 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 2 Jul 2024 01:20:41 +0100 Subject: [PATCH] use paste for messages longer than 512 and fix message sniping --- .../cc/fascinated/bat/common/PasteUtils.java | 39 +++++++++++++++++++ .../cc/fascinated/bat/common/StringUtils.java | 17 ++++++++ .../messagesnipe/MessageSnipeFeature.java | 4 +- .../command/DeletedSubCommand.java | 14 ++++--- .../model/token/paste/PasteUploadToken.java | 22 +++++++++++ 5 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/PasteUtils.java create mode 100644 src/main/java/cc/fascinated/bat/model/token/paste/PasteUploadToken.java diff --git a/src/main/java/cc/fascinated/bat/common/PasteUtils.java b/src/main/java/cc/fascinated/bat/common/PasteUtils.java new file mode 100644 index 0000000..46343b0 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/PasteUtils.java @@ -0,0 +1,39 @@ +package cc.fascinated.bat.common; + +import cc.fascinated.bat.BatApplication; +import cc.fascinated.bat.model.token.paste.PasteUploadToken; +import lombok.SneakyThrows; +import lombok.extern.log4j.Log4j2; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +/** + * @author Fascinated (fascinated7) + */ +@Log4j2 +public class PasteUtils { + private static final String PASTE_URL = "https://paste.fascinated.cc/"; + private static final String PASTE_UPLOAD_URL = PASTE_URL + "api/upload"; + private static final HttpClient httpClient = HttpClient.newHttpClient(); + + /** + * Uploads a paste to the paste server + * + * @param content the content of the paste + * @return the paste upload token + */ + @SneakyThrows + public static PasteUploadToken uploadPaste(String content) { + HttpResponse response = httpClient.send(HttpRequest.newBuilder() + .uri(URI.create(PASTE_UPLOAD_URL)) + .POST(HttpRequest.BodyPublishers.ofString(content)) + .build(), HttpResponse.BodyHandlers.ofString()); + PasteUploadToken paste = BatApplication.GSON.fromJson(response.body(), PasteUploadToken.class); + paste.setUrl(PASTE_URL + paste.getKey()); + log.info("Created paste with key \"{}\" ({})", paste.getKey(), paste.getUrl()); + return paste; + } +} diff --git a/src/main/java/cc/fascinated/bat/common/StringUtils.java b/src/main/java/cc/fascinated/bat/common/StringUtils.java index ad3af10..2171e90 100644 --- a/src/main/java/cc/fascinated/bat/common/StringUtils.java +++ b/src/main/java/cc/fascinated/bat/common/StringUtils.java @@ -17,4 +17,21 @@ public class StringUtils { } return stringBuilder.toString(); } + + /** + * Escapes meta characters in a string + * + * @param inputString the input string + * @return the string with escaped meta characters + */ + public static String escapeMetaCharacters(String inputString){ + final String[] metaCharacters = {"\\","^","$","{","}","[","]","(",")",".","*","+","?","|","<",">","-","&","%"}; + + for (String metaCharacter : metaCharacters) { + if (inputString.contains(metaCharacter)) { + inputString = inputString.replace(metaCharacter, "\\" + metaCharacter); + } + } + return inputString; + } } diff --git a/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java b/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java index 9606b9f..4623b36 100644 --- a/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java +++ b/src/main/java/cc/fascinated/bat/features/messagesnipe/MessageSnipeFeature.java @@ -56,8 +56,8 @@ public class MessageSnipeFeature extends Feature implements EventListener { * @return the sniped messages for the given guild */ public static SnipedMessage getDeletedMessage(BatGuild guild, String channelId) { - List messages = snipedMessages.getOrDefault(guild, new ArrayList<>()); - messages.sort(Comparator.comparing(SnipedMessage::getDeletedDate)); + List messages = snipedMessages.getOrDefault(guild, new ArrayList<>()).stream().filter(message -> message.getDeletedDate() != null) + .sorted(Comparator.comparing(SnipedMessage::getDeletedDate).reversed()).toList(); for (SnipedMessage message : messages) { if (message.getDeletedDate() != null // Check if the message was deleted && message.getMessage().getChannel().getId().equals(channelId)) { diff --git a/src/main/java/cc/fascinated/bat/features/messagesnipe/command/DeletedSubCommand.java b/src/main/java/cc/fascinated/bat/features/messagesnipe/command/DeletedSubCommand.java index 1e38af6..a419d53 100644 --- a/src/main/java/cc/fascinated/bat/features/messagesnipe/command/DeletedSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/messagesnipe/command/DeletedSubCommand.java @@ -3,6 +3,7 @@ package cc.fascinated.bat.features.messagesnipe.command; import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.CommandInfo; import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.common.PasteUtils; import cc.fascinated.bat.features.messagesnipe.MessageSnipeFeature; import cc.fascinated.bat.features.messagesnipe.SnipedMessage; import cc.fascinated.bat.model.BatGuild; @@ -31,20 +32,23 @@ public class DeletedSubCommand extends BatSubCommand { } User author = message.getMessage().getAuthor(); + String content = message.getMessage().getContentDisplay(); event.replyEmbeds(EmbedUtils.genericEmbed() .setDescription(""" **Deleted Message Snipe** ➜ Author: **%s** (%s) ➜ Deleted: - ➜ Content: - ``` - %s - ``` + ➜ Content: %s """.formatted( author.getAsMention(), author.getId(), message.getDeletedDate().getTime() / 1000, - message.getMessage().getContentRaw() + content.length() > 512 ? PasteUtils.uploadPaste(content).getUrl() : + """ + ``` + %s + ``` + """.formatted(content) )).build()).queue(); } } diff --git a/src/main/java/cc/fascinated/bat/model/token/paste/PasteUploadToken.java b/src/main/java/cc/fascinated/bat/model/token/paste/PasteUploadToken.java new file mode 100644 index 0000000..ffec9e2 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/model/token/paste/PasteUploadToken.java @@ -0,0 +1,22 @@ +package cc.fascinated.bat.model.token.paste; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +/** + * @author Fascinated (fascinated7) + */ +@AllArgsConstructor +@Getter @Setter +public class PasteUploadToken { + /** + * The key of the paste + */ + private final String key; + + /** + * The url of the paste + */ + private String url; +}