Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new GitHubCommand(githubReference));
features.add(new ModMailCommand(jda, config));
features.add(new HelpThreadCommand(config, helpSystemHelper, metrics));
features.add(new ReportCommand(config));
features.add(new ReportCommand(config, actionsStore));
features.add(new BookmarksCommand(bookmarksSystem));

features.add(new ChatGptCommand(chatGptService, helpSystemHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -25,6 +26,8 @@
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.moderation.audit.AuditCommand;
import org.togetherjava.tjbot.features.utils.MessageUtils;

import java.awt.Color;
Expand Down Expand Up @@ -53,15 +56,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();

Expand Down Expand Up @@ -182,9 +190,12 @@ private MessageCreateAction createModMessage(String reportReason,
.setColor(AMBIENT_COLOR)
.build();

String historyButtonId = generateComponentId(Lifespan.REGULAR, reportedMessage.authorId);

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()
Expand Down Expand Up @@ -222,7 +233,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);
Expand All @@ -231,8 +242,58 @@ 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().setEphemeral(true).queue();

long guildId = event.getGuild().getIdLong();
long reportedUserId = Long.parseLong(args.getFirst());

List<ActionRecord> actions =
Comment thread
modwodmm marked this conversation as resolved.
Outdated
moderationActionsStore.getActionsByTargetAscending(guildId, reportedUserId);

event.getJDA().retrieveUserById(reportedUserId).queue(user -> {
EmbedBuilder auditEmbed =
new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getName()))
.setAuthor(user.getName(), null, user.getEffectiveAvatarUrl())
.setColor(Color.BLACK)
.setDescription(AuditCommand.createSummaryMessageDescription(actions));

List<List<ActionRecord>> pages = AuditCommand.groupActionsByPages(actions);

if (pages.isEmpty()) {
event.getHook().sendMessageEmbeds(auditEmbed.build()).queue();
return;
}

List<net.dv8tion.jda.api.requests.RestAction<MessageEmbed.Field>> fieldTasks =
pages.getLast()
.stream()
.map(action -> AuditCommand.actionToField(action, event.getJDA()))
.toList();

net.dv8tion.jda.api.requests.RestAction.allOf(fieldTasks).queue(fields -> {
fields.forEach(auditEmbed::addField);

if (pages.size() > 1) {
auditEmbed.setFooter(
"Showing page %d/%d (Most recent actions). Use /audit to view full history."
.formatted(pages.size(), pages.size()));
}

event.getHook().sendMessageEmbeds(auditEmbed.build()).queue();
}, throwable -> event.getHook()
.sendMessage("Could not load moderation history fields.")
.queue());
}, throwable -> event.getHook()
.sendMessage("Could not retrieve audit data for this user.")
.queue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private <R extends MessageRequest<R>> RestAction<R> auditUser(
pageNumberInLimits, totalPages, guildId, targetId, callerId));
}

private List<List<ActionRecord>> groupActionsByPages(List<ActionRecord> actions) {
public static List<List<ActionRecord>> groupActionsByPages(List<ActionRecord> actions) {
List<List<ActionRecord>> groupedActions = new ArrayList<>();
for (int i = 0; i < actions.size(); i++) {
if (i % AuditCommand.MAX_PAGE_LENGTH == 0) {
Expand All @@ -148,7 +148,7 @@ private static EmbedBuilder createSummaryEmbed(User user, Collection<ActionRecor
.setColor(ModerationUtils.AMBIENT_COLOR);
}

private static String createSummaryMessageDescription(Collection<ActionRecord> actions) {
public static String createSummaryMessageDescription(Collection<ActionRecord> actions) {
int actionAmount = actions.size();

String shortSummary = "There are **%s actions** against the user."
Expand Down Expand Up @@ -192,7 +192,7 @@ private RestAction<EmbedBuilder> attachEmbedFields(EmbedBuilder auditEmbed,
});
}

private static RestAction<MessageEmbed.Field> actionToField(ActionRecord action, JDA jda) {
public static RestAction<MessageEmbed.Field> actionToField(ActionRecord action, JDA jda) {
return jda.retrieveUserById(action.authorId())
.map(author -> author == null ? "(unknown user)" : author.getName())
.map(authorText -> {
Expand Down
Loading