2323import org .togetherjava .tjbot .config .Config ;
2424
2525import java .time .Instant ;
26- import java .time .temporal .ChronoUnit ;
2726import java .util .List ;
2827import java .util .Objects ;
2928import java .util .Optional ;
@@ -46,9 +45,8 @@ public final class BanCommand extends SlashCommandAdapter {
4645 private static final String REASON_OPTION = "reason" ;
4746 private static final String COMMAND_NAME = "ban" ;
4847 private static final String ACTION_VERB = "ban" ;
49- private static final String PERMANENT_DURATION = "permanent" ;
50- private static final List <String > DURATIONS = List .of (PERMANENT_DURATION , "1 hour" , "3 hours" ,
51- "1 day" , "2 days" , "3 days" , "7 days" , "30 days" );
48+ private static final List <String > DURATIONS = List .of (ModerationUtils .PERMANENT_DURATION ,
49+ "1 hour" , "3 hours" , "1 day" , "2 days" , "3 days" , "7 days" , "30 days" );
5250 private final Predicate <String > hasRequiredRole ;
5351 private final ModerationActionsStore actionsStore ;
5452
@@ -88,10 +86,10 @@ private static RestAction<InteractionHook> handleAlreadyBanned(@NotNull Guild.Ba
8886 }
8987
9088 private static RestAction <Boolean > sendDm (@ NotNull ISnowflake target ,
91- @ Nullable TemporaryBanData temporaryBanData , @ NotNull String reason ,
89+ @ Nullable ModerationUtils . TemporaryData temporaryData , @ NotNull String reason ,
9290 @ NotNull Guild guild , @ NotNull GenericEvent event ) {
9391 String durationMessage =
94- temporaryBanData == null ? "permanently" : "for " + temporaryBanData .duration ;
92+ temporaryData == null ? "permanently" : "for " + temporaryData .duration () ;
9593 String dmMessage =
9694 """
9795 Hey there, sorry to tell you but unfortunately you have been banned %s from the server %s.
@@ -108,12 +106,10 @@ private static RestAction<Boolean> sendDm(@NotNull ISnowflake target,
108106 }
109107
110108 private static @ NotNull MessageEmbed sendFeedback (boolean hasSentDm , @ NotNull User target ,
111- @ NotNull Member author , @ Nullable TemporaryBanData temporaryBanData ,
109+ @ NotNull Member author , @ Nullable ModerationUtils . TemporaryData temporaryData ,
112110 @ NotNull String reason ) {
113- @ SuppressWarnings ("java:S1192" ) // this is not the name of the option but the user-friendly
114- // display text
115111 String durationText = "The ban duration is: "
116- + (temporaryBanData == null ? "permanent" : temporaryBanData .duration );
112+ + (temporaryData == null ? "permanent" : temporaryData .duration () );
117113 String dmNoticeText = "" ;
118114 if (!hasSentDm ) {
119115 dmNoticeText = "\n (Unable to send them a DM.)" ;
@@ -145,48 +141,28 @@ private static Optional<RestAction<InteractionHook>> handleNotAlreadyBannedRespo
145141 .setEphemeral (true ));
146142 }
147143
148- private static @ NotNull Optional <TemporaryBanData > computeTemporaryBanData (
149- @ NotNull String durationText ) {
150- if (PERMANENT_DURATION .equals (durationText )) {
151- return Optional .empty ();
152- }
153-
154- // 1 minute, 1 day, 2 days, ...
155- String [] data = durationText .split (" " , 2 );
156- int duration = Integer .parseInt (data [0 ]);
157- ChronoUnit unit = switch (data [1 ]) {
158- case "minute" , "minutes" -> ChronoUnit .MINUTES ;
159- case "hour" , "hours" -> ChronoUnit .HOURS ;
160- case "day" , "days" -> ChronoUnit .DAYS ;
161- default -> throw new IllegalArgumentException (
162- "Unsupported ban duration: " + durationText );
163- };
164-
165- return Optional .of (new TemporaryBanData (Instant .now ().plus (duration , unit ), durationText ));
166- }
167-
168144 @ SuppressWarnings ("MethodWithTooManyParameters" )
169145 private RestAction <InteractionHook > banUserFlow (@ NotNull User target , @ NotNull Member author ,
170- @ Nullable TemporaryBanData temporaryBanData , @ NotNull String reason ,
146+ @ Nullable ModerationUtils . TemporaryData temporaryData , @ NotNull String reason ,
171147 int deleteHistoryDays , @ NotNull Guild guild , @ NotNull SlashCommandEvent event ) {
172- return sendDm (target , temporaryBanData , reason , guild , event )
173- .flatMap (hasSentDm -> banUser (target , author , temporaryBanData , reason ,
174- deleteHistoryDays , guild ).map (banResult -> hasSentDm ))
175- .map (hasSentDm -> sendFeedback (hasSentDm , target , author , temporaryBanData , reason ))
148+ return sendDm (target , temporaryData , reason , guild , event )
149+ .flatMap (hasSentDm -> banUser (target , author , temporaryData , reason , deleteHistoryDays ,
150+ guild ).map (banResult -> hasSentDm ))
151+ .map (hasSentDm -> sendFeedback (hasSentDm , target , author , temporaryData , reason ))
176152 .flatMap (event ::replyEmbeds );
177153 }
178154
179155 private AuditableRestAction <Void > banUser (@ NotNull User target , @ NotNull Member author ,
180- @ Nullable TemporaryBanData temporaryBanData , @ NotNull String reason ,
156+ @ Nullable ModerationUtils . TemporaryData temporaryData , @ NotNull String reason ,
181157 int deleteHistoryDays , @ NotNull Guild guild ) {
182158 String durationMessage =
183- temporaryBanData == null ? "permanently" : "for " + temporaryBanData .duration ;
159+ temporaryData == null ? "permanently" : "for " + temporaryData .duration () ;
184160 logger .info (
185161 "'{}' ({}) banned the user '{}' ({}) {} from guild '{}' and deleted their message history of the last {} days, for reason '{}'." ,
186162 author .getUser ().getAsTag (), author .getId (), target .getAsTag (), target .getId (),
187163 durationMessage , guild .getName (), deleteHistoryDays , reason );
188164
189- Instant expiresAt = temporaryBanData == null ? null : temporaryBanData . unbanTime ;
165+ Instant expiresAt = temporaryData == null ? null : temporaryData . expiresAt () ;
190166 actionsStore .addAction (guild .getIdLong (), author .getIdLong (), target .getIdLong (),
191167 ModerationAction .BAN , expiresAt , reason );
192168
@@ -230,7 +206,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
230206
231207 Guild guild = Objects .requireNonNull (event .getGuild ());
232208 Member bot = guild .getSelfMember ();
233- Optional <TemporaryBanData > temporaryBanData = computeTemporaryBanData (duration );
209+ Optional <ModerationUtils .TemporaryData > temporaryData =
210+ ModerationUtils .computeTemporaryData (duration );
234211
235212 if (!handleChecks (bot , author , targetOption .getAsMember (), reason , guild , event )) {
236213 return ;
@@ -247,12 +224,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
247224
248225 return handleNotAlreadyBannedResponse (
249226 Objects .requireNonNull (alreadyBanned .getFailure ()), event , guild , target )
250- .orElseGet (() -> banUserFlow (target , author , temporaryBanData .orElse (null ),
227+ .orElseGet (() -> banUserFlow (target , author , temporaryData .orElse (null ),
251228 reason , deleteHistoryDays , guild , event ));
252229 }).queue ();
253230 }
254-
255-
256- private record TemporaryBanData (@ NotNull Instant unbanTime , @ NotNull String duration ) {
257- }
258231}
0 commit comments