Skip to content

Commit

Permalink
Merge pull request #88 from Softawii/feature/remove-empty-channels-start
Browse files Browse the repository at this point in the history
feat: Checking DB Rows to Remove Empty Rooms in Startup
  • Loading branch information
yaansz committed Oct 28, 2023
2 parents b852f98 + caecc80 commit 0c334a3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 11 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ dependencies {

implementation("com.github.Softawii:curupira:v0.3.0")
implementation("com.github.Softawii:curupira:v0.3.0:all")
implementation("com.github.minndevelopment:emoji-java:master-SNAPSHOT")
}

tasks.register('deploy') {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/softawii/capivara/config/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.softawii.capivara.config;

import com.softawii.capivara.listeners.events.VoiceEvents;
import com.softawii.capivara.utils.CapivaraExceptionHandler;
import com.softawii.curupira.core.Curupira;
import net.dv8tion.jda.api.JDA;
Expand Down Expand Up @@ -62,15 +61,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) {
Expand Down
55 changes: 52 additions & 3 deletions src/main/java/com/softawii/capivara/core/DroneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,9 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.awt.*;
Expand All @@ -45,6 +49,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";
Expand All @@ -53,10 +58,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("(?<nome>\\w+mente).+(Digdin)");

// Checking if some channel is pending
checkEmptyDrones();
}

public boolean canInteract(VoiceChannel channel, Member member) throws KeyNotFoundException {
Expand Down Expand Up @@ -205,15 +214,20 @@ 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) {
textChannel.delete().and(channel.delete()).submit();
} 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");
Expand Down Expand Up @@ -501,4 +515,39 @@ public void claimDrone(Guild guild, MessageChannelUnion channel, Member member)
voiceDroneService.update(drone);
createControlPanel(voiceChannel);
}

public void checkEmptyDrones() {
LOGGER.debug("Checking current hives and drones...");
Pageable request = PageRequest.of(0, 100);
Page<VoiceDrone> page = this.voiceDroneService.findAll(request);

while (page.hasContent()) {
LOGGER.debug("Checking page: {}", request.getPageNumber());
page.forEach(this::checkIfDroneIsValid);
request = request.next();
page = this.voiceDroneService.findAll(request);
}
}

private void checkIfDroneIsValid(VoiceDrone drone) {
LOGGER.debug("Checking drone: {}", drone.getChannelId());
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) {
LOGGER.info("Key not found, ignoring...");
}
} else {
Optional<Member> 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);
}
}
}
44 changes: 43 additions & 1 deletion src/main/java/com/softawii/capivara/core/VoiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,6 +15,9 @@
import net.dv8tion.jda.api.interactions.modals.ModalMapping;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.List;
Expand All @@ -30,10 +34,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) {
Expand Down Expand Up @@ -126,4 +132,40 @@ else if (mapping.getId().equals(configModal_createText))

return voiceHive;
}

public void checkRemovedHives() {
Pageable request = PageRequest.of(0, 100);
Page<VoiceHive> page = this.voiceHiveService.findAll(request);

while (page.hasContent()) {
LOGGER.debug("Checking page: {}", request.getPageNumber());
page.forEach(this::checkIfHiveIsStillValid);
request = request.next();
page = this.voiceHiveService.findAll(request);
}
}

public void checkIfHiveIsStillValid(VoiceHive voiceHive) {
LOGGER.info("Checking if hive is still valid: {}", voiceHive.getCategoryId());
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...");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.droneManager.checkEmptyDrones();
this.voiceManager.checkRemovedHives();
this.jda.addEventListener(this);
}

//region Voice Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.softawii.capivara.entity.VoiceDrone;
import com.softawii.capivara.exceptions.KeyNotFoundException;
import com.softawii.capivara.repository.VoiceDroneRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.management.openmbean.KeyAlreadyExistsException;
Expand Down Expand Up @@ -42,4 +44,8 @@ public VoiceDrone find(Long snowflakeId) throws KeyNotFoundException {
public VoiceDrone findByChatId(Long snowflakeId) throws KeyNotFoundException {
return voiceDroneRepository.findByChatId(snowflakeId);
}

public Page<VoiceDrone> findAll(Pageable request) {
return voiceDroneRepository.findAll(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.softawii.capivara.exceptions.ExistingDynamicCategoryException;
import com.softawii.capivara.exceptions.KeyNotFoundException;
import com.softawii.capivara.repository.VoiceHiveRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand Down Expand Up @@ -48,4 +50,8 @@ public VoiceHive find(Long SnowflakeId) throws KeyNotFoundException {
Optional<VoiceHive> voiceHive = voiceHiveRepository.findById(SnowflakeId);
return voiceHive.orElseThrow(KeyNotFoundException::new);
}

public Page<VoiceHive> findAll(Pageable request) {
return voiceHiveRepository.findAll(request);
}
}

0 comments on commit 0c334a3

Please sign in to comment.