11package org .togetherjava .tjbot .features .voicechat ;
22
3+ import com .github .benmanes .caffeine .cache .Cache ;
4+ import com .github .benmanes .caffeine .cache .Caffeine ;
35import net .dv8tion .jda .api .EmbedBuilder ;
46import net .dv8tion .jda .api .entities .Guild ;
57import net .dv8tion .jda .api .entities .Member ;
2022import org .togetherjava .tjbot .features .VoiceReceiverAdapter ;
2123
2224import java .util .Optional ;
25+ import java .util .concurrent .TimeUnit ;
2326
2427/**
2528 * Handles dynamic voice channel creation and deletion based on user activity.
@@ -34,6 +37,9 @@ public final class DynamicVoiceChat extends VoiceReceiverAdapter {
3437 private final VoiceChatCleanupStrategy voiceChatCleanupStrategy ;
3538 private final DynamicVoiceChatConfig dynamicVoiceChannelConfig ;
3639
40+ private final Cache <Long , Boolean > deletedChannels =
41+ Caffeine .newBuilder ().expireAfterWrite (5 , TimeUnit .MINUTES ).build ();
42+
3743 /**
3844 * Creates a new instance of {@code DynamicVoiceChat}
3945 *
@@ -78,6 +84,12 @@ private void handleVoiceChannelJoin(GuildVoiceUpdateEvent event,
7884 }
7985
8086 private void handleVoiceChannelLeave (AudioChannelUnion channelLeft ) {
87+ long channelId = channelLeft .getIdLong ();
88+
89+ if (Boolean .TRUE .equals (deletedChannels .getIfPresent (channelId ))) {
90+ return ;
91+ }
92+
8193 if (!eventHappenOnDynamicRootChannel (channelLeft )) {
8294 logger .debug ("Event happened on left channel {}" , channelLeft );
8395
@@ -92,6 +104,7 @@ private void handleVoiceChannelLeave(AudioChannelUnion channelLeft) {
92104 archiveDynamicVoiceChannel (channelLeft );
93105 } else {
94106 channelLeft .delete ().queue ();
107+ deletedChannels .put (channelId , true );
95108 }
96109 });
97110 }
0 commit comments