Files
Bat/src/main/java/cc/fascinated/bat/features/logging/LogFeature.java

73 lines
2.6 KiB
Java
Raw Normal View History

2024-07-02 18:21:24 +01:00
package cc.fascinated.bat.features.logging;
import cc.fascinated.bat.common.PasteUtils;
import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.FeatureProfile;
2024-07-02 18:21:24 +01:00
import cc.fascinated.bat.features.logging.command.LogsCommand;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.service.CommandService;
import lombok.NonNull;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
* @author Fascinated (fascinated7)
*/
@Component
public class LogFeature extends Feature {
@Autowired
public LogFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) {
super("Logging", FeatureProfile.FeatureState.DISABLED, true);
2024-07-02 18:21:24 +01:00
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(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 content to be sent in the log
*
* @param content the content to format
* @return the formatted content
*/
public String formatContent(String content) {
2024-07-04 07:49:35 +01:00
if (content == null) {
return "No content";
}
2024-07-05 21:03:56 +01:00
// More than 512 characters or is more than 4 lines
if (content.length() > 512 || content.chars().filter(ch -> ch == '\n').count() > 4) {
2024-07-04 07:53:03 +01:00
return "*Content too long, [click here to view]("+PasteUtils.uploadPaste(content).getUrl()+")*";
2024-07-04 07:49:35 +01:00
}
2024-07-05 21:03:56 +01:00
// Less than or equal to 32 characters and no new lines
2024-07-04 17:17:26 +01:00
if (content.length() <= 32 && !content.contains("\n")) {
2024-07-04 07:49:35 +01:00
return "`%s`".formatted(content);
}
2024-07-05 21:03:56 +01:00
// Everything else
2024-07-04 07:49:35 +01:00
return "\n```\n%s\n```".formatted(content);
2024-07-02 18:21:24 +01:00
}
}