-
-
Notifications
You must be signed in to change notification settings - Fork 89
Migrate /warn command #249
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
Zabuzard
merged 53 commits into
Together-Java:develop
from
BotCraftHub:feature/warn-command
Dec 17, 2021
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
fd439d9
Started work
RealYusufIsmail f127964
Working on it
RealYusufIsmail 43a477d
Update WarnCommand.java
RealYusufIsmail 5d83e6e
added the sql
RealYusufIsmail 0d35061
save
RealYusufIsmail c72d082
Finished making warn command
RealYusufIsmail 15ee586
Update WarnCommand.java
RealYusufIsmail d2bf005
Trying to fix an error
RealYusufIsmail 1c6d65e
TODO fix
RealYusufIsmail 156beff
Fixed
RealYusufIsmail 3bb4bd0
spotlessApply
RealYusufIsmail 428ceca
Removed TODO
RealYusufIsmail 4381ab7
fixed Javadoc
RealYusufIsmail a5341c9
Ready for testing
RealYusufIsmail ccf3d92
Merge branch 'develop' into feature/warn-command
RealYusufIsmail fcdf21b
add the database for the ban and kick command
RealYusufIsmail ad37972
To be continued
RealYusufIsmail bbf8a23
Cleaning the warn command up
RealYusufIsmail 3c79186
Update WarnCommand.java
RealYusufIsmail aca2110
Workflow should pass now
RealYusufIsmail dfb6646
Thats it for now
RealYusufIsmail 3282f9b
Update AuditCommand.java
RealYusufIsmail c39aeb1
finished the making of the audit command
RealYusufIsmail 9f7fc1c
Some minor changes
RealYusufIsmail 199f57e
trying to fix the duplicates
RealYusufIsmail baaee68
tweaked the dm for the warn command
RealYusufIsmail f658951
added a missing @NotNull
RealYusufIsmail af69a9f
spotlessApply
RealYusufIsmail d125122
Some minor changes
RealYusufIsmail 7338346
minor change
RealYusufIsmail 377e8ed
bug
RealYusufIsmail 3bab6bb
removed audit command + fixed flywayMigrate error
RealYusufIsmail ccd07a6
spotless
RealYusufIsmail 0de5cfa
reverted the changes on the ban and kick command
RealYusufIsmail c5e084b
Update MessageUtils.java
RealYusufIsmail c1555d7
Reverted changes
RealYusufIsmail e5dfc29
add final
RealYusufIsmail ef24c77
Update WarnCommand.java
RealYusufIsmail 553d835
removed unused methods
RealYusufIsmail 87f3aaf
fixed the dm message
RealYusufIsmail 6498923
Merge branch 'develop' into feature/warn-command
RealYusufIsmail 7e421ec
finished
RealYusufIsmail 9af8a85
added better javadoc
RealYusufIsmail ca1de68
fixed the JavaDoc
RealYusufIsmail 4886604
Update WarnCommand.java
RealYusufIsmail eeefe28
FIxed all the issues
RealYusufIsmail 0d32bcd
fixed most of the issues
RealYusufIsmail 3850fd5
fixed more issues
RealYusufIsmail 8f1ada7
implemented the fixes.
RealYusufIsmail 3b86c9e
fixed the Action record issue
RealYusufIsmail 61ebf1b
fixed the issues
RealYusufIsmail 038ca1c
Merge branch 'develop' into feature/warn-command
RealYusufIsmail 2ecbc19
spotlessApply
RealYusufIsmail 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
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
129 changes: 129 additions & 0 deletions
129
application/src/main/java/org/togetherjava/tjbot/commands/moderation/WarnCommand.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,129 @@ | ||
package org.togetherjava.tjbot.commands.moderation; | ||
|
||
import net.dv8tion.jda.api.entities.*; | ||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||
import net.dv8tion.jda.api.interactions.InteractionHook; | ||
import net.dv8tion.jda.api.interactions.commands.OptionMapping; | ||
import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
import net.dv8tion.jda.api.requests.RestAction; | ||
import net.dv8tion.jda.api.utils.Result; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.togetherjava.tjbot.commands.SlashCommandAdapter; | ||
import org.togetherjava.tjbot.commands.SlashCommandVisibility; | ||
import org.togetherjava.tjbot.config.Config; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
/** | ||
* This command can warn users. The command will also try to DM the user to inform them about the | ||
* action and the reason. | ||
* <p> | ||
* The command fails if the user triggering it is lacking permissions to either warn other users or | ||
* to warn the specific given user (for example a moderator attempting to warn an admin). | ||
*/ | ||
public final class WarnCommand extends SlashCommandAdapter { | ||
private static final Logger logger = LoggerFactory.getLogger(WarnCommand.class); | ||
private static final String USER_OPTION = "user"; | ||
private static final String REASON_OPTION = "reason"; | ||
private static final String ACTION_VERB = "warn"; | ||
private final ModerationActionsStore actionsStore; | ||
private final Predicate<String> hasRequiredRole; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param actionsStore used to store actions issued by this command | ||
*/ | ||
public WarnCommand(@NotNull ModerationActionsStore actionsStore) { | ||
super("warn", "Warns the given user", SlashCommandVisibility.GUILD); | ||
|
||
getData().addOption(OptionType.USER, USER_OPTION, "The user who you want to warn", true) | ||
.addOption(OptionType.STRING, REASON_OPTION, "Why you want to warn the user", true); | ||
|
||
hasRequiredRole = Pattern.compile(Config.getInstance().getHeavyModerationRolePattern()) | ||
.asMatchPredicate(); | ||
this.actionsStore = Objects.requireNonNull(actionsStore); | ||
} | ||
|
||
private @NotNull RestAction<InteractionHook> warnUserFlow(@NotNull User target, | ||
@NotNull Member author, @NotNull String reason, @NotNull Guild guild, | ||
@NotNull SlashCommandEvent event) { | ||
return dmUser(target, reason, guild, event).map(hasSentDm -> { | ||
warnUser(target, author, reason, guild); | ||
return hasSentDm; | ||
}) | ||
.map(hasSentDm -> sendFeedback(hasSentDm, target, author, reason)) | ||
.flatMap(event::replyEmbeds); | ||
} | ||
|
||
private static @NotNull RestAction<Boolean> dmUser(@NotNull ISnowflake target, | ||
@NotNull String reason, @NotNull Guild guild, @NotNull SlashCommandEvent event) { | ||
return event.getJDA() | ||
.openPrivateChannelById(target.getId()) | ||
.flatMap(channel -> channel.sendMessage( | ||
""" | ||
Hey there, sorry to tell you but unfortunately you have been warned in the server %s. | ||
If you think this was a mistake, please contact a moderator or admin of the server. | ||
The reason for the warning is: %s | ||
""" | ||
.formatted(guild.getName(), reason))) | ||
.mapToResult() | ||
.map(Result::isSuccess); | ||
} | ||
|
||
private void warnUser(@NotNull User target, @NotNull Member author, @NotNull String reason, | ||
@NotNull Guild guild) { | ||
logger.info("'{}' ({}) warned the user '{}' ({}) for reason '{}'.", | ||
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(), | ||
reason); | ||
|
||
actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(), | ||
ModerationUtils.Action.WARN, null, reason); | ||
} | ||
|
||
private static @NotNull MessageEmbed sendFeedback(boolean hasSentDm, @NotNull User target, | ||
@NotNull Member author, @NotNull String reason) { | ||
String dmNoticeText = ""; | ||
if (!hasSentDm) { | ||
dmNoticeText = "(Unable to send them a DM.)"; | ||
} | ||
return ModerationUtils.createActionResponse(author.getUser(), ModerationUtils.Action.WARN, | ||
target, dmNoticeText, reason); | ||
} | ||
|
||
@Override | ||
public void onSlashCommand(@NotNull SlashCommandEvent event) { | ||
OptionMapping targetOption = | ||
Objects.requireNonNull(event.getOption(USER_OPTION), "The target is null"); | ||
Member author = Objects.requireNonNull(event.getMember(), "The author is null"); | ||
Guild guild = Objects.requireNonNull(event.getGuild(), "The guild is null"); | ||
String reason = Objects.requireNonNull(event.getOption(REASON_OPTION), "The reason is null") | ||
.getAsString(); | ||
|
||
if (!handleChecks(guild.getSelfMember(), author, targetOption.getAsMember(), reason, | ||
event)) { | ||
return; | ||
} | ||
|
||
warnUserFlow(targetOption.getAsUser(), author, reason, guild, event).queue(); | ||
} | ||
|
||
@SuppressWarnings("BooleanMethodNameMustStartWithQuestion") | ||
private boolean handleChecks(@NotNull Member bot, @NotNull Member author, | ||
@Nullable Member target, String reason, @NotNull SlashCommandEvent event) { | ||
if (target != null && !ModerationUtils.handleCanInteractWithTarget(ACTION_VERB, bot, author, | ||
target, event)) { | ||
return false; | ||
} | ||
Tais993 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) { | ||
return false; | ||
} | ||
return ModerationUtils.handleReason(reason, event); | ||
} | ||
} |
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.