From 47d48b05b8f715d59e363533afd28c618950414d Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 16 Mar 2022 20:15:12 +0200 Subject: [PATCH 01/10] re-worked the temporary moderation action's failure handling --- .../temp/RevocableModerationAction.java | 33 +++----------- .../moderation/temp/TemporaryBanAction.java | 35 ++------------- .../temp/TemporaryModerationRoutine.java | 40 ++++++++++++----- .../moderation/temp/TemporaryMuteAction.java | 42 +++--------------- .../temp/TemporaryQuarantineAction.java | 43 +++---------------- 5 files changed, 51 insertions(+), 142 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java index b2cedc3007..1d3a9847c1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java @@ -12,21 +12,14 @@ */ interface RevocableModerationAction { /** - * Classification of a revocation failure. + * The name of the action, that will be logged in case of a failed revocation. + * + *

+ * Naming convention examples: {@code "ban"}, {@code "mute"}, {@code "quarantine"} + *

*/ - @SuppressWarnings("PublicInnerClass") - enum FailureIdentification { - /** - * Acknowledges that the failure is known and has been handled. Hence, further error - * handling should not be continued. - */ - KNOWN, - /** - * The failure is unknown and has not been handled. Hence, further error handling should be - * continued. - */ - UNKNOWN - } + @NotNull + String actionName(); /** * The type to apply the temporary action, such as @@ -57,16 +50,4 @@ enum FailureIdentification { @NotNull RestAction revokeAction(@NotNull Guild guild, @NotNull User target, @NotNull String reason); - - /** - * Handle a failure that might occur during revocation, i.e. execution of the action returned by - * {@link #revokeAction(Guild, User, String)}. - * - * @param failure the failure to handle - * @param targetId the id of the user who is targeted by the revocation - * @return a classification of the failure, decides whether the surrounding flow will continue - * to handle the error further or not - */ - @NotNull - FailureIdentification handleRevokeFailure(@NotNull Throwable failure, long targetId); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java index 0941224872..843d039025 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java @@ -2,12 +2,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; /** @@ -16,7 +12,10 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryBanAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(TemporaryBanAction.class); + @Override + public @NotNull String actionName() { + return "ban"; + } @Override public @NotNull ModerationAction getApplyType() { @@ -33,30 +32,4 @@ final class TemporaryBanAction implements RevocableModerationAction { @NotNull String reason) { return guild.unban(target).reason(reason); } - - @Override - public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, - long targetId) { - if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( - "Attempted to revoke a temporary ban but user '{}' does not exist anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_BAN) { - logger.debug( - "Attempted to revoke a temporary ban but the user '{}' is not banned anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { - logger.warn("Attempted to revoke a temporary ban but the bot lacks permission."); - return FailureIdentification.KNOWN; - } - } - return FailureIdentification.UNKNOWN; - } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index 26671c196e..ec6991c69d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -3,6 +3,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -122,7 +123,8 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { jda.retrieveUserById(groupIdentifier.targetId) .flatMap(target -> executeRevocation(guild, target, groupIdentifier.type)) .queue(result -> { - }, failure -> handleFailure(failure, groupIdentifier)); + }, failure -> handleFailure(failure, groupIdentifier.targetId, + getRevocableActionByType(groupIdentifier.type).actionName())); } private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, @@ -138,16 +140,34 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { return action.revokeAction(guild, target, reason); } - private void handleFailure(@NotNull Throwable failure, - @NotNull RevocationGroupIdentifier groupIdentifier) { - if (getRevocableActionByType(groupIdentifier.type).handleRevokeFailure(failure, - groupIdentifier.targetId) == RevocableModerationAction.FailureIdentification.KNOWN) { - return; - } - - logger.warn( + private void handleFailure(@NotNull Throwable failure, long targetId, + @NotNull String actionName) { + Runnable logUnknownFailure = () -> logger.warn( "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", - groupIdentifier.targetId, failure); + targetId, failure); + + if (failure instanceof ErrorResponseException errorResponseException) { + switch (errorResponseException.getErrorResponse()) { + case UNKNOWN_USER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", + actionName, targetId); + case UNKNOWN_MEMBER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", + actionName, targetId); + case UNKNOWN_ROLE -> logger.warn( + "Attempted to revoke a temporary {} but the {} role can not be found.", + actionName, actionName); + case MISSING_PERMISSIONS -> logger.warn( + "Attempted to revoke a temporary {} but the bot lacks permission.", + actionName); + case UNKNOWN_BAN -> logger.debug( + "Attempted to revoke a temporary {} but the user '{}' is not banned anymore.", + actionName, targetId); + default -> logUnknownFailure.run(); + } + } else { + logUnknownFailure.run(); + } } private @NotNull RevocableModerationAction getRevocableActionByType( diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java index f7c0d29a58..0236fd253c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java @@ -2,12 +2,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; @@ -18,7 +14,6 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryMuteAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(TemporaryMuteAction.class); private final Config config; /** @@ -30,6 +25,11 @@ final class TemporaryMuteAction implements RevocableModerationAction { this.config = config; } + @Override + public @NotNull String actionName() { + return "mute"; + } + @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.MUTE; @@ -48,36 +48,4 @@ final class TemporaryMuteAction implements RevocableModerationAction { ModerationUtils.getMutedRole(guild, config).orElseThrow()) .reason(reason); } - - @Override - public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, - long targetId) { - if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( - "Attempted to revoke a temporary mute but user '{}' does not exist anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { - logger.debug( - "Attempted to revoke a temporary mute but user '{}' is not a member of the guild anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { - logger.warn( - "Attempted to revoke a temporary mute but the mute role can not be found."); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { - logger.warn("Attempted to revoke a temporary mute but the bot lacks permission."); - return FailureIdentification.KNOWN; - } - } - return FailureIdentification.UNKNOWN; - } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java index dd87658551..eb6eddb522 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java @@ -2,12 +2,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; @@ -18,7 +14,6 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryQuarantineAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(TemporaryQuarantineAction.class); private final Config config; /** @@ -30,6 +25,11 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { this.config = config; } + @Override + public @NotNull String actionName() { + return "quarantine"; + } + @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.QUARANTINE; @@ -48,37 +48,4 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) .reason(reason); } - - @Override - public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, - long targetId) { - if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( - "Attempted to revoke a temporary quarantine but user '{}' does not exist anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { - logger.debug( - "Attempted to revoke a temporary quarantine but user '{}' is not a member of the guild anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { - logger.warn( - "Attempted to revoke a temporary quarantine but the quarantine role can not be found."); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { - logger.warn( - "Attempted to revoke a temporary quarantine but the bot lacks permission."); - return FailureIdentification.KNOWN; - } - } - return FailureIdentification.UNKNOWN; - } } From 88212b667a2cfd1fd9bc18bfee947d88af76ebd7 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 18 Mar 2022 00:22:07 +0200 Subject: [PATCH 02/10] reverted most logic, introduced `RevocableRoleBasedAction` --- .../temp/RevocableModerationAction.java | 21 +++++---- .../temp/RevocableRoleBasedAction.java | 38 +++++++++++++++ .../moderation/temp/TemporaryBanAction.java | 27 +++++++++-- .../temp/TemporaryModerationRoutine.java | 47 ++++++------------- .../moderation/temp/TemporaryMuteAction.java | 8 +--- .../temp/TemporaryQuarantineAction.java | 8 +--- 6 files changed, 91 insertions(+), 58 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java index 1d3a9847c1..a67c3678bb 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java @@ -2,6 +2,7 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.moderation.ModerationAction; @@ -11,16 +12,6 @@ * {@link TemporaryModerationRoutine} to identify and revoke such actions. */ interface RevocableModerationAction { - /** - * The name of the action, that will be logged in case of a failed revocation. - * - *

- * Naming convention examples: {@code "ban"}, {@code "mute"}, {@code "quarantine"} - *

- */ - @NotNull - String actionName(); - /** * The type to apply the temporary action, such as * {@link net.dv8tion.jda.api.audit.ActionType#BAN}. @@ -50,4 +41,14 @@ interface RevocableModerationAction { @NotNull RestAction revokeAction(@NotNull Guild guild, @NotNull User target, @NotNull String reason); + + /** + * Handle a failure that might occur during revocation, i.e. execution of the action returned by + * {@link #revokeAction(Guild, User, String)}. + * + * @param errorResponse the error that occurred + * @param targetId the id of the user who is targeted by the revocation + * @return whether it should log an unknown failure message or not + */ + boolean handleRevokeFailure(@NotNull ErrorResponse errorResponse, long targetId); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java new file mode 100644 index 0000000000..fb44ed38e9 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -0,0 +1,38 @@ +package org.togetherjava.tjbot.commands.moderation.temp; + +import net.dv8tion.jda.api.requests.ErrorResponse; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +abstract class RevocableRoleBasedAction implements RevocableModerationAction { + private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class); + + private final String actionName; + + RevocableRoleBasedAction(String actionName) { + this.actionName = actionName; + } + + @Override + public boolean handleRevokeFailure(@NotNull ErrorResponse error, long targetId) { + switch (error) { + case UNKNOWN_USER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", + actionName, targetId); + case UNKNOWN_MEMBER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", + actionName, targetId); + case UNKNOWN_ROLE -> logger.warn( + "Attempted to revoke a temporary {} but the {} role can not be found.", + actionName, actionName); + case MISSING_PERMISSIONS -> logger.warn( + "Attempted to revoke a temporary {} but the bot lacks permission.", actionName); + default -> { + return true; + } + } + + return false; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java index 843d039025..a95a503a6f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java @@ -2,8 +2,11 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; /** @@ -12,10 +15,7 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryBanAction implements RevocableModerationAction { - @Override - public @NotNull String actionName() { - return "ban"; - } + private static final Logger logger = LoggerFactory.getLogger(TemporaryBanAction.class); @Override public @NotNull ModerationAction getApplyType() { @@ -32,4 +32,23 @@ final class TemporaryBanAction implements RevocableModerationAction { @NotNull String reason) { return guild.unban(target).reason(reason); } + + @Override + public boolean handleRevokeFailure(@NotNull ErrorResponse error, long targetId) { + switch (error) { + case UNKNOWN_USER -> logger.debug( + "Attempted to revoke a temporary ban but user '{}' does not exist anymore.", + targetId); + case MISSING_PERMISSIONS -> logger + .warn("Attempted to revoke a temporary ban but the bot lacks permission."); + case UNKNOWN_BAN -> logger.debug( + "Attempted to revoke a temporary ban but the user '{}' is not banned anymore.", + targetId); + default -> { + return true; + } + } + + return false; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index ec6991c69d..ae6d8b3c32 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -123,8 +123,21 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { jda.retrieveUserById(groupIdentifier.targetId) .flatMap(target -> executeRevocation(guild, target, groupIdentifier.type)) .queue(result -> { - }, failure -> handleFailure(failure, groupIdentifier.targetId, - getRevocableActionByType(groupIdentifier.type).actionName())); + }, failure -> handleFailure(failure, groupIdentifier)); + } + + private void handleFailure(@NotNull Throwable failure, + @NotNull RevocationGroupIdentifier groupIdentifier) { + if (failure instanceof ErrorResponseException errorResponseException + && !getRevocableActionByType(groupIdentifier.type).handleRevokeFailure( + errorResponseException.getErrorResponse(), groupIdentifier.targetId)) { + + return; + } + + logger.warn( + "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", + groupIdentifier.targetId, failure); } private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, @@ -140,36 +153,6 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { return action.revokeAction(guild, target, reason); } - private void handleFailure(@NotNull Throwable failure, long targetId, - @NotNull String actionName) { - Runnable logUnknownFailure = () -> logger.warn( - "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", - targetId, failure); - - if (failure instanceof ErrorResponseException errorResponseException) { - switch (errorResponseException.getErrorResponse()) { - case UNKNOWN_USER -> logger.debug( - "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", - actionName, targetId); - case UNKNOWN_MEMBER -> logger.debug( - "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", - actionName, targetId); - case UNKNOWN_ROLE -> logger.warn( - "Attempted to revoke a temporary {} but the {} role can not be found.", - actionName, actionName); - case MISSING_PERMISSIONS -> logger.warn( - "Attempted to revoke a temporary {} but the bot lacks permission.", - actionName); - case UNKNOWN_BAN -> logger.debug( - "Attempted to revoke a temporary {} but the user '{}' is not banned anymore.", - actionName, targetId); - default -> logUnknownFailure.run(); - } - } else { - logUnknownFailure.run(); - } - } - private @NotNull RevocableModerationAction getRevocableActionByType( @NotNull ModerationAction type) { return Objects.requireNonNull(typeToRevocableAction.get(type), diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java index 0236fd253c..bcb4437534 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java @@ -13,7 +13,7 @@ * {@link org.togetherjava.tjbot.commands.moderation.MuteCommand} and executed by * {@link TemporaryModerationRoutine}. */ -final class TemporaryMuteAction implements RevocableModerationAction { +final class TemporaryMuteAction extends RevocableRoleBasedAction { private final Config config; /** @@ -22,14 +22,10 @@ final class TemporaryMuteAction implements RevocableModerationAction { * @param config the config to use to identify the muted role */ TemporaryMuteAction(@NotNull Config config) { + super("mute"); this.config = config; } - @Override - public @NotNull String actionName() { - return "mute"; - } - @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.MUTE; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java index eb6eddb522..d092d9eb7b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java @@ -13,7 +13,7 @@ * {@link org.togetherjava.tjbot.commands.moderation.QuarantineCommand} and executed by * {@link TemporaryModerationRoutine}. */ -final class TemporaryQuarantineAction implements RevocableModerationAction { +final class TemporaryQuarantineAction extends RevocableRoleBasedAction { private final Config config; /** @@ -22,14 +22,10 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { * @param config the config to use to identify the quarantined role */ TemporaryQuarantineAction(@NotNull Config config) { + super("quarantine"); this.config = config; } - @Override - public @NotNull String actionName() { - return "quarantine"; - } - @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.QUARANTINE; From 0f707a4abd99a6c350b270d466814220c7e19b70 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Fri, 18 Mar 2022 00:33:34 +0200 Subject: [PATCH 03/10] javadoc + reordered methods --- .../temp/RevocableRoleBasedAction.java | 12 +++++++++ .../temp/TemporaryModerationRoutine.java | 26 +++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java index fb44ed38e9..b658181d29 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -5,11 +5,23 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * {@link RevocableModerationAction} abstract implementation for a role based moderation action. + *
+ *
+ * Implements {@link RevocableModerationAction#handleRevokeFailure(ErrorResponse, long)} to handle + * possible errors for a role-based revocation action. + */ abstract class RevocableRoleBasedAction implements RevocableModerationAction { private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class); private final String actionName; + /** + * Creates an instance of {@link RevocableRoleBasedAction} with the given action name. + * + * @param actionName the name of the moderation action + */ RevocableRoleBasedAction(String actionName) { this.actionName = actionName; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index ae6d8b3c32..28ef79167a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -126,6 +126,19 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { }, failure -> handleFailure(failure, groupIdentifier)); } + private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, + @NotNull ModerationAction actionType) { + logger.info("Revoked temporary action {} against user '{}' ({}).", actionType, + target.getAsTag(), target.getId()); + RevocableModerationAction action = getRevocableActionByType(actionType); + + String reason = "Automatic revocation of temporary action."; + actionsStore.addAction(guild.getIdLong(), jda.getSelfUser().getIdLong(), target.getIdLong(), + action.getRevokeType(), null, reason); + + return action.revokeAction(guild, target, reason); + } + private void handleFailure(@NotNull Throwable failure, @NotNull RevocationGroupIdentifier groupIdentifier) { if (failure instanceof ErrorResponseException errorResponseException @@ -140,19 +153,6 @@ private void handleFailure(@NotNull Throwable failure, groupIdentifier.targetId, failure); } - private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, - @NotNull ModerationAction actionType) { - logger.info("Revoked temporary action {} against user '{}' ({}).", actionType, - target.getAsTag(), target.getId()); - RevocableModerationAction action = getRevocableActionByType(actionType); - - String reason = "Automatic revocation of temporary action."; - actionsStore.addAction(guild.getIdLong(), jda.getSelfUser().getIdLong(), target.getIdLong(), - action.getRevokeType(), null, reason); - - return action.revokeAction(guild, target, reason); - } - private @NotNull RevocableModerationAction getRevocableActionByType( @NotNull ModerationAction type) { return Objects.requireNonNull(typeToRevocableAction.get(type), From a74e2a19f75b11e017139d216835010a362321c0 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:06:10 +0300 Subject: [PATCH 04/10] Revert "javadoc + reordered methods" This reverts commit 0f707a4abd99a6c350b270d466814220c7e19b70. --- .../temp/RevocableRoleBasedAction.java | 12 --------- .../temp/TemporaryModerationRoutine.java | 26 +++++++++---------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java index b658181d29..fb44ed38e9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -5,23 +5,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * {@link RevocableModerationAction} abstract implementation for a role based moderation action. - *
- *
- * Implements {@link RevocableModerationAction#handleRevokeFailure(ErrorResponse, long)} to handle - * possible errors for a role-based revocation action. - */ abstract class RevocableRoleBasedAction implements RevocableModerationAction { private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class); private final String actionName; - /** - * Creates an instance of {@link RevocableRoleBasedAction} with the given action name. - * - * @param actionName the name of the moderation action - */ RevocableRoleBasedAction(String actionName) { this.actionName = actionName; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index 28ef79167a..ae6d8b3c32 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -126,19 +126,6 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { }, failure -> handleFailure(failure, groupIdentifier)); } - private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, - @NotNull ModerationAction actionType) { - logger.info("Revoked temporary action {} against user '{}' ({}).", actionType, - target.getAsTag(), target.getId()); - RevocableModerationAction action = getRevocableActionByType(actionType); - - String reason = "Automatic revocation of temporary action."; - actionsStore.addAction(guild.getIdLong(), jda.getSelfUser().getIdLong(), target.getIdLong(), - action.getRevokeType(), null, reason); - - return action.revokeAction(guild, target, reason); - } - private void handleFailure(@NotNull Throwable failure, @NotNull RevocationGroupIdentifier groupIdentifier) { if (failure instanceof ErrorResponseException errorResponseException @@ -153,6 +140,19 @@ private void handleFailure(@NotNull Throwable failure, groupIdentifier.targetId, failure); } + private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, + @NotNull ModerationAction actionType) { + logger.info("Revoked temporary action {} against user '{}' ({}).", actionType, + target.getAsTag(), target.getId()); + RevocableModerationAction action = getRevocableActionByType(actionType); + + String reason = "Automatic revocation of temporary action."; + actionsStore.addAction(guild.getIdLong(), jda.getSelfUser().getIdLong(), target.getIdLong(), + action.getRevokeType(), null, reason); + + return action.revokeAction(guild, target, reason); + } + private @NotNull RevocableModerationAction getRevocableActionByType( @NotNull ModerationAction type) { return Objects.requireNonNull(typeToRevocableAction.get(type), From 01c0dee8e66fe89474140fb93fc7e473d516e9c2 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:06:11 +0300 Subject: [PATCH 05/10] Revert "reverted most logic, introduced `RevocableRoleBasedAction`" This reverts commit 88212b667a2cfd1fd9bc18bfee947d88af76ebd7. --- .../temp/RevocableModerationAction.java | 21 ++++----- .../temp/RevocableRoleBasedAction.java | 38 --------------- .../moderation/temp/TemporaryBanAction.java | 27 ++--------- .../temp/TemporaryModerationRoutine.java | 47 +++++++++++++------ .../moderation/temp/TemporaryMuteAction.java | 8 +++- .../temp/TemporaryQuarantineAction.java | 8 +++- 6 files changed, 58 insertions(+), 91 deletions(-) delete mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java index a67c3678bb..1d3a9847c1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java @@ -2,7 +2,6 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.moderation.ModerationAction; @@ -12,6 +11,16 @@ * {@link TemporaryModerationRoutine} to identify and revoke such actions. */ interface RevocableModerationAction { + /** + * The name of the action, that will be logged in case of a failed revocation. + * + *

+ * Naming convention examples: {@code "ban"}, {@code "mute"}, {@code "quarantine"} + *

+ */ + @NotNull + String actionName(); + /** * The type to apply the temporary action, such as * {@link net.dv8tion.jda.api.audit.ActionType#BAN}. @@ -41,14 +50,4 @@ interface RevocableModerationAction { @NotNull RestAction revokeAction(@NotNull Guild guild, @NotNull User target, @NotNull String reason); - - /** - * Handle a failure that might occur during revocation, i.e. execution of the action returned by - * {@link #revokeAction(Guild, User, String)}. - * - * @param errorResponse the error that occurred - * @param targetId the id of the user who is targeted by the revocation - * @return whether it should log an unknown failure message or not - */ - boolean handleRevokeFailure(@NotNull ErrorResponse errorResponse, long targetId); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java deleted file mode 100644 index fb44ed38e9..0000000000 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.togetherjava.tjbot.commands.moderation.temp; - -import net.dv8tion.jda.api.requests.ErrorResponse; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -abstract class RevocableRoleBasedAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class); - - private final String actionName; - - RevocableRoleBasedAction(String actionName) { - this.actionName = actionName; - } - - @Override - public boolean handleRevokeFailure(@NotNull ErrorResponse error, long targetId) { - switch (error) { - case UNKNOWN_USER -> logger.debug( - "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", - actionName, targetId); - case UNKNOWN_MEMBER -> logger.debug( - "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", - actionName, targetId); - case UNKNOWN_ROLE -> logger.warn( - "Attempted to revoke a temporary {} but the {} role can not be found.", - actionName, actionName); - case MISSING_PERMISSIONS -> logger.warn( - "Attempted to revoke a temporary {} but the bot lacks permission.", actionName); - default -> { - return true; - } - } - - return false; - } -} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java index a95a503a6f..843d039025 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java @@ -2,11 +2,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; /** @@ -15,7 +12,10 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryBanAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(TemporaryBanAction.class); + @Override + public @NotNull String actionName() { + return "ban"; + } @Override public @NotNull ModerationAction getApplyType() { @@ -32,23 +32,4 @@ final class TemporaryBanAction implements RevocableModerationAction { @NotNull String reason) { return guild.unban(target).reason(reason); } - - @Override - public boolean handleRevokeFailure(@NotNull ErrorResponse error, long targetId) { - switch (error) { - case UNKNOWN_USER -> logger.debug( - "Attempted to revoke a temporary ban but user '{}' does not exist anymore.", - targetId); - case MISSING_PERMISSIONS -> logger - .warn("Attempted to revoke a temporary ban but the bot lacks permission."); - case UNKNOWN_BAN -> logger.debug( - "Attempted to revoke a temporary ban but the user '{}' is not banned anymore.", - targetId); - default -> { - return true; - } - } - - return false; - } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index ae6d8b3c32..ec6991c69d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -123,21 +123,8 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { jda.retrieveUserById(groupIdentifier.targetId) .flatMap(target -> executeRevocation(guild, target, groupIdentifier.type)) .queue(result -> { - }, failure -> handleFailure(failure, groupIdentifier)); - } - - private void handleFailure(@NotNull Throwable failure, - @NotNull RevocationGroupIdentifier groupIdentifier) { - if (failure instanceof ErrorResponseException errorResponseException - && !getRevocableActionByType(groupIdentifier.type).handleRevokeFailure( - errorResponseException.getErrorResponse(), groupIdentifier.targetId)) { - - return; - } - - logger.warn( - "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", - groupIdentifier.targetId, failure); + }, failure -> handleFailure(failure, groupIdentifier.targetId, + getRevocableActionByType(groupIdentifier.type).actionName())); } private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, @@ -153,6 +140,36 @@ private void handleFailure(@NotNull Throwable failure, return action.revokeAction(guild, target, reason); } + private void handleFailure(@NotNull Throwable failure, long targetId, + @NotNull String actionName) { + Runnable logUnknownFailure = () -> logger.warn( + "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", + targetId, failure); + + if (failure instanceof ErrorResponseException errorResponseException) { + switch (errorResponseException.getErrorResponse()) { + case UNKNOWN_USER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", + actionName, targetId); + case UNKNOWN_MEMBER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", + actionName, targetId); + case UNKNOWN_ROLE -> logger.warn( + "Attempted to revoke a temporary {} but the {} role can not be found.", + actionName, actionName); + case MISSING_PERMISSIONS -> logger.warn( + "Attempted to revoke a temporary {} but the bot lacks permission.", + actionName); + case UNKNOWN_BAN -> logger.debug( + "Attempted to revoke a temporary {} but the user '{}' is not banned anymore.", + actionName, targetId); + default -> logUnknownFailure.run(); + } + } else { + logUnknownFailure.run(); + } + } + private @NotNull RevocableModerationAction getRevocableActionByType( @NotNull ModerationAction type) { return Objects.requireNonNull(typeToRevocableAction.get(type), diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java index bcb4437534..0236fd253c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java @@ -13,7 +13,7 @@ * {@link org.togetherjava.tjbot.commands.moderation.MuteCommand} and executed by * {@link TemporaryModerationRoutine}. */ -final class TemporaryMuteAction extends RevocableRoleBasedAction { +final class TemporaryMuteAction implements RevocableModerationAction { private final Config config; /** @@ -22,10 +22,14 @@ final class TemporaryMuteAction extends RevocableRoleBasedAction { * @param config the config to use to identify the muted role */ TemporaryMuteAction(@NotNull Config config) { - super("mute"); this.config = config; } + @Override + public @NotNull String actionName() { + return "mute"; + } + @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.MUTE; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java index d092d9eb7b..eb6eddb522 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java @@ -13,7 +13,7 @@ * {@link org.togetherjava.tjbot.commands.moderation.QuarantineCommand} and executed by * {@link TemporaryModerationRoutine}. */ -final class TemporaryQuarantineAction extends RevocableRoleBasedAction { +final class TemporaryQuarantineAction implements RevocableModerationAction { private final Config config; /** @@ -22,10 +22,14 @@ final class TemporaryQuarantineAction extends RevocableRoleBasedAction { * @param config the config to use to identify the quarantined role */ TemporaryQuarantineAction(@NotNull Config config) { - super("quarantine"); this.config = config; } + @Override + public @NotNull String actionName() { + return "quarantine"; + } + @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.QUARANTINE; From e99bd41999a28424be0062c74d009971a595a7ab Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:06:12 +0300 Subject: [PATCH 06/10] Revert "re-worked the temporary moderation action's failure handling" This reverts commit 47d48b05b8f715d59e363533afd28c618950414d. --- .../temp/RevocableModerationAction.java | 33 +++++++++++--- .../moderation/temp/TemporaryBanAction.java | 35 +++++++++++++-- .../temp/TemporaryModerationRoutine.java | 40 +++++------------ .../moderation/temp/TemporaryMuteAction.java | 42 +++++++++++++++--- .../temp/TemporaryQuarantineAction.java | 43 ++++++++++++++++--- 5 files changed, 142 insertions(+), 51 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java index 1d3a9847c1..b2cedc3007 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableModerationAction.java @@ -12,14 +12,21 @@ */ interface RevocableModerationAction { /** - * The name of the action, that will be logged in case of a failed revocation. - * - *

- * Naming convention examples: {@code "ban"}, {@code "mute"}, {@code "quarantine"} - *

+ * Classification of a revocation failure. */ - @NotNull - String actionName(); + @SuppressWarnings("PublicInnerClass") + enum FailureIdentification { + /** + * Acknowledges that the failure is known and has been handled. Hence, further error + * handling should not be continued. + */ + KNOWN, + /** + * The failure is unknown and has not been handled. Hence, further error handling should be + * continued. + */ + UNKNOWN + } /** * The type to apply the temporary action, such as @@ -50,4 +57,16 @@ interface RevocableModerationAction { @NotNull RestAction revokeAction(@NotNull Guild guild, @NotNull User target, @NotNull String reason); + + /** + * Handle a failure that might occur during revocation, i.e. execution of the action returned by + * {@link #revokeAction(Guild, User, String)}. + * + * @param failure the failure to handle + * @param targetId the id of the user who is targeted by the revocation + * @return a classification of the failure, decides whether the surrounding flow will continue + * to handle the error further or not + */ + @NotNull + FailureIdentification handleRevokeFailure(@NotNull Throwable failure, long targetId); } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java index 843d039025..0941224872 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryBanAction.java @@ -2,8 +2,12 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; /** @@ -12,10 +16,7 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryBanAction implements RevocableModerationAction { - @Override - public @NotNull String actionName() { - return "ban"; - } + private static final Logger logger = LoggerFactory.getLogger(TemporaryBanAction.class); @Override public @NotNull ModerationAction getApplyType() { @@ -32,4 +33,30 @@ final class TemporaryBanAction implements RevocableModerationAction { @NotNull String reason) { return guild.unban(target).reason(reason); } + + @Override + public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, + long targetId) { + if (failure instanceof ErrorResponseException errorResponseException) { + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + logger.debug( + "Attempted to revoke a temporary ban but user '{}' does not exist anymore.", + targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_BAN) { + logger.debug( + "Attempted to revoke a temporary ban but the user '{}' is not banned anymore.", + targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { + logger.warn("Attempted to revoke a temporary ban but the bot lacks permission."); + return FailureIdentification.KNOWN; + } + } + return FailureIdentification.UNKNOWN; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java index ec6991c69d..26671c196e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryModerationRoutine.java @@ -3,7 +3,6 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -123,8 +122,7 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { jda.retrieveUserById(groupIdentifier.targetId) .flatMap(target -> executeRevocation(guild, target, groupIdentifier.type)) .queue(result -> { - }, failure -> handleFailure(failure, groupIdentifier.targetId, - getRevocableActionByType(groupIdentifier.type).actionName())); + }, failure -> handleFailure(failure, groupIdentifier)); } private @NotNull RestAction executeRevocation(@NotNull Guild guild, @NotNull User target, @@ -140,34 +138,16 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) { return action.revokeAction(guild, target, reason); } - private void handleFailure(@NotNull Throwable failure, long targetId, - @NotNull String actionName) { - Runnable logUnknownFailure = () -> logger.warn( - "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", - targetId, failure); - - if (failure instanceof ErrorResponseException errorResponseException) { - switch (errorResponseException.getErrorResponse()) { - case UNKNOWN_USER -> logger.debug( - "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", - actionName, targetId); - case UNKNOWN_MEMBER -> logger.debug( - "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", - actionName, targetId); - case UNKNOWN_ROLE -> logger.warn( - "Attempted to revoke a temporary {} but the {} role can not be found.", - actionName, actionName); - case MISSING_PERMISSIONS -> logger.warn( - "Attempted to revoke a temporary {} but the bot lacks permission.", - actionName); - case UNKNOWN_BAN -> logger.debug( - "Attempted to revoke a temporary {} but the user '{}' is not banned anymore.", - actionName, targetId); - default -> logUnknownFailure.run(); - } - } else { - logUnknownFailure.run(); + private void handleFailure(@NotNull Throwable failure, + @NotNull RevocationGroupIdentifier groupIdentifier) { + if (getRevocableActionByType(groupIdentifier.type).handleRevokeFailure(failure, + groupIdentifier.targetId) == RevocableModerationAction.FailureIdentification.KNOWN) { + return; } + + logger.warn( + "Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.", + groupIdentifier.targetId, failure); } private @NotNull RevocableModerationAction getRevocableActionByType( diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java index 0236fd253c..f7c0d29a58 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java @@ -2,8 +2,12 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; @@ -14,6 +18,7 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryMuteAction implements RevocableModerationAction { + private static final Logger logger = LoggerFactory.getLogger(TemporaryMuteAction.class); private final Config config; /** @@ -25,11 +30,6 @@ final class TemporaryMuteAction implements RevocableModerationAction { this.config = config; } - @Override - public @NotNull String actionName() { - return "mute"; - } - @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.MUTE; @@ -48,4 +48,36 @@ final class TemporaryMuteAction implements RevocableModerationAction { ModerationUtils.getMutedRole(guild, config).orElseThrow()) .reason(reason); } + + @Override + public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, + long targetId) { + if (failure instanceof ErrorResponseException errorResponseException) { + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + logger.debug( + "Attempted to revoke a temporary mute but user '{}' does not exist anymore.", + targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { + logger.debug( + "Attempted to revoke a temporary mute but user '{}' is not a member of the guild anymore.", + targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { + logger.warn( + "Attempted to revoke a temporary mute but the mute role can not be found."); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { + logger.warn("Attempted to revoke a temporary mute but the bot lacks permission."); + return FailureIdentification.KNOWN; + } + } + return FailureIdentification.UNKNOWN; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java index eb6eddb522..dd87658551 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java @@ -2,8 +2,12 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; @@ -14,6 +18,7 @@ * {@link TemporaryModerationRoutine}. */ final class TemporaryQuarantineAction implements RevocableModerationAction { + private static final Logger logger = LoggerFactory.getLogger(TemporaryQuarantineAction.class); private final Config config; /** @@ -25,11 +30,6 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { this.config = config; } - @Override - public @NotNull String actionName() { - return "quarantine"; - } - @Override public @NotNull ModerationAction getApplyType() { return ModerationAction.QUARANTINE; @@ -48,4 +48,37 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) .reason(reason); } + + @Override + public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, + long targetId) { + if (failure instanceof ErrorResponseException errorResponseException) { + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + logger.debug( + "Attempted to revoke a temporary quarantine but user '{}' does not exist anymore.", + targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { + logger.debug( + "Attempted to revoke a temporary quarantine but user '{}' is not a member of the guild anymore.", + targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { + logger.warn( + "Attempted to revoke a temporary quarantine but the quarantine role can not be found."); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { + logger.warn( + "Attempted to revoke a temporary quarantine but the bot lacks permission."); + return FailureIdentification.KNOWN; + } + } + return FailureIdentification.UNKNOWN; + } } From 4427e8f8b4436fb0b4beb9fb6c1f4614a22a677e Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:21:57 +0300 Subject: [PATCH 07/10] rewrote this, without making the code worse. --- .../temp/RevocableRoleBasedAction.java | 61 +++++++++++++++++++ .../moderation/temp/TemporaryMuteAction.java | 41 +------------ .../temp/TemporaryQuarantineAction.java | 42 +------------ 3 files changed, 67 insertions(+), 77 deletions(-) create mode 100644 application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java new file mode 100644 index 0000000000..6ecb1d182e --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -0,0 +1,61 @@ +package org.togetherjava.tjbot.commands.moderation.temp; + +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.requests.ErrorResponse; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Role based moderation actions that can be revoked, for example a {@link TemporaryMuteAction} or a + * {@link TemporaryBanAction}, which are applied implicitly purely by the presence of a role. + */ +abstract class RevocableRoleBasedAction implements RevocableModerationAction { + private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class); + + private final String actionName; + + /** + * Creates a new role based action. + * + * @param actionName the action name to be used in logging in case of a failure, e.g. + * {@code "mute"}, {@code "quarantine"} + */ + public RevocableRoleBasedAction(@NotNull String actionName) { + this.actionName = actionName; + } + + @Override + public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, + long targetId) { + + if (failure instanceof ErrorResponseException errorResponseException) { + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { + logger.debug( + "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", + actionName, targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { + logger.debug( + "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", + actionName, targetId); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { + logger.warn("Attempted to revoke a temporary {} but the {} role can not be found.", + actionName, actionName); + return FailureIdentification.KNOWN; + } + + if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { + logger.warn("Attempted to revoke a temporary {} but the bot lacks permission.", + actionName); + return FailureIdentification.KNOWN; + } + } + return FailureIdentification.UNKNOWN; + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java index f7c0d29a58..921fe8b06a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java @@ -2,12 +2,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; @@ -17,8 +13,7 @@ * {@link org.togetherjava.tjbot.commands.moderation.MuteCommand} and executed by * {@link TemporaryModerationRoutine}. */ -final class TemporaryMuteAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(TemporaryMuteAction.class); +final class TemporaryMuteAction extends RevocableRoleBasedAction { private final Config config; /** @@ -27,6 +22,8 @@ final class TemporaryMuteAction implements RevocableModerationAction { * @param config the config to use to identify the muted role */ TemporaryMuteAction(@NotNull Config config) { + super("mute"); + this.config = config; } @@ -48,36 +45,4 @@ final class TemporaryMuteAction implements RevocableModerationAction { ModerationUtils.getMutedRole(guild, config).orElseThrow()) .reason(reason); } - - @Override - public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, - long targetId) { - if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( - "Attempted to revoke a temporary mute but user '{}' does not exist anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { - logger.debug( - "Attempted to revoke a temporary mute but user '{}' is not a member of the guild anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { - logger.warn( - "Attempted to revoke a temporary mute but the mute role can not be found."); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { - logger.warn("Attempted to revoke a temporary mute but the bot lacks permission."); - return FailureIdentification.KNOWN; - } - } - return FailureIdentification.UNKNOWN; - } } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java index dd87658551..2e8c7908a9 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java @@ -2,12 +2,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.requests.ErrorResponse; import net.dv8tion.jda.api.requests.RestAction; import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.moderation.ModerationAction; import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; @@ -17,8 +13,7 @@ * {@link org.togetherjava.tjbot.commands.moderation.QuarantineCommand} and executed by * {@link TemporaryModerationRoutine}. */ -final class TemporaryQuarantineAction implements RevocableModerationAction { - private static final Logger logger = LoggerFactory.getLogger(TemporaryQuarantineAction.class); +final class TemporaryQuarantineAction extends RevocableRoleBasedAction { private final Config config; /** @@ -27,6 +22,8 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { * @param config the config to use to identify the quarantined role */ TemporaryQuarantineAction(@NotNull Config config) { + super("quarantine"); + this.config = config; } @@ -48,37 +45,4 @@ final class TemporaryQuarantineAction implements RevocableModerationAction { ModerationUtils.getQuarantinedRole(guild, config).orElseThrow()) .reason(reason); } - - @Override - public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, - long targetId) { - if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( - "Attempted to revoke a temporary quarantine but user '{}' does not exist anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { - logger.debug( - "Attempted to revoke a temporary quarantine but user '{}' is not a member of the guild anymore.", - targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { - logger.warn( - "Attempted to revoke a temporary quarantine but the quarantine role can not be found."); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { - logger.warn( - "Attempted to revoke a temporary quarantine but the bot lacks permission."); - return FailureIdentification.KNOWN; - } - } - return FailureIdentification.UNKNOWN; - } } From 8f48b1498914de5f0cf51158b66abfa02c275a4f Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:36:20 +0300 Subject: [PATCH 08/10] removed `public` from constructor --- .../commands/moderation/temp/RevocableRoleBasedAction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java index 6ecb1d182e..d01a3b30e5 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -21,7 +21,7 @@ abstract class RevocableRoleBasedAction implements RevocableModerationAction { * @param actionName the action name to be used in logging in case of a failure, e.g. * {@code "mute"}, {@code "quarantine"} */ - public RevocableRoleBasedAction(@NotNull String actionName) { + RevocableRoleBasedAction(@NotNull String actionName) { this.actionName = actionName; } From f7b62d3fccc1c692b78cbb4fccec8bcc8eb111ab Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 31 Mar 2022 12:06:05 +0300 Subject: [PATCH 09/10] switched to using a `switch` --- .../temp/RevocableRoleBasedAction.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java index d01a3b30e5..76fb08ef69 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -1,7 +1,6 @@ package org.togetherjava.tjbot.commands.moderation.temp; import net.dv8tion.jda.api.exceptions.ErrorResponseException; -import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,32 +29,34 @@ abstract class RevocableRoleBasedAction implements RevocableModerationAction { long targetId) { if (failure instanceof ErrorResponseException errorResponseException) { - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) { - logger.debug( - "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", - actionName, targetId); - return FailureIdentification.KNOWN; + switch (errorResponseException.getErrorResponse()) { + case UNKNOWN_USER -> { + logger.debug( + "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", + actionName, targetId); + } + case UNKNOWN_MEMBER -> { + logger.debug( + "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", + actionName, targetId); + } + case UNKNOWN_ROLE -> { + logger.warn( + "Attempted to revoke a temporary {} but the {} role can not be found.", + actionName, actionName); + } + case MISSING_PERMISSIONS -> { + logger.warn("Attempted to revoke a temporary {} but the bot lacks permission.", + actionName); + } + default -> { + return FailureIdentification.UNKNOWN; + } } - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) { - logger.debug( - "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", - actionName, targetId); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) { - logger.warn("Attempted to revoke a temporary {} but the {} role can not be found.", - actionName, actionName); - return FailureIdentification.KNOWN; - } - - if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) { - logger.warn("Attempted to revoke a temporary {} but the bot lacks permission.", - actionName); - return FailureIdentification.KNOWN; - } + return FailureIdentification.KNOWN; } + return FailureIdentification.UNKNOWN; } } From 74bd540433af0850ef024cf1c9da09e4cd4ab870 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Thu, 31 Mar 2022 12:26:10 +0300 Subject: [PATCH 10/10] unwrapped case blocks --- .../temp/RevocableRoleBasedAction.java | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java index 76fb08ef69..d289b00f49 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -30,25 +30,18 @@ abstract class RevocableRoleBasedAction implements RevocableModerationAction { if (failure instanceof ErrorResponseException errorResponseException) { switch (errorResponseException.getErrorResponse()) { - case UNKNOWN_USER -> { - logger.debug( - "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", - actionName, targetId); - } - case UNKNOWN_MEMBER -> { - logger.debug( - "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", - actionName, targetId); - } - case UNKNOWN_ROLE -> { - logger.warn( - "Attempted to revoke a temporary {} but the {} role can not be found.", - actionName, actionName); - } - case MISSING_PERMISSIONS -> { - logger.warn("Attempted to revoke a temporary {} but the bot lacks permission.", - actionName); - } + case UNKNOWN_USER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' does not exist anymore.", + actionName, targetId); + case UNKNOWN_MEMBER -> logger.debug( + "Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.", + actionName, targetId); + case UNKNOWN_ROLE -> logger.warn( + "Attempted to revoke a temporary {} but the {} role can not be found.", + actionName, actionName); + case MISSING_PERMISSIONS -> logger.warn( + "Attempted to revoke a temporary {} but the bot lacks permission.", + actionName); default -> { return FailureIdentification.UNKNOWN; }