Skip to content

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
merged 53 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
fd439d9
Started work
RealYusufIsmail Nov 5, 2021
f127964
Working on it
RealYusufIsmail Nov 5, 2021
43a477d
Update WarnCommand.java
RealYusufIsmail Nov 5, 2021
5d83e6e
added the sql
RealYusufIsmail Nov 6, 2021
0d35061
save
RealYusufIsmail Nov 6, 2021
c72d082
Finished making warn command
RealYusufIsmail Nov 6, 2021
15ee586
Update WarnCommand.java
RealYusufIsmail Nov 6, 2021
d2bf005
Trying to fix an error
RealYusufIsmail Nov 6, 2021
1c6d65e
TODO fix
RealYusufIsmail Nov 6, 2021
156beff
Fixed
RealYusufIsmail Nov 6, 2021
3bb4bd0
spotlessApply
RealYusufIsmail Nov 6, 2021
428ceca
Removed TODO
RealYusufIsmail Nov 6, 2021
4381ab7
fixed Javadoc
RealYusufIsmail Nov 6, 2021
a5341c9
Ready for testing
RealYusufIsmail Nov 6, 2021
ccf3d92
Merge branch 'develop' into feature/warn-command
RealYusufIsmail Nov 16, 2021
fcdf21b
add the database for the ban and kick command
RealYusufIsmail Nov 16, 2021
ad37972
To be continued
RealYusufIsmail Nov 16, 2021
bbf8a23
Cleaning the warn command up
RealYusufIsmail Nov 16, 2021
3c79186
Update WarnCommand.java
RealYusufIsmail Nov 17, 2021
aca2110
Workflow should pass now
RealYusufIsmail Nov 17, 2021
dfb6646
Thats it for now
RealYusufIsmail Nov 17, 2021
3282f9b
Update AuditCommand.java
RealYusufIsmail Nov 17, 2021
c39aeb1
finished the making of the audit command
RealYusufIsmail Nov 17, 2021
9f7fc1c
Some minor changes
RealYusufIsmail Nov 17, 2021
199f57e
trying to fix the duplicates
RealYusufIsmail Nov 17, 2021
baaee68
tweaked the dm for the warn command
RealYusufIsmail Nov 17, 2021
f658951
added a missing @NotNull
RealYusufIsmail Nov 17, 2021
af69a9f
spotlessApply
RealYusufIsmail Nov 17, 2021
d125122
Some minor changes
RealYusufIsmail Nov 17, 2021
7338346
minor change
RealYusufIsmail Nov 18, 2021
377e8ed
bug
RealYusufIsmail Dec 2, 2021
3bab6bb
removed audit command + fixed flywayMigrate error
RealYusufIsmail Dec 3, 2021
ccd07a6
spotless
RealYusufIsmail Dec 3, 2021
0de5cfa
reverted the changes on the ban and kick command
RealYusufIsmail Dec 3, 2021
c5e084b
Update MessageUtils.java
RealYusufIsmail Dec 3, 2021
c1555d7
Reverted changes
RealYusufIsmail Dec 3, 2021
e5dfc29
add final
RealYusufIsmail Dec 3, 2021
ef24c77
Update WarnCommand.java
RealYusufIsmail Dec 3, 2021
553d835
removed unused methods
RealYusufIsmail Dec 3, 2021
87f3aaf
fixed the dm message
RealYusufIsmail Dec 4, 2021
6498923
Merge branch 'develop' into feature/warn-command
RealYusufIsmail Dec 10, 2021
7e421ec
finished
RealYusufIsmail Dec 10, 2021
9af8a85
added better javadoc
RealYusufIsmail Dec 10, 2021
ca1de68
fixed the JavaDoc
RealYusufIsmail Dec 10, 2021
4886604
Update WarnCommand.java
RealYusufIsmail Dec 10, 2021
eeefe28
FIxed all the issues
RealYusufIsmail Dec 11, 2021
0d32bcd
fixed most of the issues
RealYusufIsmail Dec 11, 2021
3850fd5
fixed more issues
RealYusufIsmail Dec 11, 2021
8f1ada7
implemented the fixes.
RealYusufIsmail Dec 13, 2021
3b86c9e
fixed the Action record issue
RealYusufIsmail Dec 13, 2021
61ebf1b
fixed the issues
RealYusufIsmail Dec 14, 2021
038ca1c
Merge branch 'develop' into feature/warn-command
RealYusufIsmail Dec 14, 2021
2ecbc19
spotlessApply
RealYusufIsmail Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public enum Commands {
commands.add(new TagManageCommand(tagSystem));
commands.add(new TagsCommand(tagSystem));
commands.add(new VcActivityCommand());
commands.add(new WarnCommand(actionsStore));
commands.add(new KickCommand(actionsStore));
commands.add(new BanCommand(actionsStore));
commands.add(new UnbanCommand(actionsStore));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ enum Action {
* When a user kicks another user.
*/
KICK("kicked"),
/**
* When a user warns another user.
*/
WARN("warned"),
/**
* When a user mutes another user.
*/
Expand Down
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;
}
if (!ModerationUtils.handleHasAuthorRole(ACTION_VERB, hasRequiredRole, author, event)) {
return false;
}
return ModerationUtils.handleReason(reason, event);
}
}