diff --git a/src/main/java/com/softawii/capivara/config/SpringConfig.java b/src/main/java/com/softawii/capivara/config/SpringConfig.java index 21b60f1..ca163d3 100644 --- a/src/main/java/com/softawii/capivara/config/SpringConfig.java +++ b/src/main/java/com/softawii/capivara/config/SpringConfig.java @@ -62,15 +62,12 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory() { } @Bean - public JDA jda(VoiceEvents voiceEvents) { + public JDA jda() { JDA jda; try { JDABuilder builder = JDABuilder.create(discordToken, GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_VOICE_STATES, GatewayIntent.GUILD_EMOJIS_AND_STICKERS, GatewayIntent.GUILD_PRESENCES); builder.setMemberCachePolicy(MemberCachePolicy.ALL); builder.enableCache(CacheFlag.EMOJI, CacheFlag.ROLE_TAGS, CacheFlag.MEMBER_OVERRIDES, CacheFlag.STICKER); - builder.addEventListeners( - voiceEvents - ); jda = builder.build(); jda.awaitReady(); } catch (InterruptedException e) { diff --git a/src/main/java/com/softawii/capivara/core/DroneManager.java b/src/main/java/com/softawii/capivara/core/DroneManager.java index db89d80..dc3cb5e 100644 --- a/src/main/java/com/softawii/capivara/core/DroneManager.java +++ b/src/main/java/com/softawii/capivara/core/DroneManager.java @@ -12,6 +12,7 @@ import com.softawii.capivara.utils.Utils; import com.softawii.curupira.exceptions.MissingPermissionsException; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.ChannelType; @@ -45,6 +46,7 @@ public class DroneManager { private final Logger LOGGER = LogManager.getLogger(VoiceManager.class); private final VoiceDroneService voiceDroneService; private final VoiceHiveService voiceHiveService; + private final JDA jda; private final String renameDrone = "drone-manager-rename"; private final String limitDrone = "drone-manager-limit"; @@ -53,10 +55,14 @@ public class DroneManager { private final Pattern digdinRegex; - public DroneManager(VoiceDroneService voiceDroneService, VoiceHiveService voiceHiveService) { + public DroneManager(JDA jda, VoiceDroneService voiceDroneService, VoiceHiveService voiceHiveService) { this.voiceDroneService = voiceDroneService; this.voiceHiveService = voiceHiveService; + this.jda = jda; this.digdinRegex = Pattern.compile("(?\\w+mente).+(Digdin)"); + + // Checking if some channel is pending + checkEmptyDrones(); } public boolean canInteract(VoiceChannel channel, Member member) throws KeyNotFoundException { @@ -205,7 +211,12 @@ public void checkToDeleteTemporary(VoiceChannel channel, Member member, boolean int online = channel.getMembers().size(); TextChannel textChannel = channel.getGuild().getTextChannelById(drone.getChatId()); - if (online == 0) { + if (wasDeleted) { + voiceDroneService.destroy(snowflakeId); + if (textChannel != null) { + textChannel.delete().submit(); + } + } else if (online == 0) { voiceDroneService.destroy(snowflakeId); if (textChannel != null) { @@ -213,7 +224,7 @@ public void checkToDeleteTemporary(VoiceChannel channel, Member member, boolean } else { channel.delete().submit(); } - } else if (member != null && member.getIdLong() == drone.getOwnerId()) { + } else if (member == null || member.getIdLong() == drone.getOwnerId()) { // Election Mode! MessageEmbed embed = claimChat(); Button claim = Button.success(VoiceGroup.Dynamic.droneClaim, "Claim"); @@ -501,4 +512,29 @@ public void claimDrone(Guild guild, MessageChannelUnion channel, Member member) voiceDroneService.update(drone); createControlPanel(voiceChannel); } + + public void checkEmptyDrones() { + LOGGER.debug("Checking current hives and drones..."); + // Checking if some channel needs to be removed + this.voiceDroneService.findAll().forEach(drone -> { + VoiceChannel channel = this.jda.getVoiceChannelById(drone.getChannelId()); + TextChannel text = null; + + if(drone.getChatId() != 0L) text = this.jda.getTextChannelById(drone.getChatId()); + + if (channel == null) { + try { + LOGGER.info("Removing drone from DB: {}", drone.getChannelId()); + voiceDroneService.destroy(drone.getChannelId()); + if(text != null) text.delete().submit(); + } catch (KeyNotFoundException e) { + // Ok + } + } else { + Optional ownerOpt = channel.getMembers().stream().filter(member -> member.getIdLong() == drone.getOwnerId()).findFirst(); + LOGGER.info("Checking to remove drone: {}", drone.getChannelId()); + if(ownerOpt.isEmpty()) this.checkToDeleteTemporary(channel, null, false); + } + }); + } } diff --git a/src/main/java/com/softawii/capivara/core/VoiceManager.java b/src/main/java/com/softawii/capivara/core/VoiceManager.java index e439eb0..b2f768b 100644 --- a/src/main/java/com/softawii/capivara/core/VoiceManager.java +++ b/src/main/java/com/softawii/capivara/core/VoiceManager.java @@ -5,6 +5,7 @@ import com.softawii.capivara.exceptions.KeyNotFoundException; import com.softawii.capivara.services.VoiceHiveService; import com.softawii.capivara.utils.Utils; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.channel.concrete.Category; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; @@ -30,10 +31,12 @@ public class VoiceManager { public static final String configModal_fieldStreaming = "set-streaming"; public static final String configModal_createText = "set-text"; private final VoiceHiveService voiceHiveService; + private final JDA jda; private final Logger LOGGER = LogManager.getLogger(VoiceManager.class); - public VoiceManager(VoiceHiveService voiceHiveService) { + public VoiceManager(JDA jda, VoiceHiveService voiceHiveService) { this.voiceHiveService = voiceHiveService; + this.jda = jda; } public boolean isDynamicCategory(Category category) { @@ -126,4 +129,29 @@ else if (mapping.getId().equals(configModal_createText)) return voiceHive; } + + public void checkRemovedHives() { + this.voiceHiveService.findAll().forEach(voiceHive -> { + Category category = this.jda.getCategoryById(voiceHive.getCategoryId()); + if (category == null) { + try { + LOGGER.info("Deleting removed hive 1: {}", voiceHive.getCategoryId()); + voiceHiveService.destroy(voiceHive.getCategoryId()); + } catch (KeyNotFoundException e) { + LOGGER.debug("Key not found, ignoring..."); + } + } else { + VoiceChannel channel = this.jda.getVoiceChannelById(voiceHive.getVoiceId()); + + if(channel == null || channel.getParentCategoryIdLong() != voiceHive.getCategoryId()) { + try { + LOGGER.info("Deleting removed hive 2: {}", voiceHive.getCategoryId()); + voiceHiveService.destroy(voiceHive.getCategoryId()); + } catch (KeyNotFoundException e) { + LOGGER.debug("Key not found, ignoring..."); + } + } + } + }); + } } diff --git a/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java b/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java index ca4465b..c3309a9 100644 --- a/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java +++ b/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java @@ -4,6 +4,7 @@ import com.softawii.capivara.core.VoiceManager; import com.softawii.capivara.entity.VoiceHive; import com.softawii.capivara.exceptions.KeyNotFoundException; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.Category; @@ -27,10 +28,15 @@ public class VoiceEvents extends ListenerAdapter { private final DroneManager droneManager; private final Logger LOGGER = LogManager.getLogger(VoiceEvents.class); + private final JDA jda; - public VoiceEvents(VoiceManager voiceManager, DroneManager droneManager) { + public VoiceEvents(JDA jda, VoiceManager voiceManager, DroneManager droneManager) { this.voiceManager = voiceManager; this.droneManager = droneManager; + this.jda = jda; + this.jda.addEventListener(this); + this.droneManager.checkEmptyDrones(); + this.voiceManager.checkRemovedHives(); } //region Voice Events diff --git a/src/main/java/com/softawii/capivara/repository/VoiceDroneRepository.java b/src/main/java/com/softawii/capivara/repository/VoiceDroneRepository.java index 07fbbd4..328fb19 100644 --- a/src/main/java/com/softawii/capivara/repository/VoiceDroneRepository.java +++ b/src/main/java/com/softawii/capivara/repository/VoiceDroneRepository.java @@ -3,6 +3,8 @@ import com.softawii.capivara.entity.VoiceDrone; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface VoiceDroneRepository extends JpaRepository { VoiceDrone findByChatId(Long chatId); diff --git a/src/main/java/com/softawii/capivara/services/VoiceDroneService.java b/src/main/java/com/softawii/capivara/services/VoiceDroneService.java index 10aee7a..1234099 100644 --- a/src/main/java/com/softawii/capivara/services/VoiceDroneService.java +++ b/src/main/java/com/softawii/capivara/services/VoiceDroneService.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service; import javax.management.openmbean.KeyAlreadyExistsException; +import java.util.List; @Service public class VoiceDroneService { @@ -42,4 +43,8 @@ public VoiceDrone find(Long snowflakeId) throws KeyNotFoundException { public VoiceDrone findByChatId(Long snowflakeId) throws KeyNotFoundException { return voiceDroneRepository.findByChatId(snowflakeId); } + + public List findAll() { + return voiceDroneRepository.findAll(); + } } diff --git a/src/main/java/com/softawii/capivara/services/VoiceHiveService.java b/src/main/java/com/softawii/capivara/services/VoiceHiveService.java index 44dca5e..80489cd 100644 --- a/src/main/java/com/softawii/capivara/services/VoiceHiveService.java +++ b/src/main/java/com/softawii/capivara/services/VoiceHiveService.java @@ -48,4 +48,8 @@ public VoiceHive find(Long SnowflakeId) throws KeyNotFoundException { Optional voiceHive = voiceHiveRepository.findById(SnowflakeId); return voiceHive.orElseThrow(KeyNotFoundException::new); } + + public List findAll() { + return voiceHiveRepository.findAll(); + } }