Skip to content

Commit 88212b6

Browse files
committed
reverted most logic, introduced RevocableRoleBasedAction
1 parent 47d48b0 commit 88212b6

File tree

6 files changed

+91
-58
lines changed

6 files changed

+91
-58
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.dv8tion.jda.api.entities.Guild;
44
import net.dv8tion.jda.api.entities.User;
5+
import net.dv8tion.jda.api.requests.ErrorResponse;
56
import net.dv8tion.jda.api.requests.RestAction;
67
import org.jetbrains.annotations.NotNull;
78
import org.togetherjava.tjbot.commands.moderation.ModerationAction;
@@ -11,16 +12,6 @@
1112
* {@link TemporaryModerationRoutine} to identify and revoke such actions.
1213
*/
1314
interface RevocableModerationAction {
14-
/**
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>
20-
*/
21-
@NotNull
22-
String actionName();
23-
2415
/**
2516
* The type to apply the temporary action, such as
2617
* {@link net.dv8tion.jda.api.audit.ActionType#BAN}.
@@ -50,4 +41,14 @@ interface RevocableModerationAction {
5041
@NotNull
5142
RestAction<Void> revokeAction(@NotNull Guild guild, @NotNull User target,
5243
@NotNull String reason);
44+
45+
/**
46+
* Handle a failure that might occur during revocation, i.e. execution of the action returned by
47+
* {@link #revokeAction(Guild, User, String)}.
48+
*
49+
* @param errorResponse the error that occurred
50+
* @param targetId the id of the user who is targeted by the revocation
51+
* @return whether it should log an unknown failure message or not
52+
*/
53+
boolean handleRevokeFailure(@NotNull ErrorResponse errorResponse, long targetId);
5354
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.togetherjava.tjbot.commands.moderation.temp;
2+
3+
import net.dv8tion.jda.api.requests.ErrorResponse;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
abstract class RevocableRoleBasedAction implements RevocableModerationAction {
9+
private static final Logger logger = LoggerFactory.getLogger(RevocableRoleBasedAction.class);
10+
11+
private final String actionName;
12+
13+
RevocableRoleBasedAction(String actionName) {
14+
this.actionName = actionName;
15+
}
16+
17+
@Override
18+
public boolean handleRevokeFailure(@NotNull ErrorResponse error, long targetId) {
19+
switch (error) {
20+
case UNKNOWN_USER -> logger.debug(
21+
"Attempted to revoke a temporary {} but user '{}' does not exist anymore.",
22+
actionName, targetId);
23+
case UNKNOWN_MEMBER -> logger.debug(
24+
"Attempted to revoke a temporary {} but user '{}' is not a member of the guild anymore.",
25+
actionName, targetId);
26+
case UNKNOWN_ROLE -> logger.warn(
27+
"Attempted to revoke a temporary {} but the {} role can not be found.",
28+
actionName, actionName);
29+
case MISSING_PERMISSIONS -> logger.warn(
30+
"Attempted to revoke a temporary {} but the bot lacks permission.", actionName);
31+
default -> {
32+
return true;
33+
}
34+
}
35+
36+
return false;
37+
}
38+
}

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import net.dv8tion.jda.api.entities.Guild;
44
import net.dv8tion.jda.api.entities.User;
5+
import net.dv8tion.jda.api.requests.ErrorResponse;
56
import net.dv8tion.jda.api.requests.RestAction;
67
import org.jetbrains.annotations.NotNull;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
710
import org.togetherjava.tjbot.commands.moderation.ModerationAction;
811

912
/**
@@ -12,10 +15,7 @@
1215
* {@link TemporaryModerationRoutine}.
1316
*/
1417
final class TemporaryBanAction implements RevocableModerationAction {
15-
@Override
16-
public @NotNull String actionName() {
17-
return "ban";
18-
}
18+
private static final Logger logger = LoggerFactory.getLogger(TemporaryBanAction.class);
1919

2020
@Override
2121
public @NotNull ModerationAction getApplyType() {
@@ -32,4 +32,23 @@ final class TemporaryBanAction implements RevocableModerationAction {
3232
@NotNull String reason) {
3333
return guild.unban(target).reason(reason);
3434
}
35+
36+
@Override
37+
public boolean handleRevokeFailure(@NotNull ErrorResponse error, long targetId) {
38+
switch (error) {
39+
case UNKNOWN_USER -> logger.debug(
40+
"Attempted to revoke a temporary ban but user '{}' does not exist anymore.",
41+
targetId);
42+
case MISSING_PERMISSIONS -> logger
43+
.warn("Attempted to revoke a temporary ban but the bot lacks permission.");
44+
case UNKNOWN_BAN -> logger.debug(
45+
"Attempted to revoke a temporary ban but the user '{}' is not banned anymore.",
46+
targetId);
47+
default -> {
48+
return true;
49+
}
50+
}
51+
52+
return false;
53+
}
3554
}

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

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,21 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) {
123123
jda.retrieveUserById(groupIdentifier.targetId)
124124
.flatMap(target -> executeRevocation(guild, target, groupIdentifier.type))
125125
.queue(result -> {
126-
}, failure -> handleFailure(failure, groupIdentifier.targetId,
127-
getRevocableActionByType(groupIdentifier.type).actionName()));
126+
}, failure -> handleFailure(failure, groupIdentifier));
127+
}
128+
129+
private void handleFailure(@NotNull Throwable failure,
130+
@NotNull RevocationGroupIdentifier groupIdentifier) {
131+
if (failure instanceof ErrorResponseException errorResponseException
132+
&& !getRevocableActionByType(groupIdentifier.type).handleRevokeFailure(
133+
errorResponseException.getErrorResponse(), groupIdentifier.targetId)) {
134+
135+
return;
136+
}
137+
138+
logger.warn(
139+
"Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.",
140+
groupIdentifier.targetId, failure);
128141
}
129142

