1313import net .dv8tion .jda .api .interactions .commands .OptionType ;
1414import net .dv8tion .jda .api .interactions .components .buttons .Button ;
1515import net .dv8tion .jda .api .requests .RestAction ;
16- import net .dv8tion .jda .api .utils .TimeUtil ;
1716import net .dv8tion .jda .api .utils .messages .MessageCreateBuilder ;
1817import net .dv8tion .jda .api .utils .messages .MessageEditBuilder ;
1918import net .dv8tion .jda .api .utils .messages .MessageRequest ;
2221import org .togetherjava .tjbot .features .CommandVisibility ;
2322import org .togetherjava .tjbot .features .SlashCommandAdapter ;
2423import org .togetherjava .tjbot .features .moderation .ActionRecord ;
25- import org .togetherjava .tjbot .features .moderation .ModerationAction ;
2624import org .togetherjava .tjbot .features .moderation .ModerationActionsStore ;
2725import org .togetherjava .tjbot .features .moderation .ModerationUtils ;
2826
2927import javax .annotation .Nullable ;
3028
31- import java .time .Instant ;
32- import java .time .ZoneOffset ;
3329import java .util .ArrayList ;
3430import java .util .Collection ;
3531import java .util .List ;
36- import java .util .Map ;
3732import java .util .Objects ;
3833import java .util .function .Supplier ;
39- import java .util .stream .Collectors ;
4034
4135/**
4236 * This command lists all moderation actions that have been taken against a given user, for example
@@ -49,7 +43,6 @@ public final class AuditCommand extends SlashCommandAdapter {
4943 private static final String TARGET_OPTION = "user" ;
5044 private static final String COMMAND_NAME = "audit" ;
5145 private static final String ACTION_VERB = "audit" ;
52- private static final int MAX_PAGE_LENGTH = 10 ;
5346 private static final String PREVIOUS_BUTTON_LABEL = "⬅" ;
5447 private static final String NEXT_BUTTON_LABEL = "➡" ;
5548 private final ModerationActionsStore actionsStore ;
@@ -101,8 +94,8 @@ private boolean handleChecks(Member bot, Member author, @Nullable Member target,
10194
10295 /**
10396 * @param pageNumber page number to display when actions are divided into pages and each page
104- * can contain {@link AuditCommand#MAX_PAGE_LENGTH } actions, {@code -1} encodes the last
105- * page
97+ * can contain {@link ModerationUtils#groupActionsByPages(List) } actions, {@code -1}
98+ * encodes the last page
10699 */
107100 private <R extends MessageRequest <R >> RestAction <R > auditUser (
108101 Supplier <R > messageBuilderSupplier , long guildId , long targetId , long callerId ,
@@ -126,53 +119,20 @@ private <R extends MessageRequest<R>> RestAction<R> auditUser(
126119 pageNumberInLimits , totalPages , guildId , targetId , callerId ));
127120 }
128121
129- public static List <List <ActionRecord >> groupActionsByPages (List <ActionRecord > actions ) {
130- List <List <ActionRecord >> groupedActions = new ArrayList <>();
131- for (int i = 0 ; i < actions .size (); i ++) {
132- if (i % AuditCommand .MAX_PAGE_LENGTH == 0 ) {
133- groupedActions .add (new ArrayList <>(AuditCommand .MAX_PAGE_LENGTH ));
134- }
135-
136- groupedActions .getLast ().add (actions .get (i ));
137- }
138-
139- return groupedActions ;
122+ private static List <List <ActionRecord >> groupActionsByPages (List <ActionRecord > actions ) {
123+ return ModerationUtils .groupActionsByPages (actions );
140124 }
141125
142126 private static EmbedBuilder createSummaryEmbed (User user , Collection <ActionRecord > actions ) {
143127 String avatarOrDefaultUrl = user .getEffectiveAvatarUrl ();
144128
145129 return new EmbedBuilder ().setTitle ("Audit log of **%s**" .formatted (user .getName ()))
146130 .setAuthor (user .getName (), null , avatarOrDefaultUrl )
147- .setDescription (createSummaryMessageDescription (actions ))
131+ .setDescription (ModerationUtils .createSummaryMessageDescription (actions )) // Redirected
132+ // here
148133 .setColor (ModerationUtils .AMBIENT_COLOR );
149134 }
150135
151- public static String createSummaryMessageDescription (Collection <ActionRecord > actions ) {
152- int actionAmount = actions .size ();
153-
154- String shortSummary = "There are **%s actions** against the user."
155- .formatted (actionAmount == 0 ? "no" : actionAmount );
156-
157- if (actionAmount == 0 ) {
158- return shortSummary ;
159- }
160-
161- // Summary of all actions with their count, like "- Warn: 5", descending
162- Map <ModerationAction , Long > actionTypeToCount = actions .stream ()
163- .collect (Collectors .groupingBy (ActionRecord ::actionType , Collectors .counting ()));
164-
165- String typeCountSummary = actionTypeToCount .entrySet ()
166- .stream ()
167- .filter (typeAndCount -> typeAndCount .getValue () > 0 )
168- .sorted (Map .Entry .<ModerationAction , Long >comparingByValue ().reversed ())
169- .map (typeAndCount -> "- **%s**: %d" .formatted (typeAndCount .getKey (),
170- typeAndCount .getValue ()))
171- .collect (Collectors .joining ("\n " ));
172-
173- return shortSummary + "\n " + typeCountSummary ;
174- }
175-
176136 private RestAction <EmbedBuilder > attachEmbedFields (EmbedBuilder auditEmbed ,
177137 List <? extends List <ActionRecord >> groupedActions , int pageNumber , int totalPages ,
178138 JDA jda ) {
@@ -192,26 +152,8 @@ private RestAction<EmbedBuilder> attachEmbedFields(EmbedBuilder auditEmbed,
192152 });
193153 }
194154
195- public static RestAction <MessageEmbed .Field > actionToField (ActionRecord action , JDA jda ) {
196- return jda .retrieveUserById (action .authorId ())
197- .map (author -> author == null ? "(unknown user)" : author .getName ())
198- .map (authorText -> {
199- String expiresAtFormatted = action .actionExpiresAt () == null ? ""
200- : "\n Temporary action, expires at: " + formatTime (action .actionExpiresAt ());
201-
202- String fieldName = "%s by %s" .formatted (action .actionType ().name (), authorText );
203- String fieldDescription = """
204- %s
205- Issued at: %s%s
206- """ .formatted (action .reason (), formatTime (action .issuedAt ()),
207- expiresAtFormatted );
208-
209- return new MessageEmbed .Field (fieldName , fieldDescription , false );
210- });
211- }
212-
213- private static String formatTime (Instant when ) {
214- return TimeUtil .getDateTimeString (when .atOffset (ZoneOffset .UTC ));
155+ private static RestAction <MessageEmbed .Field > actionToField (ActionRecord action , JDA jda ) {
156+ return ModerationUtils .actionToField (action , jda );
215157 }
216158
217159 private <R extends MessageRequest <R >> R attachPageTurnButtons (
0 commit comments