Skip to content

Commit

Permalink
feat: Checking DB Rows to Remove Empty Rooms in Startup
Browse files Browse the repository at this point in the history
  • Loading branch information
yaansz committed Oct 28, 2023
1 parent b852f98 commit bc7bca1
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/softawii/capivara/config/SpringConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 39 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 Down Expand Up @@ -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";
Expand All @@ -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("(?<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 +211,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 +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<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);
}
});
}
}
30 changes: 29 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 @@ -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) {
Expand Down Expand Up @@ -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...");
}
}
}
});
}
}
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.jda.addEventListener(this);
this.droneManager.checkEmptyDrones();
this.voiceManager.checkRemovedHives();
}

//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 org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface VoiceDroneRepository extends JpaRepository<VoiceDrone, Long> {

VoiceDrone findByChatId(Long chatId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;

import javax.management.openmbean.KeyAlreadyExistsException;
import java.util.List;

@Service
public class VoiceDroneService {
Expand Down Expand Up @@ -42,4 +43,8 @@ public VoiceDrone find(Long snowflakeId) throws KeyNotFoundException {
public VoiceDrone findByChatId(Long snowflakeId) throws KeyNotFoundException {
return voiceDroneRepository.findByChatId(snowflakeId);
}

public List<VoiceDrone> findAll() {
return voiceDroneRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public VoiceHive find(Long SnowflakeId) throws KeyNotFoundException {
Optional<VoiceHive> voiceHive = voiceHiveRepository.findById(SnowflakeId);
return voiceHive.orElseThrow(KeyNotFoundException::new);
}

public List<VoiceHive> findAll() {
return voiceHiveRepository.findAll();
}
}

0 comments on commit bc7bca1

Please sign in to comment.