Bat/src/main/java/cc/fascinated/bat/model/BatGuild.java

156 lines
3.6 KiB
Java
Raw Normal View History

2024-06-25 10:55:26 +00:00
package cc.fascinated.bat.model;
2024-06-25 11:36:40 +00:00
import cc.fascinated.bat.common.ProfileHolder;
import cc.fascinated.bat.service.DiscordService;
import lombok.*;
import net.dv8tion.jda.api.entities.Guild;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Calendar;
2024-06-25 14:43:36 +00:00
import java.util.Date;
/**
* @author Fascinated (fascinated7)
*/
@RequiredArgsConstructor
2024-06-28 02:01:21 +00:00
@Getter
@Setter
@Document(collection = "guilds")
2024-06-27 12:02:55 +00:00
public class BatGuild extends ProfileHolder {
/**
* The ID of the guild
*/
2024-06-28 02:01:21 +00:00
@NonNull
@Id
private final String id;
2024-06-25 14:43:36 +00:00
/**
* 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);
}
2024-06-28 02:01:21 +00:00
@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
}
}
}