2024-06-25 11:55:26 +01:00
|
|
|
package cc.fascinated.bat.model;
|
2024-06-24 17:42:57 +01:00
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.BatApplication;
|
2024-06-25 12:36:40 +01:00
|
|
|
import cc.fascinated.bat.common.ProfileHolder;
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.common.Serializable;
|
2024-07-05 19:33:10 +01:00
|
|
|
import cc.fascinated.bat.features.FeatureProfile;
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.features.birthday.profile.BirthdayProfile;
|
2024-07-05 19:07:32 +01:00
|
|
|
import cc.fascinated.bat.features.counter.CounterProfile;
|
2024-07-06 04:04:38 +01:00
|
|
|
import cc.fascinated.bat.features.leveling.LevelingProfile;
|
2024-07-02 18:21:24 +01:00
|
|
|
import cc.fascinated.bat.features.logging.LogProfile;
|
2024-07-06 06:21:05 +01:00
|
|
|
import cc.fascinated.bat.features.minecraft.MinecraftProfile;
|
2024-07-09 19:46:48 +01:00
|
|
|
import cc.fascinated.bat.features.moderation.punish.PunishmentProfile;
|
2024-06-30 03:36:00 +01:00
|
|
|
import cc.fascinated.bat.features.namehistory.profile.guild.NameHistoryProfile;
|
2024-07-03 00:10:02 +01:00
|
|
|
import cc.fascinated.bat.features.reminder.ReminderProfile;
|
2024-07-07 06:50:25 +01:00
|
|
|
import cc.fascinated.bat.features.statschannel.StatsChannelProfile;
|
2024-07-03 19:49:19 +01:00
|
|
|
import cc.fascinated.bat.features.welcomer.WelcomerProfile;
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.premium.PremiumProfile;
|
2024-06-24 17:42:57 +01:00
|
|
|
import cc.fascinated.bat.service.DiscordService;
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.service.MongoService;
|
|
|
|
import com.mongodb.client.model.ReplaceOptions;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.Setter;
|
2024-06-24 17:42:57 +01:00
|
|
|
import net.dv8tion.jda.api.entities.Guild;
|
2024-07-01 01:12:32 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2024-06-24 17:42:57 +01:00
|
|
|
import org.springframework.data.annotation.Id;
|
|
|
|
|
2024-06-25 15:43:36 +01:00
|
|
|
import java.util.Date;
|
2024-07-01 01:12:32 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2024-06-24 17:42:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
2024-06-28 03:01:21 +01:00
|
|
|
@Getter
|
|
|
|
@Setter
|
2024-06-27 13:02:55 +01:00
|
|
|
public class BatGuild extends ProfileHolder {
|
2024-07-01 01:12:32 +01:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(BatGuild.class);
|
|
|
|
/**
|
|
|
|
* The document that belongs to this guild
|
|
|
|
*/
|
|
|
|
private final org.bson.Document document;
|
|
|
|
|
2024-06-24 17:42:57 +01:00
|
|
|
/**
|
|
|
|
* The ID of the guild
|
|
|
|
*/
|
2024-06-28 03:01:21 +01:00
|
|
|
@NonNull
|
|
|
|
@Id
|
|
|
|
private final String id;
|
2024-06-24 17:42:57 +01:00
|
|
|
|
2024-06-25 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* The time this guild was joined
|
|
|
|
*/
|
2024-07-01 01:12:32 +01:00
|
|
|
private Date createdAt;
|
2024-06-25 15:43:36 +01:00
|
|
|
|
2024-07-03 23:23:23 +01:00
|
|
|
/**
|
|
|
|
* The guild as the JDA Guild
|
|
|
|
*/
|
|
|
|
private Guild guild;
|
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
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");
|
2024-07-03 23:23:23 +01:00
|
|
|
|
|
|
|
Guild guild = DiscordService.JDA.getGuildById(id);
|
|
|
|
if (guild != null) {
|
|
|
|
this.guild = guild;
|
|
|
|
}
|
2024-06-27 21:05:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the name of the guild
|
|
|
|
*
|
|
|
|
* @return the name
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return getDiscordGuild().getName();
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:42:57 +01:00
|
|
|
/**
|
|
|
|
* Gets the guild as the JDA Guild
|
|
|
|
*
|
|
|
|
* @return the guild
|
|
|
|
*/
|
|
|
|
public Guild getDiscordGuild() {
|
2024-07-03 23:23:23 +01:00
|
|
|
if (guild == null) {
|
|
|
|
guild = DiscordService.JDA.getGuildById(id);
|
|
|
|
}
|
|
|
|
return guild;
|
2024-06-24 17:42:57 +01:00
|
|
|
}
|
2024-06-27 21:05:54 +01:00
|
|
|
|
2024-06-30 03:36:00 +01:00
|
|
|
/**
|
|
|
|
* Gets the user's name history profile
|
|
|
|
*
|
|
|
|
* @return the user's name history profile
|
|
|
|
*/
|
|
|
|
public NameHistoryProfile getNameHistoryProfile() {
|
|
|
|
return getProfile(NameHistoryProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-06-30 05:15:37 +01:00
|
|
|
/**
|
|
|
|
* Gets the feature profile
|
|
|
|
*
|
|
|
|
* @return the feature profile
|
|
|
|
*/
|
|
|
|
public FeatureProfile getFeatureProfile() {
|
|
|
|
return getProfile(FeatureProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
/**
|
|
|
|
* Gets the premium profile
|
|
|
|
*
|
|
|
|
* @return the premium profile
|
|
|
|
*/
|
|
|
|
public PremiumProfile getPremiumProfile() {
|
|
|
|
return getProfile(PremiumProfile.class);
|
|
|
|
}
|
2024-06-27 21:05:54 +01:00
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
/**
|
|
|
|
* Gets the birthday profile
|
|
|
|
*
|
|
|
|
* @return the birthday profile
|
|
|
|
*/
|
|
|
|
public BirthdayProfile getBirthdayProfile() {
|
|
|
|
return getProfile(BirthdayProfile.class);
|
|
|
|
}
|
2024-06-27 21:05:54 +01:00
|
|
|
|
2024-07-02 18:21:24 +01:00
|
|
|
/**
|
|
|
|
* Gets the log profile
|
|
|
|
*
|
|
|
|
* @return the log profile
|
|
|
|
*/
|
|
|
|
public LogProfile getLogProfile() {
|
|
|
|
return getProfile(LogProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-03 00:10:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the reminder profile
|
|
|
|
*
|
|
|
|
* @return the reminder profile
|
|
|
|
*/
|
|
|
|
public ReminderProfile getReminderProfile() {
|
|
|
|
return getProfile(ReminderProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-03 19:49:19 +01:00
|
|
|
/**
|
|
|
|
* Gets the welcomer profile
|
|
|
|
*
|
|
|
|
* @return the welcomer profile
|
|
|
|
*/
|
|
|
|
public WelcomerProfile getWelcomerProfile() {
|
|
|
|
return getProfile(WelcomerProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-05 19:07:32 +01:00
|
|
|
/**
|
|
|
|
* Gets the counter profile
|
|
|
|
*
|
|
|
|
* @return the counter profile
|
|
|
|
*/
|
|
|
|
public CounterProfile getCounterProfile() {
|
|
|
|
return getProfile(CounterProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-06 04:04:38 +01:00
|
|
|
/**
|
|
|
|
* Gets the leveling profile
|
|
|
|
*
|
|
|
|
* @return the leveling profile
|
|
|
|
*/
|
|
|
|
public LevelingProfile getLevelingProfile() {
|
|
|
|
return getProfile(LevelingProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-06 06:21:05 +01:00
|
|
|
/**
|
|
|
|
* Gets the minecraft profile
|
|
|
|
*
|
|
|
|
* @return the minecraft profile
|
|
|
|
*/
|
|
|
|
public MinecraftProfile getMinecraftProfile() {
|
|
|
|
return getProfile(MinecraftProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-07 06:50:25 +01:00
|
|
|
/**
|
|
|
|
* Gets the stats channel profile
|
|
|
|
*
|
|
|
|
* @return the stats channel profile
|
|
|
|
*/
|
|
|
|
public StatsChannelProfile getStatsChannelProfile() {
|
|
|
|
return getProfile(StatsChannelProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-09 19:46:48 +01:00
|
|
|
/**
|
|
|
|
* Gets the punishment profile
|
|
|
|
*
|
|
|
|
* @return the punishment profile
|
|
|
|
*/
|
|
|
|
public PunishmentProfile getPunishmentProfile() {
|
|
|
|
return getProfile(PunishmentProfile.class);
|
|
|
|
}
|
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
/**
|
|
|
|
* Saves the user
|
|
|
|
*/
|
|
|
|
public void save() {
|
2024-07-01 21:49:02 +01:00
|
|
|
org.bson.Document document = new org.bson.Document();
|
2024-07-01 01:12:32 +01:00
|
|
|
document.put("_id", id);
|
|
|
|
document.put("createdAt", createdAt);
|
2024-06-27 21:05:54 +01:00
|
|
|
|
2024-07-04 05:54:13 +01:00
|
|
|
Map<String, org.bson.Document> profileDocuments = this.document.get("profiles", new HashMap<>());
|
2024-07-01 01:12:32 +01:00
|
|
|
for (Serializable profile : getProfiles().values()) {
|
|
|
|
profileDocuments.put(profile.getClass().getSimpleName(), profile.serialize(BatApplication.GSON));
|
2024-06-27 21:05:54 +01:00
|
|
|
}
|
2024-07-01 01:12:32 +01:00
|
|
|
document.put("profiles", profileDocuments);
|
2024-06-27 21:05:54 +01:00
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
MongoService.INSTANCE.getGuildsCollection().replaceOne(
|
|
|
|
new org.bson.Document("_id", id),
|
2024-07-01 21:49:02 +01:00
|
|
|
document,
|
2024-07-01 01:12:32 +01:00
|
|
|
new ReplaceOptions().upsert(true)
|
|
|
|
);
|
|
|
|
}
|
2024-06-27 21:05:54 +01:00
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
@Override
|
|
|
|
public <T extends Serializable> T getProfile(Class<T> clazz) {
|
|
|
|
return getProfileFromDocument(clazz, document);
|
2024-06-27 21:05:54 +01:00
|
|
|
}
|
2024-06-24 17:42:57 +01:00
|
|
|
}
|