-
-
Notifications
You must be signed in to change notification settings - Fork 104
Removed code duplication from role based moderation actions. #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
47d48b0
re-worked the temporary moderation action's failure handling
interacsion 88212b6
reverted most logic, introduced `RevocableRoleBasedAction`
interacsion 0f707a4
javadoc + reordered methods
interacsion a74e2a1
Revert "javadoc + reordered methods"
interacsion 01c0dee
Revert "reverted most logic, introduced `RevocableRoleBasedAction`"
interacsion e99bd41
Revert "re-worked the temporary moderation action's failure handling"
interacsion 4427e8f
rewrote this, without making the code worse.
interacsion 8f48b14
removed `public` from constructor
interacsion f7b62d3
switched to using a `switch`
interacsion 74bd540
unwrapped case blocks
interacsion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...c/main/java/org/togetherjava/tjbot/commands/moderation/temp/RevocableRoleBasedAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.