Skip to content

Commit d69dcb8

Browse files
committed
Added type-to-count table to audit summary
- and added some missing NotNulls in AuditCommand
1 parent 74c679e commit d69dcb8

File tree

1 file changed

+30
-12
lines changed
  • application/src/main/java/org/togetherjava/tjbot/commands/moderation

1 file changed

+30
-12
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
import org.togetherjava.tjbot.config.Config;
1717

1818
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.*;
2320
import java.util.function.Predicate;
2421
import java.util.regex.Pattern;
22+
import java.util.stream.Collectors;
2523

2624
/**
2725
* 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) {
5452
this.actionsStore = Objects.requireNonNull(actionsStore);
5553
}
5654

57-
private static MessageEmbed createSummaryMessage(@NotNull User user,
55+
private static @NotNull MessageEmbed createSummaryMessage(@NotNull User user,
5856
@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-
6357
return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getAsTag()))
6458
.setAuthor(user.getName(), null, user.getAvatarUrl())
65-
.setDescription(description)
59+
.setDescription(createSummaryMessageDescription(actions))
6660
.setColor(ModerationUtils.AMBIENT_COLOR)
6761
.build();
6862
}
6963

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,
7188
@NotNull JDA jda) {
7289
String footer = action.actionExpiresAt() == null ? null
7390
: "Temporary action, expires at %s".formatted(TimeUtil
@@ -84,7 +101,8 @@ private static RestAction<MessageEmbed> actionToMessage(@NotNull ActionRecord ac
84101
.build());
85102
}
86103

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) {
88106
List<E> allElements = new ArrayList<>(elements.size() + 1);
89107
allElements.add(element);
90108
allElements.addAll(elements);

0 commit comments

Comments
 (0)