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..d289b00f49 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java @@ -0,0 +1,55 @@ +package org.togetherjava.tjbot.commands.moderation.temp; + +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +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"} + */ + RevocableRoleBasedAction(@NotNull String actionName) { + this.actionName = actionName; + } + + @Override + public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure, + long targetId) { + + 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); + default -> { + return FailureIdentification.UNKNOWN; + } + } + + 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; - } }