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

View File

@ -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<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
if (content.length() <= 32 && !content.contains("\n")) {