Bat/src/main/java/cc/fascinated/bat/service/GuildService.java

119 lines
3.9 KiB
Java
Raw Normal View History

2024-06-23 20:37:24 +00:00
package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils;
2024-07-01 00:33:52 +00:00
import cc.fascinated.bat.event.EventListener;
2024-06-25 10:55:26 +00:00
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.premium.PremiumProfile;
import com.mongodb.client.model.Filters;
import lombok.Getter;
2024-06-23 20:37:24 +00:00
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
2024-06-23 20:37:24 +00:00
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
2024-06-23 20:37:24 +00:00
import org.springframework.beans.factory.annotation.Autowired;
2024-06-24 12:56:01 +00:00
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Scheduled;
2024-06-23 20:37:24 +00:00
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
2024-06-27 22:51:40 +00:00
import java.util.concurrent.TimeUnit;
2024-06-23 20:37:24 +00:00
/**
* @author Fascinated (fascinated7)
*/
2024-06-28 02:01:21 +00:00
@Service
@Log4j2(topic = "Guild Service")
2024-06-28 02:01:21 +00:00
@Getter
@DependsOn({"discordService", "mongoService"})
2024-07-01 00:33:52 +00:00
public class GuildService extends ListenerAdapter implements EventListener {
private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5);
2024-06-23 20:37:24 +00:00
/**
* The cached guilds
2024-06-23 20:37:24 +00:00
*/
private final Map<String, BatGuild> guilds = new HashMap<>();
2024-06-23 20:37:24 +00:00
@Autowired
public GuildService() {
TimerUtils.scheduleRepeating(() -> {
long before = System.currentTimeMillis();
for (BatGuild guild : guilds.values()) {
guild.save();
}
log.info("Saved {} guilds in {}ms", guilds.size(), System.currentTimeMillis() - before);
}, SAVE_INTERVAL, SAVE_INTERVAL);
2024-06-24 12:56:01 +00:00
DiscordService.JDA.addEventListener(this);
2024-06-23 20:37:24 +00:00
}
@Scheduled(cron = "0 0 0 * * *")
private void validatePremiumStatus() {
for (BatGuild guild : guilds.values()) {
PremiumProfile premium = guild.getPremiumProfile();
if (!premium.hasExpired()) {
return;
}
premium.removePremium();
log.info("Removed premium status from guild \"{}\"", guild.getName());
}
}
2024-06-23 20:37:24 +00:00
/**
* Gets a guild by its ID
*
* @param id The ID of the guild
* @return The guild
*/
2024-06-24 12:56:01 +00:00
public BatGuild getGuild(@NonNull String id) {
long before = System.currentTimeMillis();
// Guild is cached
if (guilds.containsKey(id)) {
return guilds.get(id);
}
2024-07-01 00:41:40 +00:00
if (DiscordService.JDA.getGuildById(id) == null) {
log.warn("Attempted to get guild with ID \"{}\" but it does not exist", id);
return null;
}
// Guild is not cached
Document document = MongoService.INSTANCE.getGuildsCollection().find(Filters.eq("_id", id)).first();
if (document != null) {
BatGuild guild = new BatGuild(id, document);
guilds.put(id, guild);
log.info("Loaded guild \"{}\" in {}ms", guild.getName(),System.currentTimeMillis() - before);
return guild;
2024-06-23 20:37:24 +00:00
}
// New guild
BatGuild guild = new BatGuild(id, new Document());
guilds.put(id, guild);
log.info("Created guild \"{}\" - \"{}\"", guild.getName(), guild.getId());
2024-06-23 20:37:24 +00:00
return guild;
}
@Override
2024-06-24 12:56:01 +00:00
public final void onGuildJoin(GuildJoinEvent event) {
BatGuild guild = getGuild(event.getGuild().getId());
2024-06-28 17:51:58 +00:00
log.info("Joined guild \"{}\"", guild.getName());
}
@Override
public void onGuildLeave(@NotNull GuildLeaveEvent event) {
BatGuild guild = getGuild(event.getGuild().getId());
log.info("Left guild \"{}\"", guild.getName());
guild.save();
guilds.remove(guild.getId());
2024-06-23 20:37:24 +00:00
}
2024-07-01 00:33:52 +00:00
@Override
public void onSpringShutdown() {
log.info("Saving all guilds before shutdown...");
for (BatGuild guild : guilds.values()) {
guild.save();
}
log.info("Saved all guilds.");
}
2024-06-23 20:37:24 +00:00
}