Skip to content

Commit 47d48b0

Browse files
committed
re-worked the temporary moderation action's failure handling
1 parent 3ea40dc commit 47d48b0

File tree

5 files changed

+51
-142
lines changed

5 files changed

+51
-142
lines changed

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,14 @@
1212
*/
1313
interface RevocableModerationAction {
1414
/**
15-
* Classification of a revocation failure.
15+
* The name of the action, that will be logged in case of a failed revocation.
16+
*
17+
* <p>
18+
* Naming convention examples: {@code "ban"}, {@code "mute"}, {@code "quarantine"}
19+
* </p>
1620
*/
17-
@SuppressWarnings("PublicInnerClass")
18-
enum FailureIdentification {
19-
/**
20-
* Acknowledges that the failure is known and has been handled. Hence, further error
21-
* handling should not be continued.
22-
*/
23-
KNOWN,
24-
/**
25-
* The failure is unknown and has not been handled. Hence, further error handling should be
26-
* continued.
27-
*/
28-
UNKNOWN
29-
}
21+
@NotNull
22+
String actionName();
3023

3124
/**
3225
* The type to apply the temporary action, such as
@@ -57,16 +50,4 @@ enum FailureIdentification {
5750
@NotNull
5851
RestAction<Void> revokeAction(@NotNull Guild guild, @NotNull User target,
5952
@NotNull String reason);
60-
61-
/**
62-
* Handle a failure that might occur during revocation, i.e. execution of the action returned by
63-
* {@link #revokeAction(Guild, User, String)}.
64-
*
65-
* @param failure the failure to handle
66-
* @param targetId the id of the user who is targeted by the revocation
67-
* @return a classification of the failure, decides whether the surrounding flow will continue
68-
* to handle the error further or not
69-
*/
70-
@NotNull
71-
FailureIdentification handleRevokeFailure(@NotNull Throwable failure, long targetId);
7253
}

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

Lines changed: 4 additions & 31 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

139
/**
@@ -16,7 +12,10 @@
1612
* {@link TemporaryModerationRoutine}.
1713
*/
1814
final class TemporaryBanAction implements RevocableModerationAction {
19-
private static final Logger logger = LoggerFactory.getLogger(TemporaryBanAction.class);
15+
@Override
16+
public @NotNull String actionName() {
17+
return "ban";
18+
}
2019

2120
@Override
2221
public @NotNull ModerationAction getApplyType() {
@@ -33,30 +32,4 @@ final class TemporaryBanAction implements RevocableModerationAction {
3332
@NotNull String reason) {
3433
return guild.unban(target).reason(reason);
3534
}
36-
37-
@Override
38-
public @NotNull FailureIdentification handleRevokeFailure(@NotNull Throwable failure,
39-
long targetId) {
40-
if (failure instanceof ErrorResponseException errorResponseException) {
41-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_USER) {
42-
logger.debug(
43-
"Attempted to revoke a temporary ban but user '{}' does not exist anymore.",
44-
targetId);
45-
return FailureIdentification.KNOWN;
46-
}
47-
48-
if (errorResponseException.getErrorResponse() == ErrorResponse.UNKNOWN_BAN) {
49-
logger.debug(
50-
"Attempted to revoke a temporary ban but the user '{}' is not banned anymore.",
51-
targetId);
52-
return FailureIdentification.KNOWN;
53-
}
54-
55-
if (errorResponseException.getErrorResponse() == ErrorResponse.MISSING_PERMISSIONS) {
56-
logger.warn("Attempted to revoke a temporary ban but the bot lacks permission.");
57-
return FailureIdentification.KNOWN;
58-
}
59-
}
60-
return FailureIdentification.UNKNOWN;
61-
}
6235
}

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.dv8tion.jda.api.JDA;
44
import net.dv8tion.jda.api.entities.Guild;
55
import net.dv8tion.jda.api.entities.User;
6+
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
67
import net.dv8tion.jda.api.requests.RestAction;
78
import org.jetbrains.annotations.NotNull;
89
import org.slf4j.Logger;
@@ -122,7 +123,8 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) {
122123
jda.retrieveUserById(groupIdentifier.targetId)
123124
.flatMap(target -> executeRevocation(guild, target, groupIdentifier.type))
124125
.queue(result -> {
125-
}, failure -> handleFailure(failure, groupIdentifier));
126+
}, failure -> handleFailure(failure, groupIdentifier.targetId,
127+
getRevocableActionByType(groupIdentifier.type).actionName()));
126128
}
127129

