16
16
import org .togetherjava .tjbot .config .Config ;
17
17
18
18
import java .time .ZoneOffset ;
19
- import java .util .ArrayList ;
20
- import java .util .Collection ;
21
- import java .util .List ;
22
- import java .util .Objects ;
19
+ import java .util .*;
23
20
import java .util .function .Predicate ;
24
21
import java .util .regex .Pattern ;
22
+ import java .util .stream .Collectors ;
25
23
26
24
/**
27
25
* This command lists all moderation actions that have been taken against a given user, for example
@@ -54,20 +52,39 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore) {
54
52
this .actionsStore = Objects .requireNonNull (actionsStore );
55
53
}
56
54
57
- private static MessageEmbed createSummaryMessage (@ NotNull User user ,
55
+ private static @ NotNull MessageEmbed createSummaryMessage (@ NotNull User user ,
58
56
@ NotNull Collection <ActionRecord > actions ) {
59
- int actionAmount = actions .size ();
60
- String description = actionAmount == 0 ? "There are **no actions** against the user."
61
- : "There are **%d actions** against the user." .formatted (actionAmount );
62
-
63
57
return new EmbedBuilder ().setTitle ("Audit log of **%s**" .formatted (user .getAsTag ()))
64
58
.setAuthor (user .getName (), null , user .getAvatarUrl ())
65
- .setDescription (description )
59
+ .setDescription (createSummaryMessageDescription ( actions ) )
66
60
.setColor (ModerationUtils .AMBIENT_COLOR )
67
61
.build ();
68
62
}
69
63
70
- private static RestAction <MessageEmbed > actionToMessage (@ NotNull ActionRecord action ,
64
+ private static @ NotNull String createSummaryMessageDescription (
65
+ @ NotNull Collection <ActionRecord > actions ) {
66
+ int actionAmount = actions .size ();
67
+ if (actionAmount == 0 ) {
68
+ return "There are **no actions** against the user." ;
69
+ }
70
+
71
+ String shortSummary = "There are **%d actions** against the user." .formatted (actionAmount );
72
+
73
+ // Summary of all actions with their count, like "- Warn: 5", descending
74
+ Map <ModerationUtils .Action , Long > actionTypeToCount = actions .stream ()
75
+ .collect (Collectors .groupingBy (ActionRecord ::actionType , Collectors .counting ()));
76
+ String typeCountSummary = actionTypeToCount .entrySet ()
77
+ .stream ()
78
+ .filter (typeAndCount -> typeAndCount .getValue () > 0 )
79
+ .sorted (Map .Entry .<ModerationUtils .Action , Long >comparingByValue ().reversed ())
80
+ .map (typeAndCount -> "- **%s**: %d" .formatted (typeAndCount .getKey (),
81
+ typeAndCount .getValue ()))
82
+ .collect (Collectors .joining ("\n " ));
83
+
84
+ return shortSummary + "\n " + typeCountSummary ;
85
+ }
86
+
87
+ private static @ NotNull RestAction <MessageEmbed > actionToMessage (@ NotNull ActionRecord action ,
71
88
@ NotNull JDA jda ) {
72
89
String footer = action .actionExpiresAt () == null ? null
73
90
: "Temporary action, expires at %s" .formatted (TimeUtil
@@ -84,7 +101,8 @@ private static RestAction<MessageEmbed> actionToMessage(@NotNull ActionRecord ac
84
101
.build ());
85
102
}
86
103
87
- private static <E > List <E > prependElement (E element , Collection <? extends E > elements ) {
104
+ private static <E > @ NotNull List <E > prependElement (@ NotNull E element ,
105
+ @ NotNull Collection <? extends E > elements ) {
88
106
List <E > allElements = new ArrayList <>(elements .size () + 1 );
89
107
allElements .add (element );
90
108
allElements .addAll (elements );
0 commit comments