2024-06-23 21:37:24 +01:00
|
|
|
package cc.fascinated.bat.service;
|
|
|
|
|
2024-06-25 11:55:26 +01:00
|
|
|
import cc.fascinated.bat.model.BatGuild;
|
2024-06-23 21:37:24 +01:00
|
|
|
import cc.fascinated.bat.repository.GuildRepository;
|
2024-06-27 13:00:45 +01:00
|
|
|
import lombok.Getter;
|
2024-06-23 21:37:24 +01:00
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
2024-06-27 13:00:45 +01:00
|
|
|
import net.dv8tion.jda.api.entities.Guild;
|
2024-06-23 21:37:24 +01:00
|
|
|
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
|
|
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2024-06-24 13:56:01 +01:00
|
|
|
import org.springframework.context.annotation.DependsOn;
|
2024-06-23 21:37:24 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2024-06-27 13:00:45 +01:00
|
|
|
import java.util.*;
|
2024-06-23 21:37:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
2024-06-27 13:00:45 +01:00
|
|
|
@Service @Log4j2 @Getter
|
2024-06-24 13:56:01 +01:00
|
|
|
@DependsOn("discordService")
|
2024-06-23 21:37:24 +01:00
|
|
|
public class GuildService extends ListenerAdapter {
|
2024-06-27 13:00:45 +01:00
|
|
|
/**
|
|
|
|
* The cached guilds
|
|
|
|
*/
|
|
|
|
private final Map<String, BatGuild> guilds = new HashMap<>();
|
|
|
|
|
2024-06-23 21:37:24 +01:00
|
|
|
/**
|
|
|
|
* The guild repository to use
|
|
|
|
*/
|
|
|
|
private final GuildRepository guildRepository;
|
|
|
|
|
|
|
|
@Autowired
|
2024-06-24 13:56:01 +01:00
|
|
|
public GuildService(@NonNull GuildRepository guildRepository) {
|
2024-06-23 21:37:24 +01:00
|
|
|
this.guildRepository = guildRepository;
|
2024-06-24 13:56:01 +01:00
|
|
|
DiscordService.JDA.addEventListener(this);
|
2024-06-23 21:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a guild by its ID
|
|
|
|
*
|
|
|
|
* @param id The ID of the guild
|
|
|
|
* @return The guild
|
|
|
|
*/
|
2024-06-24 13:56:01 +01:00
|
|
|
public BatGuild getGuild(@NonNull String id) {
|
2024-06-27 13:00:45 +01:00
|
|
|
if (guilds.containsKey(id)) {
|
|
|
|
return guilds.get(id);
|
|
|
|
}
|
2024-06-23 21:37:24 +01:00
|
|
|
long start = System.currentTimeMillis();
|
2024-06-24 13:56:01 +01:00
|
|
|
Optional<BatGuild> optionalGuild = guildRepository.findById(id);
|
2024-06-27 13:00:45 +01:00
|
|
|
|
2024-06-23 21:37:24 +01:00
|
|
|
if (optionalGuild.isPresent()) {
|
2024-06-27 13:00:45 +01:00
|
|
|
BatGuild guild = optionalGuild.get();
|
|
|
|
guilds.put(id, guild);
|
|
|
|
return guild;
|
2024-06-23 21:37:24 +01:00
|
|
|
}
|
2024-06-24 13:56:01 +01:00
|
|
|
BatGuild guild = guildRepository.save(new BatGuild(id));
|
2024-06-23 21:37:24 +01:00
|
|
|
log.info("Created guild \"{}\" in {}ms", id, System.currentTimeMillis() - start);
|
|
|
|
return guild;
|
|
|
|
}
|
|
|
|
|
2024-06-24 13:56:01 +01:00
|
|
|
/**
|
|
|
|
* Saves a guild
|
|
|
|
*
|
|
|
|
* @param guild The guild to save
|
|
|
|
*/
|
|
|
|
public void saveGuild(@NonNull BatGuild guild) {
|
|
|
|
guildRepository.save(guild);
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:19:11 +01:00
|
|
|
/**
|
|
|
|
* Gets all guilds
|
|
|
|
*
|
|
|
|
* @return all guilds
|
|
|
|
*/
|
|
|
|
public List<BatGuild> getAllGuilds() {
|
2024-06-27 13:00:45 +01:00
|
|
|
List<BatGuild> guilds = new ArrayList<>();
|
|
|
|
for (Guild guild : DiscordService.JDA.getGuilds()) {
|
|
|
|
guilds.add(getGuild(guild.getId()));
|
|
|
|
}
|
|
|
|
return guilds;
|
2024-06-26 12:19:11 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 21:37:24 +01:00
|
|
|
@Override
|
2024-06-24 13:56:01 +01:00
|
|
|
public final void onGuildJoin(GuildJoinEvent event) {
|
2024-06-27 13:11:58 +01:00
|
|
|
Guild guild = event.getGuild();
|
|
|
|
log.info("Joined guild \"{}\"", guild.getId());
|
|
|
|
getGuild(guild.getId()); // Ensure the guild is in the database
|
2024-06-23 21:37:24 +01:00
|
|
|
}
|
|
|
|
}
|