package cc.fascinated.bat.features.logging; 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.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); 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) { if (content == null) { return "No content"; } 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()+")*"; } // 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); } }