Skip to content

Commit e5cd2c2

Browse files
authored
Removed code duplication from role based moderation actions. (#417)
1 parent 3ea40dc commit e5cd2c2

File tree

3 files changed

+61
-77
lines changed

3 files changed

+61
-77
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.togetherjava.tjbot.commands.moderation.temp;
2+
3+
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
/**
9+
* Role based moderation actions that can be revoked, for example a {@link TemporaryMuteAction} or a
10+
* {@link TemporaryBanAction}, which are applied implicitly purely by the presence of a role.
11+
*/
12+
abstract class RevocableRoleBasedAction implements RevocableModerationAction {
13+
private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class);
14+
15+
private final String actionName;
16+
17+
/**
18+
* Creates a new role based action.
19+
*
20+
* @param actionName the action name to be used in logging in case of a failure, e.g.
21+
* {@code "mute"}, {@code "quarantine"}
22+
*/
23+
RevocableRoleBasedAction(@NotNull String actionName) {
24+
this.actionName = actionName;
25+
}
26+
27+
@Override
28+
public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure,
29+
long targetId) {
30+
31+
if (failure instanceof ErrorResponseException errorResponseException) {
32+
switch (errorResponseException.getErrorResponse()) {
33+
case UNKNOWN_USER -> logger.debug(
34+
"Attempted to revoke a temporary {} but user '{}' does not exist anymore.",
35+
actionName, targetId);
36+
case UNKNOWN_MEMBER -> logger.debug(
37+
"Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.",
38+
actionName, targetId);
39+
case UNKNOWN_ROLE -> logger.warn(
40+
"Attempted to revoke a temporary {} but the {} role can not be found.",
41+
actionName, actionName);
42+
case MISSING_PERMISSIONS -> logger.warn(
43+
"Attempted to revoke a temporary {} but the bot lacks permission.",
44+
actionName);
45+
default -> {
46+
return FailureIdentification.UNKNOWN;
47+
}
48+
}
49+
50+
return FailureIdentification.KNOWN;
51+
}
52+
53+
return FailureIdentification.UNKNOWN;
54+
}
55+
}

application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryMuteAction.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
import net.dv8tion.jda.api.entities.Guild;
44
import net.dv8tion.jda.api.entities.User;
5-
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
6-
import net.dv8tion.jda.api.requests.ErrorResponse;
75
import net.dv8tion.jda.api.requests.RestAction;
86
import org.jetbrains.annotations.NotNull;
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
117
import org.togetherjava.tjbot.commands.moderation.ModerationAction;
128
import org.togetherjava.tjbot.commands.moderation.ModerationUtils;
139
import org.togetherjava.tjbot.config.Config;
@@ -17,8 +13,7 @@
1713
* {@link org.togetherjava.tjbot.commands.moderation.MuteCommand} and executed by
1814
* {@link TemporaryModerationRoutine}.
1915
*/
20-
final class TemporaryMuteAction implements RevocableModerationAction {
21-
private static final Logger logger = LoggerFactory.getLogger(TemporaryMuteAction.class);
16+
final class TemporaryMuteAction extends RevocableRoleBasedAction {
2217
private final Config config;
2318

2419
/**
@@ -27,6 +22,8 @@ final class TemporaryMuteAction implements RevocableModerationAction {
2722
* @param config the config to use to identify the muted role
2823
*/
2924
TemporaryMuteAction(@NotNull Config config) {
25+
super("mute");
26+
3027
this.config = config;
3128
}
3229

@@ -48,36 +45,4 @@ final class TemporaryMuteAction implements RevocableModerationAction {
4845
ModerationUtils.getMutedRole(guild, config).orElseThrow())
4946
.reason(reason);
5047
}
51-
52-
@Override
53-
public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure,
54-
long targetId) {
55-
if (failure instanceof ErrorResponseException errorResponseException) {
56-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
57-
logger.debug(
58-
"Attempted to revoke a temporary mute but user '{}' does not exist anymore.",
59-
targetId);
60-
return FailureIdentification.KNOWN;
61-
}
62-
63-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) {
64-
logger.debug(
65-
"Attempted to revoke a temporary mute but user '{}' is not a member of the guild anymore.",
66-
targetId);
67-
return FailureIdentification.KNOWN;
68-
}
69-
70-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) {
71-
logger.warn(
72-
"Attempted to revoke a temporary mute but the mute role can not be found.");
73-
return FailureIdentification.KNOWN;
74-
}
75-
76-
if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) {
77-
logger.warn("Attempted to revoke a temporary mute but the bot lacks permission.");
78-
return FailureIdentification.KNOWN;
79-
}
80-
}
81-
return FailureIdentification.UNKNOWN;
82-
}
8348
}

application/src/main/java/org/togetherjava/tjbot/commands/moderation/temp/TemporaryQuarantineAction.java

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
import net.dv8tion.jda.api.entities.Guild;
44
import net.dv8tion.jda.api.entities.User;
5-
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
6-
import net.dv8tion.jda.api.requests.ErrorResponse;
75
import net.dv8tion.jda.api.requests.RestAction;
86
import org.jetbrains.annotations.NotNull;
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
117
import org.togetherjava.tjbot.commands.moderation.ModerationAction;
128
import org.togetherjava.tjbot.commands.moderation.ModerationUtils;
139
import org.togetherjava.tjbot.config.Config;
@@ -17,8 +13,7 @@
1713
* {@link org.togetherjava.tjbot.commands.moderation.QuarantineCommand} and executed by
1814
* {@link TemporaryModerationRoutine}.
1915
*/
20-
final class TemporaryQuarantineAction implements RevocableModerationAction {
21-
private static final Logger logger = LoggerFactory.getLogger(TemporaryQuarantineAction.class);
16+
final class TemporaryQuarantineAction extends RevocableRoleBasedAction {
2217
private final Config config;
2318

2419
/**
@@ -27,6 +22,8 @@ final class TemporaryQuarantineAction implements RevocableModerationAction {
2722
* @param config the config to use to identify the quarantined role
2823
*/
2924
TemporaryQuarantineAction(@NotNull Config config) {
25+
super("quarantine");
26+
3027
this.config = config;
3128
}
3229

@@ -48,37 +45,4 @@ final class TemporaryQuarantineAction implements RevocableModerationAction {
4845
ModerationUtils.getQuarantinedRole(guild, config).orElseThrow())
4946
.reason(reason);
5047
}
51-
52-
@Override
53-
public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure,
54-
long targetId) {
55-
if (failure instanceof ErrorResponseException errorResponseException) {
56-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
57-
logger.debug(
58-
"Attempted to revoke a temporary quarantine but user '{}' does not exist anymore.",
59-
targetId);
60-
return FailureIdentification.KNOWN;
61-
}
62-
63-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_MEMBER) {
64-
logger.debug(
65-
"Attempted to revoke a temporary quarantine but user '{}' is not a member of the guild anymore.",
66-
targetId);
67-
return FailureIdentification.KNOWN;
68-
}
69-
70-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_ROLE) {
71-
logger.warn(
72-
"Attempted to revoke a temporary quarantine but the quarantine role can not be found.");
73-
return FailureIdentification.KNOWN;
74-
}
75-
76-
if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) {
77-
logger.warn(
78-
"Attempted to revoke a temporary quarantine but the bot lacks permission.");
79-
return FailureIdentification.KNOWN;
80-
}
81-
}
82-
return FailureIdentification.UNKNOWN;
83-
}
8448
}

0 commit comments

Comments
 (0)