-
-
Notifications
You must be signed in to change notification settings - Fork 116
Added /audit button to reports (moderation) #1499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 12 commits
b4a2d5e
27d66db
4b9b662
ece6dbf
9c71eca
940e390
9681b28
3def321
6e1805f
c919cd9
7ab5a46
9804c59
5cea235
189463b
2e2336d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,19 +3,18 @@ | |
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import net.dv8tion.jda.api.EmbedBuilder; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||
| import net.dv8tion.jda.api.entities.Role; | ||
| import net.dv8tion.jda.api.entities.*; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Star import |
||
| import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
| import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; | ||
| import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent; | ||
| import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; | ||
| import net.dv8tion.jda.api.interactions.InteractionHook; | ||
| import net.dv8tion.jda.api.interactions.commands.build.Commands; | ||
| import net.dv8tion.jda.api.interactions.components.buttons.Button; | ||
| import net.dv8tion.jda.api.interactions.components.text.TextInput; | ||
| import net.dv8tion.jda.api.interactions.components.text.TextInputStyle; | ||
| import net.dv8tion.jda.api.interactions.modals.Modal; | ||
| import net.dv8tion.jda.api.requests.RestAction; | ||
| import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | ||
| import net.dv8tion.jda.api.utils.Result; | ||
| import org.slf4j.Logger; | ||
|
|
@@ -25,14 +24,13 @@ | |
| import org.togetherjava.tjbot.features.BotCommandAdapter; | ||
| import org.togetherjava.tjbot.features.CommandVisibility; | ||
| import org.togetherjava.tjbot.features.MessageContextCommand; | ||
| import org.togetherjava.tjbot.features.componentids.Lifespan; | ||
| import org.togetherjava.tjbot.features.utils.MessageUtils; | ||
|
|
||
| import java.awt.Color; | ||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.*; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Star import |
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Predicate; | ||
| import java.util.regex.Pattern; | ||
|
|
@@ -53,15 +51,20 @@ public final class ReportCommand extends BotCommandAdapter implements MessageCon | |
| private final Predicate<String> modMailChannelNamePredicate; | ||
| private final Predicate<String> configModGroupPattern; | ||
| private final String configModMailChannelPattern; | ||
| private final ModerationActionsStore moderationActionsStore; | ||
|
|
||
| /** | ||
| * Creates a new instance. | ||
| * | ||
| * @param config to get the channel to forward reports to | ||
| * @param moderationActionsStore to get the history of moderation actions against the reported | ||
| * user | ||
| */ | ||
| public ReportCommand(Config config) { | ||
| public ReportCommand(Config config, ModerationActionsStore moderationActionsStore) { | ||
| super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); | ||
|
|
||
| this.moderationActionsStore = Objects.requireNonNull(moderationActionsStore); | ||
|
|
||
| modMailChannelNamePredicate = | ||
| Pattern.compile(config.getModMailChannelPattern()).asMatchPredicate(); | ||
|
|
||
|
|
@@ -182,9 +185,13 @@ private MessageCreateAction createModMessage(String reportReason, | |
| .setColor(AMBIENT_COLOR) | ||
| .build(); | ||
|
|
||
| String historyButtonId = | ||
| generateComponentId(Lifespan.REGULAR, reportedMessage.authorId, "0"); | ||
|
|
||
| MessageCreateAction message = | ||
| modMailAuditLog.sendMessageEmbeds(reportedMessageEmbed, reportReasonEmbed) | ||
| .addActionRow(Button.link(reportedMessage.jumpUrl, "Go to message")); | ||
| .addActionRow(Button.link(reportedMessage.jumpUrl, "Go to message"), | ||
| Button.primary(historyButtonId, "Audit")); | ||
|
|
||
| Optional<Role> moderatorRole = guild.getRoles() | ||
| .stream() | ||
|
|
@@ -222,7 +229,7 @@ private static String createUserReply(Result<Message> result) { | |
| } | ||
|
|
||
| private record ReportedMessage(String content, String id, String jumpUrl, String channelID, | ||
| Instant timestamp, String authorName, String authorAvatarUrl) { | ||
| Instant timestamp, String authorName, String authorAvatarUrl, String authorId) { | ||
| static ReportedMessage ofArgs(List<String> args) { | ||
| String content = args.getFirst(); | ||
| String id = args.get(1); | ||
|
|
@@ -231,8 +238,92 @@ static ReportedMessage ofArgs(List<String> args) { | |
| Instant timestamp = Instant.parse(args.get(4)); | ||
| String authorName = args.get(5); | ||
| String authorAvatarUrl = args.get(6); | ||
| String authorId = args.get(7); | ||
| return new ReportedMessage(content, id, jumpUrl, channelID, timestamp, authorName, | ||
| authorAvatarUrl); | ||
| authorAvatarUrl, authorId); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onButtonClick(ButtonInteractionEvent event, List<String> args) { | ||
| event.deferReply(true).queue(); | ||
|
|
||
| Guild guild = | ||
| Objects.requireNonNull(event.getGuild(), "Guild cannot be null for this command."); | ||
| long guildId = guild.getIdLong(); | ||
|
|
||
| long reportedUserId = Long.parseLong(args.get(0)); | ||
| int targetPage = Integer.parseInt(args.get(1)); | ||
|
|
||
| List<ActionRecord> actions = new ArrayList<>( | ||
| moderationActionsStore.getActionsByTargetAscending(guildId, reportedUserId)); | ||
| Collections.reverse(actions); | ||
| List<List<ActionRecord>> pages = ModerationUtils.groupActionsByPages(actions); | ||
|
|
||
| event.getJDA() | ||
| .retrieveUserById(reportedUserId) | ||
| .flatMap(user -> prepareAuditEmbedTasks(event, user, actions, pages, targetPage)) | ||
| .onErrorFlatMap(_ -> event.getHook() | ||
| .sendMessage("Could not load audit data for this user.") | ||
| .map(_ -> null)) | ||
| .queue(); | ||
| } | ||
|
|
||
| private RestAction<List<MessageEmbed.Field>> prepareAuditEmbedTasks( | ||
| ButtonInteractionEvent event, User user, List<ActionRecord> actions, | ||
| List<List<ActionRecord>> pages, int targetPage) { | ||
|
|
||
| EmbedBuilder auditEmbed = | ||
| new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getName())) | ||
| .setAuthor(user.getName(), null, user.getEffectiveAvatarUrl()) | ||
| .setColor(Color.BLACK) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make use of the recently introduced |
||
| .setDescription(ModerationUtils.createSummaryMessageDescription(actions)); | ||
|
|
||
| if (pages.isEmpty()) { | ||
| return event.getHook() | ||
| .editOriginalEmbeds(auditEmbed.build()) | ||
| .setComponents(List.of()) | ||
| .map(_ -> List.of()); | ||
| } | ||
|
|
||
| int currentPageIndex = Math.clamp(targetPage, 0, pages.size() - 1); | ||
|
|
||
| List<RestAction<MessageEmbed.Field>> fetchFieldActions = pages.get(currentPageIndex) | ||
| .stream() | ||
| .map(actionRecord -> ModerationUtils.moderationActionToEmbedField(actionRecord, | ||
| event.getJDA())) | ||
| .toList(); | ||
|
|
||
| return RestAction.allOf(fetchFieldActions).map(embedFields -> { | ||
| finalizeAndSendEmbed(event, auditEmbed, embedFields, user.getIdLong(), currentPageIndex, | ||
| pages.size()); | ||
| return embedFields; | ||
| }); | ||
|
Zabuzard marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private void finalizeAndSendEmbed(ButtonInteractionEvent event, EmbedBuilder auditEmbed, | ||
| List<MessageEmbed.Field> embedFields, long reportedUserId, int currentPageIndex, | ||
| int totalPages) { | ||
| auditEmbed.clearFields(); | ||
| embedFields.forEach(auditEmbed::addField); | ||
|
|
||
| auditEmbed.setFooter( | ||
| "Page %d/%d (Most recent first)".formatted(currentPageIndex + 1, totalPages)); | ||
|
|
||
| String prevButtonId = generateComponentId(Lifespan.REGULAR, String.valueOf(reportedUserId), | ||
| String.valueOf(currentPageIndex - 1)); | ||
| String nextButtonId = generateComponentId(Lifespan.REGULAR, String.valueOf(reportedUserId), | ||
| String.valueOf(currentPageIndex + 1)); | ||
|
|
||
| Button prevButton = | ||
| Button.primary(prevButtonId, "◀ Previous").withDisabled(currentPageIndex == 0); | ||
| Button nextButton = Button.primary(nextButtonId, "Next ▶") | ||
| .withDisabled(currentPageIndex == totalPages - 1); | ||
|
|
||
| event.getHook() | ||
| .editOriginalEmbeds(auditEmbed.build()) | ||
| .setActionRow(prevButton, nextButton) | ||
| .queue(); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: The "markdown" word should be capitalized, considering you are referring to the formatting language.