128130
private @NotNull RestAction<Void> executeRevocation(@NotNull Guild guild, @NotNull User target,
@@ -138,16 +140,34 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) {
138140
return action.revokeAction(guild, target, reason);
139141
}
140142

141-
private void handleFailure(@NotNull Throwable failure,
142-
@NotNull RevocationGroupIdentifier groupIdentifier) {
143-
if (getRevocableActionByType(groupIdentifier.type).handleRevokeFailure(failure,
144-
groupIdentifier.targetId) == RevocableModerationAction.FailureIdentification.KNOWN) {
145-
return;
146-
}
147-
148-
logger.warn(
143+
private void handleFailure(@NotNull Throwable failure, long targetId,
144+
@NotNull String actionName) {
145+
Runnable logUnknownFailure = () -> logger.warn(
149146
"Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.",
150-
groupIdentifier.targetId, failure);
147+
targetId, failure);
148+
149+
if (failure instanceof ErrorResponseException errorResponseException) {
150+
switch (errorResponseException.getErrorResponse()) {
151+
case UNKNOWN_USER -> logger.debug(
152+
"Attempted to revoke a temporary {} but user '{}' does not exist anymore.",
153+
actionName, targetId);
154+
case UNKNOWN_MEMBER -> logger.debug(
155+
"Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.",
156+
actionName, targetId);
157+
case UNKNOWN_ROLE -> logger.warn(
158+
"Attempted to revoke a temporary {} but the {} role can not be found.",
159+
actionName, actionName);
160+
case MISSING_PERMISSIONS -> logger.warn(
161+
"Attempted to revoke a temporary {} but the bot lacks permission.",
162+
actionName);
163+
case UNKNOWN_BAN -> logger.debug(
164+
"Attempted to revoke a temporary {} but the user '{}' is not banned anymore.",
165+
actionName, targetId);
166+
default -> logUnknownFailure.run();
167+
}
168+
} else {
169+
logUnknownFailure.run();
170+
}
151171
}
152172

153173
private @NotNull RevocableModerationAction getRevocableActionByType(

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

Lines changed: 5 additions & 37 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;
@@ -18,7 +14,6 @@
1814
* {@link TemporaryModerationRoutine}.
1915
*/
2016
final class TemporaryMuteAction implements RevocableModerationAction {
21-
private static final Logger logger = LoggerFactory.getLogger(TemporaryMuteAction.class);
2217
private final Config config;
2318

2419
/**
@@ -30,6 +25,11 @@ final class TemporaryMuteAction implements RevocableModerationAction {
3025
this.config = config;
3126
}
3227

28+
@Override
29+
public @NotNull String actionName() {
30+
return "mute";
31+
}
32+
3333
@Override
3434
public @NotNull ModerationAction getApplyType() {
3535
return ModerationAction.MUTE;
@@ -48,36 +48,4 @@ final class TemporaryMuteAction implements RevocableModerationAction {
4848
ModerationUtils.getMutedRole(guild, config).orElseThrow())
4949
.reason(reason);
5050
}
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-
}
8351
}

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

Lines changed: 5 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;
@@ -18,7 +14,6 @@
1814
* {@link TemporaryModerationRoutine}.
1915
*/
2016
final class TemporaryQuarantineAction implements RevocableModerationAction {
21-
private static final Logger logger = LoggerFactory.getLogger(TemporaryQuarantineAction.class);
2217
private final Config config;
2318

2419
/**
@@ -30,6 +25,11 @@ final class TemporaryQuarantineAction implements RevocableModerationAction {
3025
this.config = config;
3126
}
3227

28+
@Override
29+
public @NotNull String actionName() {
30+
return "quarantine";
31+
}
32+
3333
@Override
3434
public @NotNull ModerationAction getApplyType() {
3535
return ModerationAction.QUARANTINE;
@@ -48,37 +48,4 @@ final class TemporaryQuarantineAction implements RevocableModerationAction {
4848
ModerationUtils.getQuarantinedRole(guild, config).orElseThrow())
4949
.reason(reason);
5050
}
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-
}
8451
}

0 commit comments

Comments
 (0)