package cc.fascinated.bat.model; import cc.fascinated.bat.common.ProfileHolder; import cc.fascinated.bat.service.DiscordService; import jakarta.annotation.PostConstruct; import lombok.*; import net.dv8tion.jda.api.entities.Guild; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.stereotype.Component; import java.util.Calendar; import java.util.Date; /** * @author Fascinated (fascinated7) */ @RequiredArgsConstructor @Getter @Setter @Document(collection = "guilds") public class BatGuild extends ProfileHolder { /** * The ID of the guild */ @NonNull @Id private final String id; /** * The time this guild was joined */ private Date createdAt = new Date(); /** * The premium information for the guild */ private Premium premium; /** * The premium information for the guild * * @return the premium information */ public Premium getPremium() { if (this.premium == null) { this.premium = new Premium(null, null, null); } return this.premium; } /** * 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); } @AllArgsConstructor @Getter @Setter public static class Premium { /** * The time the premium was activated */ private Date activatedAt; /** * The time the premium expires */ private Date expiresAt; /** * The type of premium */ private Type type; /** * Checks if the guild has premium * * @return whether the guild has premium */ public boolean hasPremium() { return this.type == Type.INFINITE || (this.expiresAt != null && this.expiresAt.after(new Date())); } /** * Adds a month to the premium time */ public void addTime(int months) { if (this.type == null) { // If the type is null, set it to monthly this.type = Type.MONTHLY; } if (this.expiresAt == null) { this.expiresAt = new Date(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MONTH, months); this.expiresAt = calendar.getTime(); this.type = Type.MONTHLY; } /** * Adds a month to the premium time */ public void addTime() { addTime(1); } /** * Adds infinite time to the premium */ public void addInfiniteTime() { this.type = Type.INFINITE; this.expiresAt = null; this.activatedAt = new Date(); } /** * Removes the premium from the guild */ public void removePremium() { this.activatedAt = null; this.expiresAt = null; this.type = null; } /** * Checks if the premium is infinite * * @return whether the premium is infinite */ public boolean isInfinite() { return this.type == Type.INFINITE; } /** * The premium type for the guild */ public enum Type { INFINITE, MONTHLY } } }