97 lines
3.7 KiB
Java
97 lines
3.7 KiB
Java
package cc.fascinated.bat.logging;
|
|
|
|
import cc.fascinated.bat.common.DateUtils;
|
|
import cc.fascinated.bat.common.PasteUtils;
|
|
import cc.fascinated.bat.common.feature.Feature;
|
|
import cc.fascinated.bat.common.feature.FeatureProfile;
|
|
import cc.fascinated.bat.logging.command.LogsCommand;
|
|
import cc.fascinated.bat.common.model.BatGuild;
|
|
import cc.fascinated.bat.common.model.DiscordMessage;
|
|
import cc.fascinated.bat.service.OldCommandService;
|
|
import lombok.NonNull;
|
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
|
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
|
import org.jetbrains.annotations.NotNull;
|
|
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)
|
|
*/
|
|
@Component
|
|
public class LogFeature extends Feature {
|
|
public static LogFeature INSTANCE;
|
|
|
|
@Autowired
|
|
public LogFeature(@NonNull ApplicationContext context, @NonNull OldCommandService commandService) {
|
|
super("Logging", FeatureProfile.FeatureState.DISABLED, true);
|
|
INSTANCE = this;
|
|
|
|
super.registerCommand(commandService, context.getBean(LogsCommand.class));
|
|
}
|
|
|
|
/**
|
|
* Sends a log to the log channel
|
|
*
|
|
* @param guild the guild to send the log in
|
|
* @param type the type of log
|
|
* @param embed the embed to send
|
|
*/
|
|
public void sendLog(@NotNull BatGuild guild, LogType type, MessageEmbed embed) {
|
|
FeatureProfile featureProfile = guild.getFeatureProfile();
|
|
if (featureProfile.isFeatureDisabled(this)) { // The feature is disabled
|
|
return;
|
|
}
|
|
LogProfile logProfile = guild.getLogProfile();
|
|
if (!logProfile.hasLogChannel(type)) { // The guild has no log channel for this type
|
|
return;
|
|
}
|
|
TextChannel logChannel = logProfile.getLogChannel(type);
|
|
if (logChannel == null) { // The log channel has been removed
|
|
return;
|
|
}
|
|
logChannel.sendMessageEmbeds(embed).queue();
|
|
}
|
|
|
|
/**
|
|
* Formats the message content to be sent in the log
|
|
*
|
|
* @param message the message to format
|
|
* @return the formatted content
|
|
*/
|
|
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) {
|
|
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")) {
|
|
return "`%s`".formatted(content);
|
|
}
|
|
// Everything else
|
|
return "\n```\n%s\n```".formatted(content);
|
|
}
|
|
}
|