package cc.fascinated.bat.features.logging; import cc.fascinated.bat.common.Serializable; import cc.fascinated.bat.service.DiscordService; import com.google.gson.Gson; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import org.bson.Document; import java.util.HashMap; import java.util.Map; /** * @author Fascinated (fascinated7) */ public class LogProfile extends Serializable { /** * The log channels for this profile */ private final Map logChannels = new HashMap<>(); /** * Checks if the log channel for the specified log type exists * * @param logType - the log type * @return true if it exists, false otherwise */ public boolean hasLogChannel(LogType logType) { return this.logChannels.containsKey(logType); } /** * Gets the log channel for the specified log type * * @param logType - the log type * @return the log channel, or null if it doesn't exist */ public TextChannel getLogChannel(LogType logType) { TextChannel textChannel = this.logChannels.get(logType); if (textChannel == null) { return null; } // Ensure the channel exists if (DiscordService.JDA.getTextChannelById(textChannel.getId()) == null) { this.logChannels.remove(logType); return null; } return textChannel; } /** * Sets the log channel for the specified log type * * @param logType - the log type * @param channel - the channel */ public void setLogChannel(LogType logType, TextChannel channel) { this.logChannels.put(logType, channel); } /** * Removes the log channel for the specified log type * * @param logType - the log type */ public void removeLogChannel(LogType logType) { this.logChannels.remove(logType); } @Override public void load(Document document, Gson gson) { JDA jda = DiscordService.JDA; for (LogType logType : LogType.values()) { if (document.containsKey(logType.name())) { TextChannel channel = jda.getTextChannelById(document.getString(logType.name())); if (channel == null) { return; } this.logChannels.put(logType, channel); } } } @Override public Document serialize(Gson gson) { Document document = new Document(); for (Map.Entry entry : this.logChannels.entrySet()) { document.append(entry.getKey().name(), entry.getValue().getId()); } return document; } @Override public void reset() { this.logChannels.clear(); } }