-
-
Notifications
You must be signed in to change notification settings - Fork 105
feat(DynamicVoiceChat): implement main logic #1370
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
Open
christolis
wants to merge
4
commits into
Together-Java:develop
Choose a base branch
from
christolis:feat/custom-vcs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
41a827f
feat(DynamicVoiceChat): implement main logic
christolis cabd30a
DynamicVoiceChat.java: use trace instead of info
christolis 42d61f6
DynamicVoiceChat.java: more trace instead of info
christolis e93861d
Make class final and add JavaDoc
christolis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
application/src/main/java/org/togetherjava/tjbot/features/voicechat/DynamicVoiceChat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package org.togetherjava.tjbot.features.voicechat; | ||
|
|
||
| import net.dv8tion.jda.api.EmbedBuilder; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||
| import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; | ||
| import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel; | ||
| import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion; | ||
| import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.togetherjava.tjbot.config.Config; | ||
| import org.togetherjava.tjbot.features.VoiceReceiverAdapter; | ||
|
|
||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Handles dynamic voice channel creation and deletion based on user activity. | ||
| * <p> | ||
| * When a member joins a configured root channel, a temporary copy is created and the member is | ||
| * moved into it. Once the channel becomes empty, it is deleted. | ||
| */ | ||
| public final class DynamicVoiceChat extends VoiceReceiverAdapter { | ||
| private static final Logger logger = LoggerFactory.getLogger(DynamicVoiceChat.class); | ||
| private final List<Pattern> dynamicVoiceChannelPatterns; | ||
|
|
||
| public DynamicVoiceChat(Config config) { | ||
| this.dynamicVoiceChannelPatterns = | ||
| config.getDynamicVoiceChannelPatterns().stream().map(Pattern::compile).toList(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) { | ||
| AudioChannelUnion channelJoined = event.getChannelJoined(); | ||
| AudioChannelUnion channelLeft = event.getChannelLeft(); | ||
|
|
||
| if (channelJoined != null && eventHappenOnDynamicRootChannel(channelJoined)) { | ||
| logger.debug("Event happened on joined channel {}", channelJoined); | ||
| createDynamicVoiceChannel(event, channelJoined.asVoiceChannel()); | ||
| } | ||
|
|
||
| if (channelLeft != null && !eventHappenOnDynamicRootChannel(channelLeft)) { | ||
| logger.debug("Event happened on left channel {}", channelLeft); | ||
| deleteDynamicVoiceChannel(channelLeft); | ||
| } | ||
| } | ||
|
|
||
| private boolean eventHappenOnDynamicRootChannel(AudioChannelUnion channel) { | ||
| return dynamicVoiceChannelPatterns.stream() | ||
| .anyMatch(pattern -> pattern.matcher(channel.getName()).matches()); | ||
| } | ||
|
|
||
| private void createDynamicVoiceChannel(@NotNull GuildVoiceUpdateEvent event, | ||
| VoiceChannel channel) { | ||
| Guild guild = event.getGuild(); | ||
| Member member = event.getMember(); | ||
| String newChannelName = "%s's %s".formatted(member.getEffectiveName(), channel.getName()); | ||
|
|
||
| channel.createCopy() | ||
| .setName(newChannelName) | ||
| .setPosition(channel.getPositionRaw()) | ||
| .onSuccess(newChannel -> { | ||
| moveMember(guild, member, newChannel); | ||
| sendWarningEmbed(newChannel); | ||
| }) | ||
| .queue(newChannel -> logger.trace("Successfully created {} voice channel.", | ||
| newChannel.getName()), | ||
| error -> logger.error("Failed to create dynamic voice channel", error)); | ||
| } | ||
|
|
||
| private void moveMember(Guild guild, Member member, AudioChannel channel) { | ||
| guild.moveVoiceMember(member, channel) | ||
| .queue(_ -> logger.trace( | ||
| "Successfully moved {} to newly created dynamic voice channel {}", | ||
| member.getEffectiveName(), channel.getName()), | ||
| error -> logger.error( | ||
| "Failed to move user into dynamically created voice channel {}, {}", | ||
| member.getNickname(), channel.getName(), error)); | ||
| } | ||
|
|
||
| private void deleteDynamicVoiceChannel(AudioChannelUnion channel) { | ||
| int memberCount = channel.getMembers().size(); | ||
|
|
||
| if (memberCount > 0) { | ||
| logger.debug("Voice channel {} not empty ({} members), so not removing.", | ||
| channel.getName(), memberCount); | ||
| return; | ||
| } | ||
|
|
||
| channel.delete() | ||
| .queue(_ -> logger.trace("Deleted dynamically created voice channel: {} ", | ||
| channel.getName()), | ||
| error -> logger.error("Failed to delete dynamically created voice channel: {} ", | ||
| channel.getName(), error)); | ||
| } | ||
|
|
||
| private void sendWarningEmbed(VoiceChannel channel) { | ||
| MessageEmbed messageEmbed = new EmbedBuilder() | ||
| .addField("👋 Heads up!", | ||
| """ | ||
| This is a **temporary** voice chat channel. Messages sent here will be *cleared* once \ | ||
| the channel is deleted when everyone leaves. If you need to keep something important, \ | ||
| make sure to save it elsewhere. 💬 | ||
| """, | ||
| false) | ||
| .build(); | ||
|
|
||
| channel.sendMessageEmbeds(messageEmbed).queue(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would kinda prefer if this workflow will post a warning in the voice channel chat with a specific timer for deletion (30sec) or whatever, then checking again for
memberCount == 0and then actually deleting or otherwise stopping the workflowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this when reviewing and decided to skip past a timer. There is a message sent when the channel is created and there's really no need to keep the channel longer. It can be recreated right away anyway.
Having said this, I am now thinking about moderation...
Temp voice chat -> users posting nonsense -> evidence deleted ...?
Perhaps instead of deleting the channel, we can move it to a private/mod-only
voice-chat-archivechannel so only we can still check chats. Followed by a daily clean up task.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds great, we need to keep track of what people post in these ephemeral channels. Perhaps instead of deleting, send a log of all the messages sent as one embed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll hit the character limit very quickly with an embed. It's best to just hide the channel so only moderators can see this and clean up after some time period.