package cc.fascinated.bat.model; import cc.fascinated.bat.BatApplication; import cc.fascinated.bat.common.ProfileHolder; import cc.fascinated.bat.common.Serializable; import cc.fascinated.bat.features.base.profile.FeatureProfile; import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; import cc.fascinated.bat.features.namehistory.profile.guild.NameHistoryProfile; import cc.fascinated.bat.premium.PremiumProfile; import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.MongoService; import com.mongodb.client.model.ReplaceOptions; import lombok.Getter; import lombok.NonNull; import lombok.Setter; import net.dv8tion.jda.api.entities.Guild; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author Fascinated (fascinated7) */ @Getter @Setter @Document(collection = "guilds") public class BatGuild extends ProfileHolder { private static final Logger log = LoggerFactory.getLogger(BatGuild.class); /** * The document that belongs to this guild */ private final org.bson.Document document; /** * The ID of the guild */ @NonNull @Id private final String id; /** * The time this guild was joined */ private Date createdAt; public BatGuild(@NonNull String id, @NonNull org.bson.Document document) { this.id = id; this.document = document; boolean newAccount = this.document.isEmpty(); this.createdAt = newAccount ? new Date() : document.getDate("createdAt"); } /** * Gets the name of the guild * * @return the name */ public String getName() { return getDiscordGuild().getName(); } /** * Gets the guild as the JDA Guild * * @return the guild */ public Guild getDiscordGuild() { return DiscordService.JDA.getGuildById(id); } /** * Gets the user's name history profile * * @return the user's name history profile */ public NameHistoryProfile getNameHistoryProfile() { return getProfile(NameHistoryProfile.class); } /** * Gets the feature profile * * @return the feature profile */ public FeatureProfile getFeatureProfile() { return getProfile(FeatureProfile.class); } /** * Gets the premium profile * * @return the premium profile */ public PremiumProfile getPremiumProfile() { return getProfile(PremiumProfile.class); } /** * Gets the birthday profile * * @return the birthday profile */ public BirthdayProfile getBirthdayProfile() { return getProfile(BirthdayProfile.class); } /** * Saves the user */ public void save() { document.put("_id", id); document.put("createdAt", createdAt); Map profileDocuments = new HashMap<>(); for (Serializable profile : getProfiles().values()) { profileDocuments.put(profile.getClass().getSimpleName(), profile.serialize(BatApplication.GSON)); } document.put("profiles", profileDocuments); MongoService.INSTANCE.getGuildsCollection().replaceOne( new org.bson.Document("_id", id), this.getDocument(), new ReplaceOptions().upsert(true) ); } @Override public T getProfile(Class clazz) { return getProfileFromDocument(clazz, document); } }