|
| 1 | +package org.togetherjava.tjbot.commands.moderation; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.*; |
| 4 | +import net.dv8tion.jda.api.events.GenericEvent; |
| 5 | +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; |
| 6 | +import net.dv8tion.jda.api.interactions.Interaction; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 8 | +import net.dv8tion.jda.api.requests.RestAction; |
| 9 | +import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; |
| 10 | +import net.dv8tion.jda.api.utils.Result; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.togetherjava.tjbot.commands.SlashCommandAdapter; |
| 16 | +import org.togetherjava.tjbot.commands.SlashCommandVisibility; |
| 17 | +import org.togetherjava.tjbot.config.Config; |
| 18 | + |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.function.Predicate; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +/** |
| 24 | + * This command can quarantine users. Quarantining can also be paired with a reason. The command |
| 25 | + * will also try to DM the user to inform them about the action and the reason. |
| 26 | + * <p> |
| 27 | + * The command fails if the user triggering it is lacking permissions to either quarantine other |
| 28 | + * users or to quarantine the specific given user (for example a moderator attempting to quarantine |
| 29 | + * an admin). |
| 30 | + */ |
| 31 | +public final class QuarantineCommand extends SlashCommandAdapter { |
| 32 | + private static final Logger logger = LoggerFactory.getLogger(QuarantineCommand.class); |
| 33 | + private static final String TARGET_OPTION = "user"; |
| 34 | + private static final String REASON_OPTION = "reason"; |
| 35 | + private static final String COMMAND_NAME = "quarantine"; |
| 36 | + private static final String ACTION_VERB = "quarantine"; |
| 37 | + private final Predicate<String> hasRequiredRole; |
| 38 | + private final ModerationActionsStore actionsStore; |
| 39 | + private final Config config; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs an instance. |
| 43 | + * |
| 44 | + * @param actionsStore used to store actions issued by this command |
| 45 | + * @param config the config to use for this |
| 46 | + */ |
| 47 | + public QuarantineCommand(@NotNull ModerationActionsStore actionsStore, @NotNull Config config) { |
| 48 | + super(COMMAND_NAME, |
| 49 | + "Puts the given user under quarantine. They can not interact with anyone anymore then.", |
| 50 | + SlashCommandVisibility.GUILD); |
| 51 | + |
| 52 | + getData() |
| 53 | + .addOption(OptionType.USER, TARGET_OPTION, "The user who you want to quarantine", true) |
| 54 | + .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be quarantined", |
| 55 | + true); |
| 56 | + |
| 57 | + this.config = config; |
| 58 | + hasRequiredRole = Pattern.compile(config.getSoftModerationRolePattern()).asMatchPredicate(); |
| 59 | + this.actionsStore = Objects.requireNonNull(actionsStore); |
| 60 | + } |
| 61 | + |
| 62 | + private static void handleAlreadyQuarantinedTarget(@NotNull Interaction event) { |
| 63 | + event.reply("The user is already quarantined.").setEphemeral(true).queue(); |
| 64 | + } |
| 65 | + |
| 66 | + private static RestAction<Boolean> sendDm(@NotNull ISnowflake target, @NotNull String reason, |
| 67 | + @NotNull Guild guild, @NotNull GenericEvent event) { |
| 68 | + String dmMessage = |
| 69 | + """ |
| 70 | + Hey there, sorry to tell you but unfortunately you have been put under quarantine in the server %s. |
| 71 | + This means you can no longer interact with anyone in the server until you have been unquarantined again. |
| 72 | + If you think this was a mistake, or the reason no longer applies, please contact a moderator or admin of the server. |
| 73 | + The reason for the quarantine is: %s |
| 74 | + """ |
| 75 | + .formatted(guild.getName(), reason); |
| 76 | + |
| 77 | + return event.getJDA() |
| 78 | + .openPrivateChannelById(target.getIdLong()) |
| 79 | + .flatMap(channel -> channel.sendMessage(dmMessage)) |
| 80 | + .mapToResult() |
| 81 | + .map(Result::isSuccess); |
| 82 | + } |
| 83 | + |
| 84 | + private static @NotNull MessageEmbed sendFeedback(boolean hasSentDm, @NotNull Member target, |
| 85 | + @NotNull Member author, @NotNull String reason) { |
| 86 | + String dmNoticeText = ""; |
| 87 | + if (!hasSentDm) { |
| 88 | + dmNoticeText = "\n(Unable to send them a DM.)"; |
| 89 | + } |
| 90 | + return ModerationUtils.createActionResponse(author.getUser(), ModerationAction.QUARANTINE, |
| 91 | + target.getUser(), dmNoticeText, reason); |
| 92 | + } |
| 93 | + |
| 94 | + private AuditableRestAction<Void> quarantineUser(@NotNull Member target, @NotNull Member author, |
| 95 | + @NotNull String reason, @NotNull Guild guild) { |
| 96 | + logger.info("'{}' ({}) quarantined the user '{}' ({}) in guild '{}' for reason '{}'.", |
| 97 | + author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), |
| 98 | + target.getId(), guild.getName(), reason); |
| 99 | + |
| 100 | + actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(), |
| 101 | + ModerationAction.QUARANTINE, null, reason); |
| 102 | + |
| 103 | + return guild |
| 104 | + .addRoleToMember(target, |
| 105 | + ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) |
| 106 | + .reason(reason); |
| 107 | + } |
| 108 | + |
| 109 | + private void quarantineUserFlow(@NotNull Member target, @NotNull Member author, |
| 110 | + @NotNull String reason, @NotNull Guild guild, @NotNull SlashCommandEvent event) { |
| 111 | + sendDm(target, reason, guild, event) |
| 112 | + .flatMap(hasSentDm -> quarantineUser(target, author, reason, guild) |
| 113 | + .map(result -> hasSentDm)) |
| 114 | + .map(hasSentDm -> sendFeedback(hasSentDm, target, author, reason)) |
| 115 | + .flatMap(event::replyEmbeds) |
| 116 | + .queue(); |
| 117 | + } |
| 118 | + |
| 119 | + @SuppressWarnings({"BooleanMethodNameMustStartWithQuestion", "MethodWithTooManyParameters"}) |
| 120 | + private boolean handleChecks(@NotNull Member bot, @NotNull Member author, |
| 121 | + @Nullable Member target, @NotNull CharSequence reason, @NotNull Guild guild, |
| 122 | + @NotNull Interaction event) { |
| 123 | + if (!ModerationUtils.handleRoleChangeChecks( |
| 124 | + ModerationUtils.getQuarantinedRole(guild, config).orElse(null), ACTION_VERB, target, |
| 125 | + bot, author, guild, hasRequiredRole, reason, event)) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if (Objects.requireNonNull(target) |
| 130 | + .getRoles() |
| 131 | + .stream() |
| 132 | + .map(Role::getName) |
| 133 | + .anyMatch(ModerationUtils.getIsQuarantinedRolePredicate(config))) { |
| 134 | + handleAlreadyQuarantinedTarget(event); |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onSlashCommand(@NotNull SlashCommandEvent event) { |
| 143 | + Member target = event.getOption(TARGET_OPTION).getAsMember(); |
| 144 | + Member author = event.getMember(); |
| 145 | + String reason = event.getOption(REASON_OPTION).getAsString(); |
| 146 | + |
| 147 | + Guild guild = Objects.requireNonNull(event.getGuild()); |
| 148 | + Member bot = guild.getSelfMember(); |
| 149 | + |
| 150 | + if (!handleChecks(bot, author, target, reason, guild, event)) { |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + quarantineUserFlow(Objects.requireNonNull(target), author, reason, guild, event); |
| 155 | + } |
| 156 | +} |
0 commit comments