11package org .togetherjava .tjbot .features .basic ;
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 .Permission ;
57import net .dv8tion .jda .api .entities .Guild ;
3335import org .togetherjava .tjbot .features .componentids .Lifespan ;
3436
3537import java .awt .Color ;
38+ import java .time .Duration ;
3639import java .time .Instant ;
40+ import java .time .OffsetDateTime ;
3741import java .util .List ;
3842import java .util .Optional ;
43+ import java .util .concurrent .TimeUnit ;
3944import java .util .function .Predicate ;
4045import java .util .regex .Pattern ;
4146
@@ -51,8 +56,11 @@ public class ApplicationCreateCommand extends SlashCommandAdapter {
5156 private static final Color AMBIENT_COLOR = new Color (24 , 221 , 136 , 255 );
5257 private static final int MIN_REASON_LENGTH = 50 ;
5358 private static final int MAX_REASON_LENGTH = 500 ;
59+ private static final int APPLICATION_SUBMIT_COOLDOWN = 5 ;
5460 private static final String DEFAULT_QUESTION =
5561 "What makes you a valuable addition to the team? 😎" ;
62+
63+ private final Cache <Member , OffsetDateTime > applicationSubmitCooldown ;
5664 private final Predicate <String > applicationChannelPattern ;
5765 private final ApplicationFormConfig config ;
5866
@@ -70,6 +78,10 @@ public ApplicationCreateCommand(Config config) {
7078 this .config = config .getApplicationFormConfig ();
7179 this .applicationChannelPattern =
7280 Pattern .compile (this .config .applicationChannelPattern ()).asMatchPredicate ();
81+
82+ this .applicationSubmitCooldown = Caffeine .newBuilder ()
83+ .expireAfterWrite (APPLICATION_SUBMIT_COOLDOWN , TimeUnit .MINUTES )
84+ .build ();
7385 }
7486
7587 @ Override
@@ -121,6 +133,18 @@ public void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String>
121133 return ;
122134 }
123135
136+ OffsetDateTime timeSentCache = applicationSubmitCooldown .getIfPresent (event .getMember ());
137+ if (timeSentCache != null ) {
138+ Duration duration = Duration .between (timeSentCache , OffsetDateTime .now ());
139+
140+ if (duration .toMinutes () < APPLICATION_SUBMIT_COOLDOWN ) {
141+ event .reply ("Please wait before sending a new application form." )
142+ .setEphemeral (true )
143+ .queue ();
144+ return ;
145+ }
146+ }
147+
124148 TextInput body = TextInput
125149 .create (generateComponentId (event .getUser ().getId ()), "Question" ,
126150 TextInputStyle .PARAGRAPH )
@@ -162,6 +186,8 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
162186 event .reply ("Your application has been submitted. Thank you for applying! 😎" )
163187 .setEphemeral (true )
164188 .queue ();
189+
190+ applicationSubmitCooldown .put (event .getMember (), OffsetDateTime .now ());
165191 }
166192
167193 /**
0 commit comments