From c433d3d2d0579ea5fd00d2587e0f3e278713b3bb Mon Sep 17 00:00:00 2001 From: FerroEduardo <47820549+FerroEduardo@users.noreply.github.com> Date: Sun, 11 Aug 2024 00:06:59 -0300 Subject: [PATCH 1/3] yes --- .../softawii/capivara/core/DroneManager.java | 26 +++++++++++++++++++ .../listeners/events/VoiceEvents.java | 25 ++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/main/java/com/softawii/capivara/core/DroneManager.java b/src/main/java/com/softawii/capivara/core/DroneManager.java index cc94661..c2ca631 100644 --- a/src/main/java/com/softawii/capivara/core/DroneManager.java +++ b/src/main/java/com/softawii/capivara/core/DroneManager.java @@ -553,4 +553,30 @@ private void checkIfDroneIsValid(VoiceDrone drone) { if(ownerOpt.isEmpty()) this.checkToDeleteTemporary(channel, null, false); } } + + public boolean isUserOwner(Member member, AudioChannelUnion channel) { + try { + VoiceDrone voiceDrone = this.voiceDroneService.find(channel.getIdLong()); + return voiceDrone.getOwnerId().equals(member.getIdLong()); + } catch (KeyNotFoundException e) { + return false; + } + } + + public void renameDrone(Member member, AudioChannelUnion channel) { + long snowflakeId = channel.getParentCategoryIdLong(); + + try { + if(!isUserOwner(member, channel)) return; // not the owner or not a dynamic channel + + // Checking if the current category is a dynamic category + VoiceDrone drone = voiceDroneService.find(snowflakeId); + String name = getDroneName(member, drone) + + + } 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 c4c8456..0a1745c 100644 --- a/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java +++ b/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java @@ -6,17 +6,20 @@ import com.softawii.capivara.exceptions.KeyNotFoundException; import com.softawii.capivara.utils.CapivaraExceptionHandler; import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.GuildVoiceState; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.concrete.Category; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; +import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion; import net.dv8tion.jda.api.events.Event; import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; import net.dv8tion.jda.api.events.channel.update.ChannelUpdateParentEvent; import net.dv8tion.jda.api.events.channel.update.GenericChannelUpdateEvent; import net.dv8tion.jda.api.events.guild.override.GenericPermissionOverrideEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; +import net.dv8tion.jda.api.events.user.update.UserUpdateActivitiesEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -190,6 +193,28 @@ public void onGenericChannelUpdate(@NotNull GenericChannelUpdateEvent event) } //endregion + + @Override + public void onUserUpdateActivities(UserUpdateActivitiesEvent event) { + // 1. Check if the user is in a voice channel + Member member = event.getMember(); + + // 2. Check if user is the owner of the voice channel + GuildVoiceState state = member.getVoiceState(); + + if (state == null || !state.inAudioChannel()) return; + + AudioChannelUnion channel = state.getChannel(); + + if(channel == null) return; + + // 3. Rename the voice channel + + if(droneManager.isUserOwner(member, channel)) { + // String name = droneManager.get + } + } + private void handleException(Exception exception, Event event) { if (exceptionHandler != null) { exceptionHandler.handle(exception, event); From 72668d4d7d5bf8b3f79cce3b64f4b2c54f92436d Mon Sep 17 00:00:00 2001 From: yaansz Date: Sun, 11 Aug 2024 00:36:49 -0300 Subject: [PATCH 2/3] feat: applying new name to discord channel --- .../softawii/capivara/core/DroneManager.java | 17 +++++++++++------ .../capivara/listeners/events/VoiceEvents.java | 5 +---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/softawii/capivara/core/DroneManager.java b/src/main/java/com/softawii/capivara/core/DroneManager.java index c2ca631..4b4fa33 100644 --- a/src/main/java/com/softawii/capivara/core/DroneManager.java +++ b/src/main/java/com/softawii/capivara/core/DroneManager.java @@ -20,6 +20,7 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; +import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion; import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.interactions.components.ActionRow; @@ -563,17 +564,21 @@ public boolean isUserOwner(Member member, AudioChannelUnion channel) { } } - public void renameDrone(Member member, AudioChannelUnion channel) { - long snowflakeId = channel.getParentCategoryIdLong(); - + public void tryRenameDrone(Member member, AudioChannelUnion channel) { try { if(!isUserOwner(member, channel)) return; // not the owner or not a dynamic channel - // Checking if the current category is a dynamic category - VoiceDrone drone = voiceDroneService.find(snowflakeId); - String name = getDroneName(member, drone) + // getting drone hive settings + long parentCategoryIdLong = channel.getParentCategoryIdLong(); + VoiceHive hive = voiceHiveService.find(parentCategoryIdLong); + String name = getDroneName(member, hive); + + LOGGER.info("Renaming channel: {}, New: {}", channel.getIdLong(), name); + // TODO: Check if the name is different + // renaming the channel + channel.getManager().setName(name).queue(); } 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 0a1745c..7e9e191 100644 --- a/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java +++ b/src/main/java/com/softawii/capivara/listeners/events/VoiceEvents.java @@ -209,10 +209,7 @@ public void onUserUpdateActivities(UserUpdateActivitiesEvent event) { if(channel == null) return; // 3. Rename the voice channel - - if(droneManager.isUserOwner(member, channel)) { - // String name = droneManager.get - } + droneManager.tryRenameDrone(member, channel); } private void handleException(Exception exception, Event event) { From 0b5b841e2f8cbdb7ba24405303fa44ea5d510318 Mon Sep 17 00:00:00 2001 From: yaansz Date: Sun, 11 Aug 2024 00:39:33 -0300 Subject: [PATCH 3/3] feat: checking valid activities in user b4 apply name change --- .../softawii/capivara/core/DroneManager.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/softawii/capivara/core/DroneManager.java b/src/main/java/com/softawii/capivara/core/DroneManager.java index 4b4fa33..871b23d 100644 --- a/src/main/java/com/softawii/capivara/core/DroneManager.java +++ b/src/main/java/com/softawii/capivara/core/DroneManager.java @@ -400,6 +400,10 @@ public void makePermanent(VoiceChannel channel, boolean permanent) throws KeyNot voiceDroneService.update(drone); } + public boolean hasValidActivities(Member member) { + return member.getActivities().stream().anyMatch(activity -> activity.getType() == Activity.ActivityType.STREAMING || activity.getType() == Activity.ActivityType.PLAYING); + } + private String getDroneName(Member member, VoiceHive hive) { String droneName = VoiceManager.configModal_idle; String username = member.getNickname() == null ? member.getEffectiveName() : member.getNickname(); @@ -571,14 +575,17 @@ public void tryRenameDrone(Member member, AudioChannelUnion channel) { // getting drone hive settings long parentCategoryIdLong = channel.getParentCategoryIdLong(); VoiceHive hive = voiceHiveService.find(parentCategoryIdLong); - String name = getDroneName(member, hive); - - LOGGER.info("Renaming channel: {}, New: {}", channel.getIdLong(), name); - - // TODO: Check if the name is different // renaming the channel - channel.getManager().setName(name).queue(); + if(hasValidActivities(member)) { + String name = getDroneName(member, hive); + if(name.equals(channel.getName())) return; // no need to rename + + LOGGER.info("Renaming channel: {}, New: {}", channel.getIdLong(), name); + channel.getManager().setName(name).queue(); + } else { + LOGGER.debug("User has no valid activities, ignoring... {}", member.getIdLong()); + } } catch (KeyNotFoundException e) { LOGGER.debug("Key not found, ignoring..."); }