Skip to content

Commit 73d5bd6

Browse files
committed
fix: add caffeine cache to prevent double fire
1 parent 0309044 commit 73d5bd6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

application/src/main/java/org/togetherjava/tjbot/features/voicechat/DynamicVoiceChat.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.togetherjava.tjbot.features.voicechat;
22

3+
import com.github.benmanes.caffeine.cache.Cache;
4+
import com.github.benmanes.caffeine.cache.Caffeine;
35
import net.dv8tion.jda.api.EmbedBuilder;
46
import net.dv8tion.jda.api.entities.Guild;
57
import net.dv8tion.jda.api.entities.Member;
@@ -20,6 +22,7 @@
2022
import org.togetherjava.tjbot.features.VoiceReceiverAdapter;
2123

2224
import 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

Comments
 (0)