2024-06-23 21:37:24 +01:00
|
|
|
package cc.fascinated.bat.service;
|
|
|
|
|
2024-06-24 17:42:57 +01:00
|
|
|
import cc.fascinated.bat.model.guild.BatGuild;
|
2024-06-23 21:37:24 +01:00
|
|
|
import cc.fascinated.bat.repository.GuildRepository;
|
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
|
|
|
@Service @Log4j2
|
2024-06-24 13:56:01 +01:00
|
|
|
@DependsOn("discordService")
|
2024-06-23 21:37:24 +01:00
|
|
|
public class GuildService extends ListenerAdapter {
|
|
|
|
/**
|
|
|
|
* 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-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-23 21:37:24 +01:00
|
|
|
if (optionalGuild.isPresent()) {
|
|
|
|
return optionalGuild.get();
|
|
|
|
}
|
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-23 21:37:24 +01:00
|
|
|
@Override
|
2024-06-24 13:56:01 +01:00
|
|
|
public final void onGuildJoin(GuildJoinEvent event) {
|
2024-06-23 21:37:24 +01:00
|
|
|
log.info("Joined guild \"{}\"", event.getGuild().getId());
|
|
|
|
getGuild(event.getGuild().getId()); // Ensure the guild is in the database
|
|
|
|
}
|
|
|
|
}
|