diff --git a/src/main/java/com/softawii/capivara/core/DroneManager.java b/src/main/java/com/softawii/capivara/core/DroneManager.java index cc94661..871b23d 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; @@ -399,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(); @@ -553,4 +558,37 @@ 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 tryRenameDrone(Member member, AudioChannelUnion channel) { + try { + if(!isUserOwner(member, channel)) return; // not the owner or not a dynamic channel + + // getting drone hive settings + long parentCategoryIdLong = channel.getParentCategoryIdLong(); + VoiceHive hive = voiceHiveService.find(parentCategoryIdLong); + + // renaming the channel + 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..."); + } + } + } 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..7e9e191 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,25 @@ 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 + droneManager.tryRenameDrone(member, channel); + } + private void handleException(Exception exception, Event event) { if (exceptionHandler != null) { exceptionHandler.handle(exception, event);