Skip to content

Commit 4427e8f

Browse files
committed
rewrote this, without making the code worse.
1 parent e99bd41 commit 4427e8f

File tree

3 files changed

+67
-77
lines changed

3 files changed

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

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)