Skip to content

Commit c919cd9

Browse files
committed
Made requested changes and made the audit message ephemeral
1 parent 6e1805f commit c919cd9

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/features/moderation/ModerationUtils.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ private ModerationUtils() {
5252
* {@link AuditableRestAction#reason(String)}.
5353
*/
5454
private static final int REASON_MAX_LENGTH = 512;
55+
/**
56+
* The maximum amount of moderation actions displayed on a single page
57+
*/
58+
private static final int MAX_PAGE_LENGTH = 10;
5559
/**
5660
* Human-readable text representing the duration of a permanent action, will be shown to the
5761
* user as option for selection.
@@ -458,11 +462,10 @@ record TemporaryData(Instant expiresAt, String duration) {
458462
*/
459463
public static List<List<ActionRecord>> groupActionsByPages(List<ActionRecord> actions) {
460464
List<List<ActionRecord>> groupedActions = new ArrayList<>();
461-
final int maxPageLength = 10;
462465

463466
for (int i = 0; i < actions.size(); i++) {
464-
if (i % maxPageLength == 0) {
465-
groupedActions.add(new ArrayList<>(maxPageLength));
467+
if (i % MAX_PAGE_LENGTH == 0) {
468+
groupedActions.add(new ArrayList<>(MAX_PAGE_LENGTH));
466469
}
467470
groupedActions.getLast().add(actions.get(i));
468471
}
@@ -503,29 +506,31 @@ public static String createSummaryMessageDescription(Collection<ActionRecord> ac
503506
/**
504507
* Converts an action record item asynchronously into a formatted embed data field.
505508
*
506-
* @param action the record data item
509+
* @param actionRecord the moderation action history record to convert
507510
* @param jda the active JDA instance used to resolve the moderator's handle
508-
* @return a field representation task mapping out the execution card detail
511+
* @return a rest action that resolves to the embed field representing the moderation action
509512
*/
510-
public static RestAction<MessageEmbed.Field> actionToField(ActionRecord action, JDA jda) {
511-
return jda.retrieveUserById(action.authorId())
513+
public static RestAction<MessageEmbed.Field> actionToEmbedField(ActionRecord actionRecord,
514+
JDA jda) {
515+
return jda.retrieveUserById(actionRecord.authorId())
512516
.map(author -> author == null ? "(unknown user)" : author.getName())
513517
.map(authorText -> {
514-
String expiresAtFormatted = action.actionExpiresAt() == null ? ""
515-
: "\nTemporary action, expires at: " + net.dv8tion.jda.api.utils.TimeUtil
516-
.getDateTimeString(action.actionExpiresAt().atOffset(ZoneOffset.UTC));
517-
518-
String fieldName = "%s by %s".formatted(action.actionType().name(), authorText);
519-
String fieldDescription =
520-
"""
521-
%s
522-
Issued at: %s%s
523-
""".formatted(action.reason(),
524-
net.dv8tion.jda.api.utils.TimeUtil
525-
.getDateTimeString(action.issuedAt().atOffset(ZoneOffset.UTC)),
526-
expiresAtFormatted);
527-
528-
return new MessageEmbed.Field(fieldName, fieldDescription, false);
518+
String expiresAtFormatted = actionRecord.actionExpiresAt() == null ? ""
519+
: "\nTemporary action, expires at: "
520+
+ net.dv8tion.jda.api.utils.TimeUtil.getDateTimeString(
521+
actionRecord.actionExpiresAt().atOffset(ZoneOffset.UTC));
522+
523+
String embedFieldName =
524+
"%s by %s".formatted(actionRecord.actionType().name(), authorText);
525+
String embedFieldDescription = """
526+
%s
527+
Issued at: %s%s
528+
""".formatted(actionRecord.reason(),
529+
net.dv8tion.jda.api.utils.TimeUtil
530+
.getDateTimeString(actionRecord.issuedAt().atOffset(ZoneOffset.UTC)),
531+
expiresAtFormatted);
532+
533+
return new MessageEmbed.Field(embedFieldName, embedFieldDescription, false);
529534
});
530535
}
531536

application/src/main/java/org/togetherjava/tjbot/features/moderation/ReportCommand.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
import java.awt.Color;
3131
import java.time.Instant;
3232
import java.time.temporal.ChronoUnit;
33-
import java.util.Collections;
34-
import java.util.List;
35-
import java.util.Objects;
36-
import java.util.Optional;
33+
import java.util.*;
3734
import java.util.concurrent.TimeUnit;
3835
import java.util.function.Predicate;
3936
import java.util.regex.Pattern;
@@ -249,7 +246,7 @@ static ReportedMessage ofArgs(List<String> args) {
249246

250247
@Override
251248
public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
252-
event.deferEdit().queue();
249+
event.deferReply(true).queue();
253250

254251
Guild guild =
255252
Objects.requireNonNull(event.getGuild(), "Guild cannot be null for this command.");
@@ -258,7 +255,7 @@ public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
258255
long reportedUserId = Long.parseLong(args.get(0));
259256
int targetPage = Integer.parseInt(args.get(1));
260257

261-
List<ActionRecord> actions = new java.util.ArrayList<>(
258+
List<ActionRecord> actions = new ArrayList<>(
262259
moderationActionsStore.getActionsByTargetAscending(guildId, reportedUserId));
263260
Collections.reverse(actions);
264261
List<List<ActionRecord>> pages = ModerationUtils.groupActionsByPages(actions);
@@ -293,7 +290,7 @@ private RestAction<List<MessageEmbed.Field>> prepareAuditEmbedTasks(
293290

294291
List<RestAction<MessageEmbed.Field>> fetchFieldActions = pages.get(currentPageIndex)
295292
.stream()
296-
.map(action -> ModerationUtils.actionToField(action, event.getJDA()))
293+
.map(actionRecord -> ModerationUtils.actionToEmbedField(actionRecord, event.getJDA()))
297294
.toList();
298295

299296
return RestAction.allOf(fetchFieldActions).map(embedFields -> {

application/src/main/java/org/togetherjava/tjbot/features/moderation/audit/AuditCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private RestAction<EmbedBuilder> attachEmbedFields(EmbedBuilder auditEmbed,
152152
}
153153

154154
private static RestAction<MessageEmbed.Field> actionToField(ActionRecord action, JDA jda) {
155-
return ModerationUtils.actionToField(action, jda);
155+
return ModerationUtils.actionToEmbedField(action, jda);
156156
}
157157

158158
private <R extends MessageRequest<R>> R attachPageTurnButtons(

0 commit comments

Comments
 (0)