1
1
package org .togetherjava .tjbot .commands .help ;
2
2
3
+ import com .github .benmanes .caffeine .cache .Cache ;
4
+ import com .github .benmanes .caffeine .cache .Caffeine ;
3
5
import net .dv8tion .jda .api .entities .Guild ;
4
6
import net .dv8tion .jda .api .entities .Message ;
5
7
import net .dv8tion .jda .api .entities .Role ;
14
16
import org .togetherjava .tjbot .commands .SlashCommandVisibility ;
15
17
import org .togetherjava .tjbot .config .Config ;
16
18
19
+ import java .time .Instant ;
20
+ import java .time .temporal .ChronoUnit ;
17
21
import java .util .Optional ;
22
+ import java .util .concurrent .TimeUnit ;
18
23
19
24
/**
20
25
* Implements the {@code /change-help-category} command, which is able to change the category of a
29
34
public final class ChangeHelpCategoryCommand extends SlashCommandAdapter {
30
35
private static final String CATEGORY_OPTION = "category" ;
31
36
37
+ private static final int COOLDOWN_DURATION_VALUE = 1 ;
38
+ private static final ChronoUnit COOLDOWN_DURATION_UNIT = ChronoUnit .HOURS ;
39
+
32
40
private final HelpSystemHelper helper ;
41
+ private final Cache <Long , Instant > helpThreadIdToLastCategoryChange ;
33
42
34
43
/**
35
44
* Creates a new instance.
@@ -49,6 +58,11 @@ public ChangeHelpCategoryCommand(@NotNull Config config, @NotNull HelpSystemHelp
49
58
50
59
getData ().addOptions (category );
51
60
61
+ helpThreadIdToLastCategoryChange = Caffeine .newBuilder ()
62
+ .maximumSize (1_000 )
63
+ .expireAfterAccess (COOLDOWN_DURATION_VALUE , TimeUnit .of (COOLDOWN_DURATION_UNIT ))
64
+ .build ();
65
+
52
66
this .helper = helper ;
53
67
}
54
68
@@ -66,6 +80,16 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
66
80
return ;
67
81
}
68
82
83
+ if (isHelpThreadOnCooldown (helpThread )) {
84
+ event
85
+ .reply ("Please wait a bit, this command can only be used once per %d %s."
86
+ .formatted (COOLDOWN_DURATION_VALUE , COOLDOWN_DURATION_UNIT ))
87
+ .setEphemeral (true )
88
+ .queue ();
89
+ return ;
90
+ }
91
+ helpThreadIdToLastCategoryChange .put (helpThread .getIdLong (), Instant .now ());
92
+
69
93
event .deferReply ().queue ();
70
94
71
95
helper .renameChannelToCategoryTitle (helpThread , category )
@@ -96,4 +120,13 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) {
96
120
return action .flatMap (any -> helpThread .sendMessage (headsUpWithoutRole )
97
121
.flatMap (message -> message .editMessage (headsUpWithRole )));
98
122
}
123
+
124
+ private boolean isHelpThreadOnCooldown (@ NotNull ThreadChannel helpThread ) {
125
+ return Optional
126
+ .ofNullable (helpThreadIdToLastCategoryChange .getIfPresent (helpThread .getIdLong ()))
127
+ .map (lastCategoryChange -> lastCategoryChange .plus (COOLDOWN_DURATION_VALUE ,
128
+ COOLDOWN_DURATION_UNIT ))
129
+ .filter (Instant .now ()::isBefore )
130
+ .isPresent ();
131
+ }
99
132
}
0 commit comments