Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/presence enhancement #106

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/com/softawii/capivara/core/DroneManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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...");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down