Skip to content

Commit 22886ae

Browse files
committed
add timeout for help channel close suggestions
1 parent 8fb7543 commit 22886ae

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/main/java/net/javadiscord/javabot/systems/help/HelpListener.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.ArrayList;
3434
import java.util.HashMap;
3535
import java.util.HashSet;
36+
import java.util.LinkedHashMap;
3637
import java.util.List;
3738
import java.util.Map;
3839
import java.util.Set;
@@ -66,6 +67,13 @@ public class HelpListener extends ListenerAdapter implements ButtonHandler {
6667
{"issue","solv"},
6768
{"thank"}
6869
};
70+
private final long SUGGEST_CLOSE_TIMEOUT = 5 * 60_000L;//5 minutes
71+
private final Map<Long, Long> recentlyCloseSuggestedPosts = new LinkedHashMap<>(8, 0.75f, true) {
72+
@Override
73+
protected boolean removeEldestEntry(Map.Entry<Long, Long> eldest) {
74+
return System.currentTimeMillis() > eldest.getValue() || size() >= 32;
75+
}
76+
};
6977

7078
@Override
7179
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
@@ -109,6 +117,10 @@ private void replyCloseSuggestionIfPatternMatches(Message msg) {
109117
if (content.contains("```")) {
110118
return;
111119
}
120+
long postId = msg.getChannel().getIdLong();
121+
if (recentlyCloseSuggestedPosts.containsKey(postId) && recentlyCloseSuggestedPosts.get(postId) > System.currentTimeMillis()) {
122+
return;
123+
}
112124
if(msg.getChannel().asThreadChannel().getOwnerIdLong() == msg.getAuthor().getIdLong()) {
113125
for (String[] detector : closeSuggestionDetectors) {
114126
if (doesMatchDetector(content, detector)) {
@@ -120,6 +132,7 @@ private void replyCloseSuggestionIfPatternMatches(Message msg) {
120132
.addActionRow(createCloseSuggestionButton(msg.getChannel().asThreadChannel()),
121133
Button.secondary(InteractionUtils.DELETE_ORIGINAL_TEMPLATE, "\uD83D\uDDD1️"))
122134
.queue();
135+
recentlyCloseSuggestedPosts.put(postId, System.currentTimeMillis() + SUGGEST_CLOSE_TIMEOUT);
123136
}
124137
}
125138
}

0 commit comments

Comments
 (0)