130143
private @NotNull RestAction<Void> executeRevocation(@NotNull Guild guild, @NotNull User target,
@@ -140,36 +153,6 @@ private void revokeAction(@NotNull RevocationGroupIdentifier groupIdentifier) {
140153
return action.revokeAction(guild, target, reason);
141154
}
142155

143-
private void handleFailure(@NotNull Throwable failure, long targetId,
144-
@NotNull String actionName) {
145-
Runnable logUnknownFailure = () -> logger.warn(
146-
"Attempted to revoke a temporary moderation action for user '{}' but something unexpected went wrong.",
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-
}
171-
}
172-
173156
private @NotNull RevocableModerationAction getRevocableActionByType(
174157
@NotNull ModerationAction type) {
175158
return Objects.requireNonNull(typeToRevocableAction.get(type),

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* {@link org.togetherjava.tjbot.commands.moderation.MuteCommand} and executed by
1414
* {@link TemporaryModerationRoutine}.
1515
*/
16-
final class TemporaryMuteAction implements RevocableModerationAction {
16+
final class TemporaryMuteAction extends RevocableRoleBasedAction {
1717
private final Config config;
1818

1919
/**
@@ -22,14 +22,10 @@ final class TemporaryMuteAction implements RevocableModerationAction {
2222
* @param config the config to use to identify the muted role
2323
*/
2424
TemporaryMuteAction(@NotNull Config config) {
25+
super("mute");
2526
this.config = config;
2627
}
2728

28-
@Override
29-
public @NotNull String actionName() {
30-
return "mute";
31-
}
32-
3329
@Override
3430
public @NotNull ModerationAction getApplyType() {
3531
return ModerationAction.MUTE;

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* {@link org.togetherjava.tjbot.commands.moderation.QuarantineCommand} and executed by
1414
* {@link TemporaryModerationRoutine}.
1515
*/
16-
final class TemporaryQuarantineAction implements RevocableModerationAction {
16+
final class TemporaryQuarantineAction extends RevocableRoleBasedAction {
1717
private final Config config;
1818

1919
/**
@@ -22,14 +22,10 @@ final class TemporaryQuarantineAction implements RevocableModerationAction {
2222
* @param config the config to use to identify the quarantined role
2323
*/
2424
TemporaryQuarantineAction(@NotNull Config config) {
25+
super("quarantine");
2526
this.config = config;
2627
}
2728

28-
@Override
29-
public @NotNull String actionName() {
30-
return "quarantine";
31-
}
32-
3329
@Override
3430
public @NotNull ModerationAction getApplyType() {
3531
return ModerationAction.QUARANTINE;

0 commit comments

Comments
 (0)