Repository navigation
Feature: Add self-service Unquarantine button for some quarantined users #1603
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 all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,11 +9,15 @@ | |||||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||||||
| import net.dv8tion.jda.api.entities.Role; | ||||||
| import net.dv8tion.jda.api.entities.User; | ||||||
| import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||||||
| import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback; | ||||||
| import net.dv8tion.jda.api.interactions.components.buttons.Button; | ||||||
| import net.dv8tion.jda.api.requests.RestAction; | ||||||
| import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; | ||||||
| import net.dv8tion.jda.api.utils.Result; | ||||||
| import net.dv8tion.jda.api.utils.TimeUtil; | ||||||
| import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; | ||||||
| import net.dv8tion.jda.api.utils.messages.MessageCreateData; | ||||||
| import net.dv8tion.jda.internal.requests.CompletedRestAction; | ||||||
| import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | ||||||
|
|
@@ -36,9 +40,11 @@ | |||||
| import java.util.List; | ||||||
| import java.util.Locale; | ||||||
| import java.util.Map; | ||||||
| import java.util.NoSuchElementException; | ||||||
| import java.util.Optional; | ||||||
| import java.util.concurrent.TimeUnit; | ||||||
| import java.util.function.Predicate; | ||||||
| import java.util.function.Supplier; | ||||||
| import java.util.regex.Pattern; | ||||||
| import java.util.stream.Collectors; | ||||||
|
|
||||||
|
|
@@ -617,4 +623,78 @@ public static RestAction<MessageEmbed.Field> moderationActionToEmbedField( | |||||
| }); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Quarantines the given user by recording the action and assigning the quarantine role. If the | ||||||
| * user was never quarantined in this guild before, a message pinging them with an "Unquarantine | ||||||
| * me" button is then posted in the quarantine channel. | ||||||
| * <p> | ||||||
| * The action is recorded when this method is called, the role is only assigned once the | ||||||
| * returned action is executed. The message is sent only after the role was applied, and | ||||||
| * problems offering it (e.g. missing channel) are logged and never fail the returned action. | ||||||
| * | ||||||
| * @param config the app's config, used to find the quarantine role and channel | ||||||
| * @param selfServiceComponentId supplies the button's component ID | ||||||
| * @param actionsStore the store to record the action in and to check the user's history | ||||||
| * @param guild the guild to quarantine the user in | ||||||
| * @param author the member issuing the quarantine | ||||||
| * @param target the member to quarantine | ||||||
| * @param reason the reason of the quarantine | ||||||
| * @return the action assigning the quarantine role to the target | ||||||
| * @throws java.util.NoSuchElementException if the quarantine role does not exist | ||||||
| * @see <a href="https://github.com/Together-Java/TJ-Bot/issues/1594">Issue #1594</a> | ||||||
| */ | ||||||
| public static RestAction<Void> quarantine(Config config, | ||||||
| Supplier<String> selfServiceComponentId, ModerationActionsStore actionsStore, | ||||||
| Guild guild, Member author, Member target, String reason) | ||||||
| throws NoSuchElementException { | ||||||
|
|
||||||
| boolean isFirsttime = | ||||||
| actionsStore.getActionsByTargetAscending(guild.getIdLong(), target.getIdLong()) | ||||||
| .stream() | ||||||
| .noneMatch(action -> action.actionType() == ModerationAction.QUARANTINE); | ||||||
|
|
||||||
| recordAction(guild, target.getUser(), author, ModerationAction.QUARANTINE, null, reason, | ||||||
| actionsStore); | ||||||
|
|
||||||
| return guild.addRoleToMember(target, getQuarantinedRole(guild, config).orElseThrow()) | ||||||
| .reason(reason) | ||||||
| .onSuccess(_ -> { | ||||||
|
|
||||||
| final String guildId = guild.getId(); | ||||||
| final String targetId = target.getId(); | ||||||
|
|
||||||
| if (!isFirsttime) { | ||||||
|
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.
Suggested change
Corresponding to above |
||||||
| logger.warn("Unable to offer self-service unquarantine to '{}' in guild '{}'.", | ||||||
| targetId, guildId); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| Optional<TextChannel> quarantineChannel = Guilds.findTextChannel(guild, | ||||||
| Pattern.compile(config.getQuarantineChannelPattern()).asMatchPredicate()); | ||||||
|
|
||||||
| if (quarantineChannel.isEmpty()) { | ||||||
| logger.warn( | ||||||
| "Unable to offer self-service unquarantine, no channel matching '{}' in guild '{}'.", | ||||||
| config.getQuarantineChannelPattern(), guild.getName()); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| MessageCreateData message = new MessageCreateBuilder() | ||||||
| .setContent( | ||||||
| """ | ||||||
| %s. | ||||||
| If you regained control over your account by now, feel free to click the button \ | ||||||
| below to get back into the server. Otherwise, a moderator will get back to you here. Cheers""" | ||||||
| .formatted(target.getAsMention())) | ||||||
| .mentionUsers(target.getIdLong()) | ||||||
| .setActionRow(Button.primary(selfServiceComponentId.get(), "Unquarantine me")) | ||||||
| .build(); | ||||||
|
|
||||||
| quarantineChannel.orElseThrow().sendMessage(message).queue(_ -> { | ||||||
| }, failure -> logger.warn( | ||||||
| "unable to offer self-service unquarantine to '{}' in guild '{}'.", | ||||||
| targetId, guildId, failure)); | ||||||
|
|
||||||
| }); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,20 +6,21 @@ | |||||
| import net.dv8tion.jda.api.entities.Role; | ||||||
| import net.dv8tion.jda.api.entities.User; | ||||||
| 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.OptionType; | ||||||
| import net.dv8tion.jda.api.requests.RestAction; | ||||||
| import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; | ||||||
| import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | ||||||
|
|
||||||
| import org.togetherjava.tjbot.config.Config; | ||||||
| import org.togetherjava.tjbot.features.CommandVisibility; | ||||||
| import org.togetherjava.tjbot.features.SlashCommandAdapter; | ||||||
| import org.togetherjava.tjbot.logging.LogMarkers; | ||||||
| import org.togetherjava.tjbot.features.componentids.Lifespan; | ||||||
| import org.togetherjava.tjbot.features.utils.Guilds; | ||||||
| import org.togetherjava.tjbot.features.utils.MessageUtils; | ||||||
|
|
||||||
| import javax.annotation.Nullable; | ||||||
|
|
||||||
| import java.util.List; | ||||||
| import java.util.Objects; | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -31,12 +32,15 @@ | |||||
| * an admin). | ||||||
| */ | ||||||
| public final class QuarantineCommand extends SlashCommandAdapter { | ||||||
| private static final Logger logger = LoggerFactory.getLogger(QuarantineCommand.class); | ||||||
| private static final String TARGET_OPTION = "user"; | ||||||
| private static final String REASON_OPTION = "reason"; | ||||||
| private static final String COMMAND_NAME = "quarantine"; | ||||||
| public static final String COMMAND_NAME = "quarantine"; | ||||||
| private static final String ACTION_VERB = "quarantine"; | ||||||
| private static final String ACTION_TITLE = "Quarantine"; | ||||||
|
|
||||||
| private static final String SELF_UNQUARANTINE_REASON = | ||||||
| "User regained control and unquarantined themselves."; | ||||||
|
|
||||||
| private final ModerationActionsStore actionsStore; | ||||||
| private final Config config; | ||||||
|
|
||||||
|
|
@@ -85,29 +89,15 @@ private static MessageEmbed sendFeedback(boolean hasSentDm, Member target, Membe | |||||
| target.getUser(), dmNoticeText, reason); | ||||||
| } | ||||||
|
|
||||||
| private AuditableRestAction<Void> quarantineUser(Member target, Member author, String reason, | ||||||
| Guild guild) { | ||||||
| logger.info(LogMarkers.SENSITIVE, | ||||||
| "'{}' ({}) quarantined the user '{}' ({}) in guild '{}' for reason '{}'.", | ||||||
| author.getUser().getName(), author.getId(), target.getUser().getName(), | ||||||
| target.getId(), guild.getName(), reason); | ||||||
|
|
||||||
| actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(), | ||||||
| ModerationAction.QUARANTINE, null, reason); | ||||||
|
|
||||||
| return guild | ||||||
| .addRoleToMember(target, | ||||||
| ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) | ||||||
| .reason(reason); | ||||||
| } | ||||||
|
|
||||||
| private void quarantineUserFlow(Member target, Member author, String reason, Guild guild, | ||||||
| SlashCommandInteractionEvent event) { | ||||||
| event.deferReply().queue(); | ||||||
|
|
||||||
| sendDm(target.getUser(), reason, guild) | ||||||
| .flatMap(hasSentDm -> quarantineUser(target, author, reason, guild) | ||||||
| .map(result -> hasSentDm)) | ||||||
| .flatMap(hasSentDm -> ModerationUtils | ||||||
| .quarantine(config, () -> generateComponentId(Lifespan.PERMANENT, target.getId()), | ||||||
| actionsStore, guild, author, target, reason) | ||||||
| .map(_ -> hasSentDm)) | ||||||
| .map(hasSentDm -> sendFeedback(hasSentDm, target, author, reason)) | ||||||
| .flatMap(event.getHook()::sendMessageEmbeds) | ||||||
| .queue(); | ||||||
|
|
@@ -148,4 +138,36 @@ public void onSlashCommand(SlashCommandInteractionEvent event) { | |||||
|
|
||||||
| quarantineUserFlow(Objects.requireNonNull(target), author, reason, guild, event); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void onButtonClick(ButtonInteractionEvent event, List<String> args) { | ||||||
|
|
||||||
| Guild guild = Objects.requireNonNull(event.getGuild()); | ||||||
| Member member = Objects.requireNonNull(event.getMember()); | ||||||
|
|
||||||
| if (!Guilds.hasMemberRole(member, ModerationUtils.getIsQuarantinedRolePredicate(config))) { | ||||||
| event.reply("You are not quarantined anymore!").setEphemeral(true).queue(); | ||||||
|
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.
Suggested change
|
||||||
|
|
||||||
| MessageUtils.disableButtons(event.getMessage()); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| event.deferEdit().queue(); | ||||||
|
|
||||||
| guild | ||||||
| .removeRoleFromMember(member, | ||||||
| ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) | ||||||
| .reason(SELF_UNQUARANTINE_REASON) | ||||||
| .queue(_ -> { | ||||||
| actionsStore.addAction(guild.getIdLong(), guild.getSelfMember().getIdLong(), | ||||||
| event.getUser().getIdLong(), ModerationAction.UNQUARANTINE, null, | ||||||
| SELF_UNQUARANTINE_REASON); | ||||||
|
|
||||||
| MessageUtils.disableButtons(event.getMessage()); | ||||||
| }, _ -> event.getHook() | ||||||
| .sendMessage( | ||||||
| "Something went wrong while trying to unquarantine your account, try to reach via /modmail.") | ||||||
| .setEphemeral(true) | ||||||
| .queue()); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |
| import net.dv8tion.jda.api.entities.Member; | ||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||
| import net.dv8tion.jda.api.entities.SelfUser; | ||
| import net.dv8tion.jda.api.entities.User; | ||
| import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; | ||
| import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
|
|
@@ -30,9 +29,10 @@ | |
| import org.togetherjava.tjbot.features.analytics.Metrics; | ||
| import org.togetherjava.tjbot.features.componentids.ComponentIdGenerator; | ||
| import org.togetherjava.tjbot.features.componentids.ComponentIdInteractor; | ||
| import org.togetherjava.tjbot.features.moderation.ModerationAction; | ||
| import org.togetherjava.tjbot.features.componentids.Lifespan; | ||
| import org.togetherjava.tjbot.features.moderation.ModerationActionsStore; | ||
| import org.togetherjava.tjbot.features.moderation.ModerationUtils; | ||
| import org.togetherjava.tjbot.features.moderation.QuarantineCommand; | ||
| import org.togetherjava.tjbot.features.moderation.modmail.ModMailCommand; | ||
| import org.togetherjava.tjbot.features.utils.AmbientColors; | ||
| import org.togetherjava.tjbot.features.utils.Guilds; | ||
|
|
@@ -78,6 +78,9 @@ public final class ScamBlocker extends MessageReceiverAdapter implements UserInt | |
| private final Metrics metrics; | ||
| private final ComponentIdInteractor componentIdInteractor; | ||
|
|
||
| private final ComponentIdInteractor quarantineComponentIds = new ComponentIdInteractor( | ||
| UserInteractionType.SLASH_COMMAND, QuarantineCommand.COMMAND_NAME); | ||
|
|
||
| /** | ||
| * Creates a new listener to receive all message sent in any channel. | ||
| * | ||
|
|
@@ -121,6 +124,8 @@ public UserInteractionType getInteractionType() { | |
| @Override | ||
| public void acceptComponentIdGenerator(ComponentIdGenerator generator) { | ||
| componentIdInteractor.acceptComponentIdGenerator(generator); | ||
| quarantineComponentIds.acceptComponentIdGenerator(generator); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -223,19 +228,26 @@ private void deleteMessage(MessageReceivedEvent event) { | |
| } | ||
|
|
||
| private void quarantineAuthor(MessageReceivedEvent event) { | ||
| quarantineAuthor(event.getGuild(), event.getMember(), event.getJDA().getSelfUser()); | ||
| } | ||
| Guild guild = event.getGuild(); | ||
| Member member = event.getMember(); | ||
|
|
||
| private void quarantineAuthor(Guild guild, Member author, SelfUser bot) { | ||
| String reason = "User posted scam that was automatically detected"; | ||
| if (member == null) { | ||
| logger.warn(LogMarkers.SENSITIVE, | ||
| "scam message detected from a user that is not a member of the guild '{}'.", | ||
| guild.getId()); | ||
| return; | ||
| } | ||
|
Comment on lines
+234
to
+239
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. The only channels that are read are TextChannel's (a MessageChannel specifically for Guilds), that said if the user is not a Member of the guild we would have a shitton of NPE's before we arrive at this point. See ScamBlocker:142 |
||
|
|
||
| actionsStore.addAction(guild.getIdLong(), bot.getIdLong(), author.getIdLong(), | ||
| ModerationAction.QUARANTINE, null, reason); | ||
| quarantineAuthor(guild, member); | ||
| } | ||
|
|
||
| guild | ||
| .addRoleToMember(author, | ||
| ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) | ||
| .reason(reason) | ||
| private void quarantineAuthor(Guild guild, Member author) { | ||
| ModerationUtils | ||
| .quarantine(config, | ||
| () -> quarantineComponentIds.generateComponentId(Lifespan.PERMANENT, | ||
| author.getId()), | ||
| actionsStore, guild, guild.getSelfMember(), author, | ||
| "User posted scam that was automatically detected") | ||
| .queue(); | ||
| } | ||
|
|
||
|
|
@@ -356,7 +368,7 @@ public void onButtonClick(ButtonInteractionEvent event, List<String> argsRaw) { | |
| } | ||
|
|
||
| Consumer<Member> onRetrieveAuthorSuccess = author -> { | ||
| quarantineAuthor(guild, author, event.getJDA().getSelfUser()); | ||
| quarantineAuthor(guild, author); | ||
| dmUser(guild, args.authorId, event.getJDA()); | ||
|
|
||
| // Delete all messages like this | ||
|
|
||
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.
Use
findLastActionAgainstTargetByTypeand renameisFirsttimetoisFirstTime