|
| 1 | +package org.togetherjava.tjbot.commands.moderation; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 4 | +import net.dv8tion.jda.api.JDA; |
| 5 | +import net.dv8tion.jda.api.entities.*; |
| 6 | +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
| 7 | +import net.dv8tion.jda.api.interactions.Interaction; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 10 | +import net.dv8tion.jda.api.requests.RestAction; |
| 11 | +import net.dv8tion.jda.api.utils.TimeUtil; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | +import org.togetherjava.tjbot.commands.SlashCommandAdapter; |
| 15 | +import org.togetherjava.tjbot.commands.SlashCommandVisibility; |
| 16 | +import org.togetherjava.tjbot.config.Config; |
| 17 | + |
| 18 | +import java.time.ZoneOffset; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.function.Predicate; |
| 24 | +import java.util.regex.Pattern; |
| 25 | + |
| 26 | +/** |
| 27 | + * This command lists all moderation actions that have been taken against a given user, for example |
| 28 | + * warnings, mutes and bans. |
| 29 | + * <p> |
| 30 | + * The command fails if the user triggering it is lacking permissions to either audit other users or |
| 31 | + * to audit the specific given user (for example a moderator attempting to audit an admin). |
| 32 | + */ |
| 33 | +public final class AuditCommand extends SlashCommandAdapter { |
| 34 | + private static final String TARGET_OPTION = "user"; |
| 35 | + private static final String COMMAND_NAME = "audit"; |
| 36 | + private static final String ACTION_VERB = "audit"; |
| 37 | + private final Predicate<String> hasRequiredRole; |
| 38 | + private final ModerationActionsStore actionsStore; |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructs an instance. |
| 42 | + * |
| 43 | + * @param actionsStore used to store actions issued by this command |
| 44 | + */ |
| 45 | + public AuditCommand(@NotNull ModerationActionsStore actionsStore) { |
| 46 | + super(COMMAND_NAME, "Lists all moderation actions that have been taken against a user", |
| 47 | + SlashCommandVisibility.GUILD); |
| 48 | + |
| 49 | + getData().addOption(OptionType.USER, TARGET_OPTION, "The user who to retrieve actions for", |
| 50 | + true); |
| 51 | + |
| 52 | + hasRequiredRole = Pattern.compile(Config.getInstance().getHeavyModerationRolePattern()) |
| 53 | + .asMatchPredicate(); |
| 54 | + this.actionsStore = Objects.requireNonNull(actionsStore); |
| 55 | + } |
| 56 | + |
| 57 | + private static MessageEmbed createSummaryMessage(@NotNull User user, |
| 58 | + @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 | + |
| 63 | + return new EmbedBuilder().setTitle("Audit log of **%s**".formatted(user.getAsTag())) |
| 64 | + .setAuthor(user.getName(), null, user.getAvatarUrl()) |
| 65 | + .setDescription(description) |
| 66 | + .setColor(ModerationUtils.AMBIENT_COLOR) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + private static RestAction<MessageEmbed> actionToMessage(@NotNull ActionRecord action, |
| 71 | + @NotNull JDA jda) { |
| 72 | + String footer = action.actionExpiresAt() == null ? null |
| 73 | + : "Temporary action, expires at %s".formatted(TimeUtil |
| 74 | + .getDateTimeString(action.actionExpiresAt().atOffset(ZoneOffset.UTC))); |
| 75 | + |
| 76 | + return jda.retrieveUserById(action.authorId()) |
| 77 | + .map(author -> new EmbedBuilder().setTitle(action.actionType().name()) |
| 78 | + .setAuthor(author == null ? "(unknown user)" : author.getAsTag(), null, |
| 79 | + author == null ? null : author.getAvatarUrl()) |
| 80 | + .setDescription(action.reason()) |
| 81 | + .setTimestamp(action.issuedAt()) |
| 82 | + .setFooter(footer) |
| 83 | + .setColor(ModerationUtils.AMBIENT_COLOR) |
| 84 | + .build()); |
| 85 | + } |
| 86 | + |
| 87 | + private static <E> List<E> prependElement(E element, Collection<? extends E> elements) { |
| 88 | + List<E> allElements = new ArrayList<>(elements.size() + 1); |
| 89 | + allElements.add(element); |
| 90 | + allElements.addAll(elements); |
| 91 | + return allElements; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onSlashCommand(@NotNull SlashCommandEvent event) { |
| 96 | + OptionMapping targetOption = |
| 97 | + Objects.requireNonNull(event.getOption(TARGET_OPTION), "The target is null"); |
| 98 | + User target = targetOption.getAsUser(); |
| 99 | + Member author = Objects.requireNonNull(event.getMember(), "The author is null"); |
| 100 | + |
| 101 | + Guild guild = Objects.requireNonNull(event.getGuild()); |
| 102 | + Member bot = guild.getSelfMember(); |
| 103 | + |
| 104 | + if (!handleChecks(bot, author, targetOption.getAsMember(), guild, event)) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + auditUser(target, guild, event); |
| 109 | + } |
| 110 | + |
| 111 | + @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") |
| 112 | + private boolean handleChecks(@NotNull Member bot, @NotNull Member author, |
| 113 | + @Nullable Member target, @NotNull Guild guild, @NotNull Interaction event) { |
| 114 | + // Member doesn't exist if attempting to audit a user who is not part of the guild. |
| 115 | + if (target != null && !ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, |
| 116 | + target, event)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + return ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event); |
| 120 | + } |
| 121 | + |
| 122 | + private void auditUser(@NotNull User user, @NotNull ISnowflake guild, |
| 123 | + @NotNull Interaction event) { |
| 124 | + List<ActionRecord> actions = |
| 125 | + actionsStore.getActionsByTargetAscending(guild.getIdLong(), user.getIdLong()); |
| 126 | + |
| 127 | + MessageEmbed summary = createSummaryMessage(user, actions); |
| 128 | + if (actions.isEmpty()) { |
| 129 | + event.replyEmbeds(summary).queue(); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + // Computing messages for actual actions is done deferred and might require asking the |
| 134 | + // Discord API |
| 135 | + event.deferReply().queue(); |
| 136 | + JDA jda = event.getJDA(); |
| 137 | + |
| 138 | + RestAction<List<MessageEmbed>> messagesTask = RestAction |
| 139 | + .allOf(actions.stream().map(action -> actionToMessage(action, jda)).toList()); |
| 140 | + messagesTask.map(messages -> prependElement(summary, messages)) |
| 141 | + .flatMap(messages -> event.getHook().sendMessageEmbeds(messages)) |
| 142 | + .queue(); |
| 143 | + } |
| 144 | +} |
0 commit comments