Skip to content

Commit eee594c

Browse files
tj-wazeisurajkumar
andauthored
fix: double fire when channel is deleted (#1398)
* fix: add caffeine cache to prevent double fire * fix: check is bot only on join * feat: add synchronized keyword * chore: try-catch the delete for extra protection --------- Co-authored-by: Suraj Kumar <sk96.uk@gmail.com>
1 parent 0309044 commit eee594c

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

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

Lines changed: 20 additions & 7 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
*
@@ -53,14 +59,10 @@ public void onVoiceUpdate(GuildVoiceUpdateEvent event) {
5359
Member member = event.getMember();
5460
User user = member.getUser();
5561

56-
if (user.isBot()) {
57-
return;
58-
}
59-
6062
AudioChannelUnion channelJoined = event.getChannelJoined();
6163
AudioChannelUnion channelLeft = event.getChannelLeft();
6264

63-
if (channelJoined != null && isVoiceChannel(channelJoined)) {
65+
if (channelJoined != null && isVoiceChannel(channelJoined) && !user.isBot()) {
6466
handleVoiceChannelJoin(event, channelJoined);
6567
}
6668

@@ -77,7 +79,13 @@ private void handleVoiceChannelJoin(GuildVoiceUpdateEvent event,
7779
}
7880
}
7981

80-
private void handleVoiceChannelLeave(AudioChannelUnion channelLeft) {
82+
private synchronized void handleVoiceChannelLeave(AudioChannelUnion channelLeft) {
83+
long channelId = channelLeft.getIdLong();
84+
85+
if (Boolean.TRUE.equals(deletedChannels.getIfPresent(channelId))) {
86+
return;
87+
}
88+
8189
if (!eventHappenOnDynamicRootChannel(channelLeft)) {
8290
logger.debug("Event happened on left channel {}", channelLeft);
8391

@@ -91,7 +99,12 @@ private void handleVoiceChannelLeave(AudioChannelUnion channelLeft) {
9199
if (messages.size() > 1) {
92100
archiveDynamicVoiceChannel(channelLeft);
93101
} else {
94-
channelLeft.delete().queue();
102+
deletedChannels.put(channelId, true);
103+
try {
104+
channelLeft.delete().queue();
105+
} catch (Exception _) {
106+
// Ignore
107+
}
95108
}
96109
});
97110
}

0 commit comments

Comments
 (0)