use paste for messages longer than 512 and fix message sniping
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m10s

This commit is contained in:
Lee 2024-07-02 01:20:41 +01:00
parent 1a69bce9dd
commit 4f975ab07a
5 changed files with 89 additions and 7 deletions

View File

@ -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<String> 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;
}
}

View File

@ -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;
}
}

View File

@ -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<SnipedMessage> messages = snipedMessages.getOrDefault(guild, new ArrayList<>());
messages.sort(Comparator.comparing(SnipedMessage::getDeletedDate));
List<SnipedMessage> 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)) {

View File

@ -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: <t:%d:R>
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();
}
}

View File

@ -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;
}