From eeccd2e7789ae75cb476fa8573c385ddca51a013 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 27 Jul 2022 18:41:56 +0530 Subject: [PATCH 01/20] Audit rework --- .../commands/moderation/AuditCommand.java | 75 +++++++------------ 1 file changed, 26 insertions(+), 49 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index edd7c0df43..f630268b29 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -7,16 +7,19 @@ import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.utils.TimeUtil; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; + import org.togetherjava.tjbot.commands.SlashCommandAdapter; import org.togetherjava.tjbot.commands.SlashCommandVisibility; import org.togetherjava.tjbot.config.Config; +import java.time.Instant; import java.time.ZoneOffset; import java.util.*; +import java.util.function.Function; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -53,27 +56,24 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi this.actionsStore = Objects.requireNonNull(actionsStore); } - private static @NotNull MessageEmbed createSummaryMessage(@NotNull User user, - @NotNull Collection actions) { + private static @NotNull EmbedBuilder createSummaryEmbed(@NotNull User user, + @NotNull Collection actions) { return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getAsTag())) .setAuthor(user.getName(), null, user.getAvatarUrl()) .setDescription(createSummaryMessageDescription(actions)) - .setColor(ModerationUtils.AMBIENT_COLOR) - .build(); + .setColor(ModerationUtils.AMBIENT_COLOR); } private static @NotNull String createSummaryMessageDescription( @NotNull Collection actions) { int actionAmount = actions.size(); - if (actionAmount == 0) { - return "There are **no actions** against the user."; - } - String shortSummary = "There are **%d actions** against the user.".formatted(actionAmount); + String shortSummary = "There are **%s actions** against the user.".formatted(actionAmount == 0 ? "no" : actionAmount); // Summary of all actions with their count, like "- Warn: 5", descending Map actionTypeToCount = actions.stream() .collect(Collectors.groupingBy(ActionRecord::actionType, Collectors.counting())); + String typeCountSummary = actionTypeToCount.entrySet() .stream() .filter(typeAndCount -> typeAndCount.getValue() > 0) @@ -85,30 +85,19 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi return shortSummary + "\n" + typeCountSummary; } - private static @NotNull RestAction actionToMessage(@NotNull ActionRecord action, - @NotNull JDA jda) { - String footer = action.actionExpiresAt() == null ? null - : "Temporary action, expires at %s".formatted(TimeUtil - .getDateTimeString(action.actionExpiresAt().atOffset(ZoneOffset.UTC))); - - return jda.retrieveUserById(action.authorId()) - .onErrorMap(error -> null) - .map(author -> new EmbedBuilder().setTitle(action.actionType().name()) - .setAuthor(author == null ? "(unknown user)" : author.getAsTag(), null, - author == null ? null : author.getAvatarUrl()) - .setDescription(action.reason()) - .setTimestamp(action.issuedAt()) - .setFooter(footer) - .setColor(ModerationUtils.AMBIENT_COLOR) - .build()); - } + private static @NotNull MessageEmbed.Field actionToField(@NotNull ActionRecord action, + @NotNull JDA jda) { + Function formatTime = instant -> TimeUtil.getDateTimeString(instant.atOffset(ZoneOffset.UTC)); - private static @NotNull List prependElement(@NotNull E element, - @NotNull Collection elements) { - List allElements = new ArrayList<>(elements.size() + 1); - allElements.add(element); - allElements.addAll(elements); - return allElements; + String expiresAt = action.actionExpiresAt() == null ? "" : "\nTemporary action, Expires at: " + formatTime.apply(action.actionExpiresAt()); + + User targetUser = jda.getUserById(action.authorId()); + + return new MessageEmbed.Field( + action.actionType().name() + " by " + (targetUser == null ? "(unknown user)" : targetUser.getAsTag()), + action.reason() + "\nIssued at: " + formatTime.apply(action.issuedAt()) + expiresAt, + false + ); } @Override @@ -121,7 +110,7 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { Guild guild = Objects.requireNonNull(event.getGuild()); Member bot = guild.getSelfMember(); - if (!handleChecks(bot, author, targetOption.getAsMember(), guild, event)) { + if (!handleChecks(bot, author, targetOption.getAsMember(), event)) { return; } @@ -130,7 +119,7 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") private boolean handleChecks(@NotNull Member bot, @NotNull Member author, - @Nullable Member target, @NotNull Guild guild, @NotNull IReplyCallback event) { + @Nullable Member target, @NotNull IReplyCallback event) { // Member doesn't exist if attempting to audit a user who is not part of the guild. if (target != null && !ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, target, event)) { @@ -144,21 +133,9 @@ private void auditUser(@NotNull User user, @NotNull ISnowflake guild, List actions = actionsStore.getActionsByTargetAscending(guild.getIdLong(), user.getIdLong()); - MessageEmbed summary = createSummaryMessage(user, actions); - if (actions.isEmpty()) { - event.replyEmbeds(summary).queue(); - return; - } - - // Computing messages for actual actions is done deferred and might require asking the - // Discord API - event.deferReply().queue(); - JDA jda = event.getJDA(); + EmbedBuilder audit = createSummaryEmbed(user, actions); + actions.forEach(action -> audit.addField(actionToField(action, event.getJDA()))); - RestAction> messagesTask = RestAction - .allOf(actions.stream().map(action -> actionToMessage(action, jda)).toList()); - messagesTask.map(messages -> prependElement(summary, messages)) - .flatMap(messages -> event.getHook().sendMessageEmbeds(messages)) - .queue(); + event.replyEmbeds(audit.build()).queue(); } } From ee74ee9867a8cf171e0eef19f39c93aed4775737 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 27 Jul 2022 19:03:03 +0530 Subject: [PATCH 02/20] reformat code --- .../commands/moderation/AuditCommand.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index f630268b29..5785b7276c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -57,7 +57,7 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi } private static @NotNull EmbedBuilder createSummaryEmbed(@NotNull User user, - @NotNull Collection actions) { + @NotNull Collection actions) { return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getAsTag())) .setAuthor(user.getName(), null, user.getAvatarUrl()) .setDescription(createSummaryMessageDescription(actions)) @@ -68,7 +68,8 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi @NotNull Collection actions) { int actionAmount = actions.size(); - String shortSummary = "There are **%s actions** against the user.".formatted(actionAmount == 0 ? "no" : actionAmount); + String shortSummary = "There are **%s actions** against the user." + .formatted(actionAmount == 0 ? "no" : actionAmount); // Summary of all actions with their count, like "- Warn: 5", descending Map actionTypeToCount = actions.stream() @@ -86,18 +87,22 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi } private static @NotNull MessageEmbed.Field actionToField(@NotNull ActionRecord action, - @NotNull JDA jda) { - Function formatTime = instant -> TimeUtil.getDateTimeString(instant.atOffset(ZoneOffset.UTC)); - - String expiresAt = action.actionExpiresAt() == null ? "" : "\nTemporary action, Expires at: " + formatTime.apply(action.actionExpiresAt()); + @NotNull JDA jda) { + Function formatTime = instant -> { + if (instant == null) + return ""; + return TimeUtil.getDateTimeString(instant.atOffset(ZoneOffset.UTC)); + }; - User targetUser = jda.getUserById(action.authorId()); + User author = jda.getUserById(action.authorId()); return new MessageEmbed.Field( - action.actionType().name() + " by " + (targetUser == null ? "(unknown user)" : targetUser.getAsTag()), - action.reason() + "\nIssued at: " + formatTime.apply(action.issuedAt()) + expiresAt, - false - ); + action.actionType().name() + " by " + + (author == null ? "(unknown user)" : author.getAsTag()), + action.reason() + "\nIssued at: " + formatTime.apply(action.issuedAt()) + + "\nTemporary action, Expires at: " + + formatTime.apply(action.actionExpiresAt()), + false); } @Override From 3841b0f40951bb0833133663d36ee94e4923f625 Mon Sep 17 00:00:00 2001 From: Taz Date: Wed, 27 Jul 2022 19:14:48 +0530 Subject: [PATCH 03/20] added brackets in if statement --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 5785b7276c..80faea1452 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -89,8 +89,9 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi private static @NotNull MessageEmbed.Field actionToField(@NotNull ActionRecord action, @NotNull JDA jda) { Function formatTime = instant -> { - if (instant == null) + if (instant == null) { return ""; + } return TimeUtil.getDateTimeString(instant.atOffset(ZoneOffset.UTC)); }; From 24e1dca9575adf1cfd09e0b398356ce9860f2792 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 18:14:35 +0530 Subject: [PATCH 04/20] added previous and next button --- .../commands/moderation/AuditCommand.java | 81 +++++++++++++++++-- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 80faea1452..902009ef34 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -1,12 +1,17 @@ package org.togetherjava.tjbot.commands.moderation; +import io.r2dbc.spi.Parameter; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.utils.TimeUtil; import org.jetbrains.annotations.NotNull; @@ -120,7 +125,12 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { return; } - auditUser(target, guild, event); + event.reply(auditMessage( + guild.getIdLong(), + target.getIdLong(), + 1, + event.getJDA() + )).queue(); } @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") @@ -134,14 +144,69 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event); } - private void auditUser(@NotNull User user, @NotNull ISnowflake guild, - @NotNull IReplyCallback event) { - List actions = - actionsStore.getActionsByTargetAscending(guild.getIdLong(), user.getIdLong()); + private Message auditMessage(long guildId, long targetId, int pageNo, @NotNull JDA jda) { + List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); - EmbedBuilder audit = createSummaryEmbed(user, actions); - actions.forEach(action -> audit.addField(actionToField(action, event.getJDA()))); + List> groupedActions = new ArrayList<>(); + for (int i = 0; i < actions.size(); i++) { + if (i % 25 == 0) { + groupedActions.add(new ArrayList<>(25)); + } + + groupedActions.get(groupedActions.size() - 1).add(actions.get(i)); + } + + int totalPage = groupedActions.size(); + + EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions) + .setFooter("Page: " + pageNo + "/" + totalPage); + groupedActions.get(pageNo - 1).forEach(action -> audit.addField(actionToField(action, jda))); + + return new MessageBuilder(audit.build()) + .setActionRows(ActionRow.of( + previousButton(guildId, targetId, pageNo), + nextButton(guildId, targetId, pageNo, totalPage) + )) + .build(); + } + + private Button previousButton(long guildId, long targetId, int pageNo) { + Button previousButton = Button.primary(generateComponentId( + String.valueOf(guildId), + String.valueOf(targetId), + String.valueOf(pageNo), + "-1" + ), "⬅"); + + if (pageNo == 1) { + previousButton = previousButton.asDisabled(); + } + + return previousButton; + } - event.replyEmbeds(audit.build()).queue(); + private Button nextButton(long guildId, long targetId, int pageNo, int totalPage) { + Button nextButton = Button.primary(generateComponentId( + String.valueOf(guildId), + String.valueOf(targetId), + String.valueOf(pageNo), + "1" + ), "➡"); + + if (pageNo == totalPage) { + nextButton = nextButton.asDisabled(); + } + + return nextButton; + } + + @Override + public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { + event.editMessage(auditMessage( + Long.parseLong(args.get(0)), + Long.parseLong(args.get(1)), + Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)), + event.getJDA() + )).queue(); } } From 1a26e0da4ee23c7cacb1278eb69a23873d458177 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 18:17:03 +0530 Subject: [PATCH 05/20] reformat code --- .../commands/moderation/AuditCommand.java | 46 ++++++------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 902009ef34..df4c189548 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -125,12 +125,7 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { return; } - event.reply(auditMessage( - guild.getIdLong(), - target.getIdLong(), - 1, - event.getJDA() - )).queue(); + event.reply(auditMessage(guild.getIdLong(), target.getIdLong(), 1, event.getJDA())).queue(); } @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") @@ -159,24 +154,19 @@ private Message auditMessage(long guildId, long targetId, int pageNo, @NotNull J int totalPage = groupedActions.size(); EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions) - .setFooter("Page: " + pageNo + "/" + totalPage); - groupedActions.get(pageNo - 1).forEach(action -> audit.addField(actionToField(action, jda))); + .setFooter("Page: " + pageNo + "/" + totalPage); + groupedActions.get(pageNo - 1) + .forEach(action -> audit.addField(actionToField(action, jda))); return new MessageBuilder(audit.build()) - .setActionRows(ActionRow.of( - previousButton(guildId, targetId, pageNo), - nextButton(guildId, targetId, pageNo, totalPage) - )) - .build(); + .setActionRows(ActionRow.of(previousButton(guildId, targetId, pageNo), + nextButton(guildId, targetId, pageNo, totalPage))) + .build(); } private Button previousButton(long guildId, long targetId, int pageNo) { - Button previousButton = Button.primary(generateComponentId( - String.valueOf(guildId), - String.valueOf(targetId), - String.valueOf(pageNo), - "-1" - ), "⬅"); + Button previousButton = Button.primary(generateComponentId(String.valueOf(guildId), + String.valueOf(targetId), String.valueOf(pageNo), "-1"), "⬅"); if (pageNo == 1) { previousButton = previousButton.asDisabled(); @@ -186,12 +176,8 @@ private Button previousButton(long guildId, long targetId, int pageNo) { } private Button nextButton(long guildId, long targetId, int pageNo, int totalPage) { - Button nextButton = Button.primary(generateComponentId( - String.valueOf(guildId), - String.valueOf(targetId), - String.valueOf(pageNo), - "1" - ), "➡"); + Button nextButton = Button.primary(generateComponentId(String.valueOf(guildId), + String.valueOf(targetId), String.valueOf(pageNo), "1"), "➡"); if (pageNo == totalPage) { nextButton = nextButton.asDisabled(); @@ -202,11 +188,9 @@ private Button nextButton(long guildId, long targetId, int pageNo, int totalPage @Override public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { - event.editMessage(auditMessage( - Long.parseLong(args.get(0)), - Long.parseLong(args.get(1)), - Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)), - event.getJDA() - )).queue(); + event + .editMessage(auditMessage(Long.parseLong(args.get(0)), Long.parseLong(args.get(1)), + Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)), event.getJDA())) + .queue(); } } From dd722225dc3ceb4e0b69a90360f531201825c15b Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 18:22:30 +0530 Subject: [PATCH 06/20] removed unnecessay import --- .../org/togetherjava/tjbot/commands/moderation/AuditCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index df4c189548..7123682b4c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -1,6 +1,5 @@ package org.togetherjava.tjbot.commands.moderation; -import io.r2dbc.spi.Parameter; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.MessageBuilder; From 9469e425b6f2f51d268d245dc91ea4e2b3ba2f05 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 18:46:03 +0530 Subject: [PATCH 07/20] handle no actions case --- .../tjbot/commands/moderation/AuditCommand.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 7123682b4c..0fa84b28f2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -152,12 +152,16 @@ private Message auditMessage(long guildId, long targetId, int pageNo, @NotNull J int totalPage = groupedActions.size(); - EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions) - .setFooter("Page: " + pageNo + "/" + totalPage); + EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions); + + if (groupedActions.size() == 0) { + return new MessageBuilder(audit.build()).build(); + } + groupedActions.get(pageNo - 1) .forEach(action -> audit.addField(actionToField(action, jda))); - return new MessageBuilder(audit.build()) + return new MessageBuilder(audit.setFooter("Page: " + pageNo + "/" + totalPage).build()) .setActionRows(ActionRow.of(previousButton(guildId, targetId, pageNo), nextButton(guildId, targetId, pageNo, totalPage))) .build(); From 78990761b1ead862ebd6b633894b16cbe14b0ded Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 18:56:55 +0530 Subject: [PATCH 08/20] used appropriate method in if statement --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 0fa84b28f2..2a3fc66a76 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -154,7 +154,7 @@ private Message auditMessage(long guildId, long targetId, int pageNo, @NotNull J EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions); - if (groupedActions.size() == 0) { + if (groupedActions.isEmpty()) { return new MessageBuilder(audit.build()).build(); } From 4b2757e9aa29ca157e3f0ebc63c29b6f94898b96 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 19:10:32 +0530 Subject: [PATCH 09/20] removed temp action line from non temp actions --- .../tjbot/commands/moderation/AuditCommand.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 2a3fc66a76..9ddaecdab3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -101,12 +101,15 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi User author = jda.getUserById(action.authorId()); + Instant expiresAt = action.actionExpiresAt(); + String expiresAtFormatted = expiresAt == null ? "" + : "\nTemporary action, expires at: " + formatTime.apply(expiresAt); + return new MessageEmbed.Field( action.actionType().name() + " by " + (author == null ? "(unknown user)" : author.getAsTag()), action.reason() + "\nIssued at: " + formatTime.apply(action.issuedAt()) - + "\nTemporary action, Expires at: " - + formatTime.apply(action.actionExpiresAt()), + + expiresAtFormatted, false); } From 8d221e0cce85955cd99bf9b94d59e7b0f799bca0 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 19:11:44 +0530 Subject: [PATCH 10/20] fixed method naming --- .../tjbot/commands/moderation/AuditCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 9ddaecdab3..8f26bf7325 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -127,7 +127,7 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { return; } - event.reply(auditMessage(guild.getIdLong(), target.getIdLong(), 1, event.getJDA())).queue(); + event.reply(auditUser(guild.getIdLong(), target.getIdLong(), 1, event.getJDA())).queue(); } @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") @@ -141,7 +141,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event); } - private Message auditMessage(long guildId, long targetId, int pageNo, @NotNull JDA jda) { + private Message auditUser(long guildId, long targetId, int pageNo, @NotNull JDA jda) { List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); List> groupedActions = new ArrayList<>(); @@ -195,7 +195,7 @@ private Button nextButton(long guildId, long targetId, int pageNo, int totalPage @Override public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { event - .editMessage(auditMessage(Long.parseLong(args.get(0)), Long.parseLong(args.get(1)), + .editMessage(auditUser(Long.parseLong(args.get(0)), Long.parseLong(args.get(1)), Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)), event.getJDA())) .queue(); } From 44708fac6df6aa8ace3f79c90bd41da068bfa83d Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 19:22:11 +0530 Subject: [PATCH 11/20] return short summary if there are no actions on the user --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 8f26bf7325..f5c3ab8ec1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -75,6 +75,10 @@ public AuditCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Confi String shortSummary = "There are **%s actions** against the user." .formatted(actionAmount == 0 ? "no" : actionAmount); + if (actionAmount == 0) { + return shortSummary; + } + // Summary of all actions with their count, like "- Warn: 5", descending Map actionTypeToCount = actions.stream() .collect(Collectors.groupingBy(ActionRecord::actionType, Collectors.counting())); From 2d9f7769fedabef4056a85453f6a0f57ab6fced6 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 20:09:53 +0530 Subject: [PATCH 12/20] made the code more understandable --- .../commands/moderation/AuditCommand.java | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index f5c3ab8ec1..df2c918fdf 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -39,6 +39,7 @@ public final class AuditCommand extends SlashCommandAdapter { private static final String TARGET_OPTION = "user"; private static final String COMMAND_NAME = "audit"; private static final String ACTION_VERB = "audit"; + private static final int MAX_PAGE_LENGTH = 25; private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; @@ -145,19 +146,23 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event); } - private Message auditUser(long guildId, long targetId, int pageNo, @NotNull JDA jda) { - List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); - + private @NotNull List> groupActions(@NotNull List actions) { List> groupedActions = new ArrayList<>(); for (int i = 0; i < actions.size(); i++) { - if (i % 25 == 0) { - groupedActions.add(new ArrayList<>(25)); + if (i % AuditCommand.MAX_PAGE_LENGTH == 0) { + groupedActions.add(new ArrayList<>(AuditCommand.MAX_PAGE_LENGTH)); } groupedActions.get(groupedActions.size() - 1).add(actions.get(i)); } - int totalPage = groupedActions.size(); + return groupedActions; + } + + private @NotNull Message auditUser(long guildId, long targetId, int pageNumber, @NotNull JDA jda) { + List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); + List> groupedActions = groupActions(actions); + int totalPages = groupedActions.size(); EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions); @@ -165,42 +170,38 @@ private Message auditUser(long guildId, long targetId, int pageNo, @NotNull JDA return new MessageBuilder(audit.build()).build(); } - groupedActions.get(pageNo - 1) + groupedActions.get(pageNumber - 1) .forEach(action -> audit.addField(actionToField(action, jda))); - return new MessageBuilder(audit.setFooter("Page: " + pageNo + "/" + totalPage).build()) - .setActionRows(ActionRow.of(previousButton(guildId, targetId, pageNo), - nextButton(guildId, targetId, pageNo, totalPage))) + return new MessageBuilder(audit.setFooter("Page: " + pageNumber + "/" + totalPages).build()) + .setActionRows(makeActionRow(guildId, targetId, pageNumber, totalPages)) .build(); } - private Button previousButton(long guildId, long targetId, int pageNo) { - Button previousButton = Button.primary(generateComponentId(String.valueOf(guildId), - String.valueOf(targetId), String.valueOf(pageNo), "-1"), "⬅"); + private @NotNull ActionRow makeActionRow(long guildId, long targetId, int pageNumber, int totalPages) { + String stringGuildId = String.valueOf(guildId); + String stringTargetId = String.valueOf(targetId); + String stringPageNumber = String.valueOf(pageNumber); - if (pageNo == 1) { + Button previousButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "-1"), "⬅"); + if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } - return previousButton; - } - - private Button nextButton(long guildId, long targetId, int pageNo, int totalPage) { - Button nextButton = Button.primary(generateComponentId(String.valueOf(guildId), - String.valueOf(targetId), String.valueOf(pageNo), "1"), "➡"); - - if (pageNo == totalPage) { + Button nextButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "1"), "➡"); + if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); } - return nextButton; + return ActionRow.of(previousButton, nextButton); } @Override public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { - event - .editMessage(auditUser(Long.parseLong(args.get(0)), Long.parseLong(args.get(1)), - Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)), event.getJDA())) - .queue(); + long guildId = Long.parseLong(args.get(0)); + long targetId = Long.parseLong(args.get(1)); + int pageNumber = Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)); + + event.editMessage(auditUser(guildId, targetId, pageNumber, event.getJDA())).queue(); } } From 37071b605c2837fc6012d577e8e1bd24f1575279 Mon Sep 17 00:00:00 2001 From: Taz Date: Thu, 28 Jul 2022 20:11:02 +0530 Subject: [PATCH 13/20] reformated code --- .../tjbot/commands/moderation/AuditCommand.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index df2c918fdf..9b25daad64 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -159,7 +159,8 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return groupedActions; } - private @NotNull Message auditUser(long guildId, long targetId, int pageNumber, @NotNull JDA jda) { + private @NotNull Message auditUser(long guildId, long targetId, int pageNumber, + @NotNull JDA jda) { List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); List> groupedActions = groupActions(actions); int totalPages = groupedActions.size(); @@ -178,17 +179,20 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, .build(); } - private @NotNull ActionRow makeActionRow(long guildId, long targetId, int pageNumber, int totalPages) { + private @NotNull ActionRow makeActionRow(long guildId, long targetId, int pageNumber, + int totalPages) { String stringGuildId = String.valueOf(guildId); String stringTargetId = String.valueOf(targetId); String stringPageNumber = String.valueOf(pageNumber); - Button previousButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "-1"), "⬅"); + Button previousButton = Button.primary( + generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "-1"), "⬅"); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } - Button nextButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "1"), "➡"); + Button nextButton = Button.primary( + generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "1"), "➡"); if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); } From d74170283b281220669ed0e837c9c61dc1c9562b Mon Sep 17 00:00:00 2001 From: Taz Date: Fri, 29 Jul 2022 01:03:25 +0530 Subject: [PATCH 14/20] Only the user who triggered the command can use the buttons --- .../commands/moderation/AuditCommand.java | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 9b25daad64..1054d74e72 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -132,7 +132,10 @@ public void onSlashCommand(@NotNull SlashCommandInteractionEvent event) { return; } - event.reply(auditUser(guild.getIdLong(), target.getIdLong(), 1, event.getJDA())).queue(); + event + .reply(auditUser(guild.getIdLong(), target.getIdLong(), event.getMember().getIdLong(), + 1, event.getJDA())) + .queue(); } @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") @@ -159,7 +162,7 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return groupedActions; } - private @NotNull Message auditUser(long guildId, long targetId, int pageNumber, + private @NotNull Message auditUser(long guildId, long targetId, long callerId, int pageNumber, @NotNull JDA jda) { List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); List> groupedActions = groupActions(actions); @@ -175,24 +178,25 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, .forEach(action -> audit.addField(actionToField(action, jda))); return new MessageBuilder(audit.setFooter("Page: " + pageNumber + "/" + totalPages).build()) - .setActionRows(makeActionRow(guildId, targetId, pageNumber, totalPages)) + .setActionRows(makeActionRow(guildId, targetId, callerId, pageNumber, totalPages)) .build(); } - private @NotNull ActionRow makeActionRow(long guildId, long targetId, int pageNumber, - int totalPages) { + private @NotNull ActionRow makeActionRow(long guildId, long targetId, long callerId, + int pageNumber, int totalPages) { String stringGuildId = String.valueOf(guildId); String stringTargetId = String.valueOf(targetId); + String stringCallerId = String.valueOf(callerId); String stringPageNumber = String.valueOf(pageNumber); - Button previousButton = Button.primary( - generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "-1"), "⬅"); + Button previousButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, + stringCallerId, stringPageNumber, "-1"), "⬅"); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } - Button nextButton = Button.primary( - generateComponentId(stringGuildId, stringTargetId, stringPageNumber, "1"), "➡"); + Button nextButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, + stringCallerId, stringPageNumber, "1"), "➡"); if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); } @@ -202,10 +206,21 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, @Override public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { - long guildId = Long.parseLong(args.get(0)); - long targetId = Long.parseLong(args.get(1)); - int pageNumber = Integer.parseInt(args.get(2)) + Integer.parseInt(args.get(3)); - - event.editMessage(auditUser(guildId, targetId, pageNumber, event.getJDA())).queue(); + long callerId = Long.parseLong(args.get(2)); + long interactorId = event.getMember().getIdLong(); + + if (callerId == interactorId) { + long guildId = Long.parseLong(args.get(0)); + long targetId = Long.parseLong(args.get(1)); + int pageNumber = Integer.parseInt(args.get(3)) + Integer.parseInt(args.get(4)); + + event + .editMessage(auditUser(guildId, targetId, interactorId, pageNumber, event.getJDA())) + .queue(); + } else { + event.reply("Only the user who triggered the command can use these buttons.") + .setEphemeral(true) + .queue(); + } } } From 4027cf70635ae59be6094e7f5d53e7e18848ef16 Mon Sep 17 00:00:00 2001 From: Taz Date: Fri, 29 Jul 2022 14:06:00 +0530 Subject: [PATCH 15/20] added checks for too high and too low page numbers --- .../commands/moderation/AuditCommand.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 1054d74e72..c8a76711c7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -45,7 +45,7 @@ public final class AuditCommand extends SlashCommandAdapter { /** * Constructs an instance. - * + * * @param actionsStore used to store actions issued by this command * @param config the config to use for this */ @@ -168,6 +168,10 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, List> groupedActions = groupActions(actions); int totalPages = groupedActions.size(); + // Handles the case of too low page number and too high page number + pageNumber = Math.max(1, pageNumber); + pageNumber = Math.min(totalPages, pageNumber); + EmbedBuilder audit = createSummaryEmbed(jda.retrieveUserById(targetId).complete(), actions); if (groupedActions.isEmpty()) { @@ -189,14 +193,16 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, String stringCallerId = String.valueOf(callerId); String stringPageNumber = String.valueOf(pageNumber); + String previousButtonTurnPageBy = "-1"; Button previousButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, - stringCallerId, stringPageNumber, "-1"), "⬅"); + stringCallerId, stringPageNumber, previousButtonTurnPageBy), "⬅"); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } + String nextButtonTurnPageBy = "1"; Button nextButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, - stringCallerId, stringPageNumber, "1"), "➡"); + stringCallerId, stringPageNumber, nextButtonTurnPageBy), "➡"); if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); } @@ -210,12 +216,16 @@ public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List Date: Fri, 29 Jul 2022 14:59:30 +0530 Subject: [PATCH 16/20] made code more understandable --- .../commands/moderation/AuditCommand.java | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index c8a76711c7..96132c6750 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -149,7 +149,8 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event); } - private @NotNull List> groupActions(@NotNull List actions) { + private @NotNull List> groupActionsByPages( + @NotNull List actions) { List> groupedActions = new ArrayList<>(); for (int i = 0; i < actions.size(); i++) { if (i % AuditCommand.MAX_PAGE_LENGTH == 0) { @@ -162,10 +163,14 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return groupedActions; } + /** + * @param pageNumber page number to display when actions are divided into pages and each page + * can contain {@link AuditCommand#MAX_PAGE_LENGTH} actions + */ private @NotNull Message auditUser(long guildId, long targetId, long callerId, int pageNumber, @NotNull JDA jda) { List actions = actionsStore.getActionsByTargetAscending(guildId, targetId); - List> groupedActions = groupActions(actions); + List> groupedActions = groupActionsByPages(actions); int totalPages = groupedActions.size(); // Handles the case of too low page number and too high page number @@ -188,21 +193,20 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, private @NotNull ActionRow makeActionRow(long guildId, long targetId, long callerId, int pageNumber, int totalPages) { - String stringGuildId = String.valueOf(guildId); - String stringTargetId = String.valueOf(targetId); - String stringCallerId = String.valueOf(callerId); - String stringPageNumber = String.valueOf(pageNumber); - String previousButtonTurnPageBy = "-1"; - Button previousButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, - stringCallerId, stringPageNumber, previousButtonTurnPageBy), "⬅"); + Button previousButton = Button + .primary(generateComponentId(String.valueOf(guildId), String.valueOf(targetId), + String.valueOf(callerId), String.valueOf(pageNumber), previousButtonTurnPageBy), + "⬅"); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } String nextButtonTurnPageBy = "1"; - Button nextButton = Button.primary(generateComponentId(stringGuildId, stringTargetId, - stringCallerId, stringPageNumber, nextButtonTurnPageBy), "➡"); + Button nextButton = Button.primary( + generateComponentId(String.valueOf(guildId), String.valueOf(targetId), + String.valueOf(callerId), String.valueOf(pageNumber), nextButtonTurnPageBy), + "➡"); if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); } @@ -215,22 +219,20 @@ public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List Date: Fri, 29 Jul 2022 15:07:07 +0530 Subject: [PATCH 17/20] added return --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 96132c6750..b0fadf6e25 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -223,6 +223,8 @@ public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List Date: Fri, 29 Jul 2022 20:22:28 +0530 Subject: [PATCH 18/20] added new method for creating button --- .../commands/moderation/AuditCommand.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index b0fadf6e25..1649ee75e9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -40,6 +40,8 @@ public final class AuditCommand extends SlashCommandAdapter { private static final String COMMAND_NAME = "audit"; private static final String ACTION_VERB = "audit"; private static final int MAX_PAGE_LENGTH = 25; + private static final String PREVIOUS_BUTTON = "⬅"; + private static final String NEXT_BUTTON = "➡"; private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; @@ -193,20 +195,16 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, private @NotNull ActionRow makeActionRow(long guildId, long targetId, long callerId, int pageNumber, int totalPages) { - String previousButtonTurnPageBy = "-1"; - Button previousButton = Button - .primary(generateComponentId(String.valueOf(guildId), String.valueOf(targetId), - String.valueOf(callerId), String.valueOf(pageNumber), previousButtonTurnPageBy), - "⬅"); + int previousButtonTurnPageBy = -1; + Button previousButton = createPageTurnButton(PREVIOUS_BUTTON, guildId, targetId, callerId, + pageNumber, previousButtonTurnPageBy); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } - String nextButtonTurnPageBy = "1"; - Button nextButton = Button.primary( - generateComponentId(String.valueOf(guildId), String.valueOf(targetId), - String.valueOf(callerId), String.valueOf(pageNumber), nextButtonTurnPageBy), - "➡"); + int nextButtonTurnPageBy = +1; + Button nextButton = createPageTurnButton(NEXT_BUTTON, guildId, targetId, callerId, + pageNumber, nextButtonTurnPageBy); if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); } @@ -214,6 +212,13 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, return ActionRow.of(previousButton, nextButton); } + private @NotNull Button createPageTurnButton(@NotNull String label, long guildId, long targetId, + long callerId, long pageNumber, int turnPageBy) { + return Button.primary(generateComponentId(String.valueOf(guildId), String.valueOf(targetId), + String.valueOf(callerId), String.valueOf(pageNumber), String.valueOf(turnPageBy)), + label); + } + @Override public void onButtonClick(@NotNull ButtonInteractionEvent event, @NotNull List args) { long callerId = Long.parseLong(args.get(2)); From ef1141183306e93307f1b49431246931b0d9da23 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 1 Aug 2022 17:01:18 +0530 Subject: [PATCH 19/20] minor changes --- .../tjbot/commands/moderation/AuditCommand.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 1649ee75e9..092badfb57 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -40,8 +40,8 @@ public final class AuditCommand extends SlashCommandAdapter { private static final String COMMAND_NAME = "audit"; private static final String ACTION_VERB = "audit"; private static final int MAX_PAGE_LENGTH = 25; - private static final String PREVIOUS_BUTTON = "⬅"; - private static final String NEXT_BUTTON = "➡"; + private static final String PREVIOUS_BUTTON_LABEL = "⬅"; + private static final String NEXT_BUTTON_LABEL = "➡"; private final Predicate hasRequiredRole; private final ModerationActionsStore actionsStore; @@ -196,14 +196,14 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, private @NotNull ActionRow makeActionRow(long guildId, long targetId, long callerId, int pageNumber, int totalPages) { int previousButtonTurnPageBy = -1; - Button previousButton = createPageTurnButton(PREVIOUS_BUTTON, guildId, targetId, callerId, + Button previousButton = createPageTurnButton(PREVIOUS_BUTTON_LABEL, guildId, targetId, callerId, pageNumber, previousButtonTurnPageBy); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); } - int nextButtonTurnPageBy = +1; - Button nextButton = createPageTurnButton(NEXT_BUTTON, guildId, targetId, callerId, + int nextButtonTurnPageBy = 1; + Button nextButton = createPageTurnButton(NEXT_BUTTON_LABEL, guildId, targetId, callerId, pageNumber, nextButtonTurnPageBy); if (pageNumber == totalPages) { nextButton = nextButton.asDisabled(); From dbc134c64568d6a0ad02e97d218ee5d6a0698d18 Mon Sep 17 00:00:00 2001 From: Taz Date: Mon, 1 Aug 2022 17:12:43 +0530 Subject: [PATCH 20/20] reformat code --- .../togetherjava/tjbot/commands/moderation/AuditCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java index 092badfb57..6097286232 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/AuditCommand.java @@ -196,8 +196,8 @@ private boolean handleChecks(@NotNull Member bot, @NotNull Member author, private @NotNull ActionRow makeActionRow(long guildId, long targetId, long callerId, int pageNumber, int totalPages) { int previousButtonTurnPageBy = -1; - Button previousButton = createPageTurnButton(PREVIOUS_BUTTON_LABEL, guildId, targetId, callerId, - pageNumber, previousButtonTurnPageBy); + Button previousButton = createPageTurnButton(PREVIOUS_BUTTON_LABEL, guildId, targetId, + callerId, pageNumber, previousButtonTurnPageBy); if (pageNumber == 1) { previousButton = previousButton.asDisabled(); }