Skip to content

Commit c793b88

Browse files
committed
Integrated store into moderation commands
* ban * unban * kick
1 parent bf14aca commit c793b88

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/Commands.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.togetherjava.tjbot.commands.mathcommands.TeXCommand;
88
import org.togetherjava.tjbot.commands.moderation.BanCommand;
99
import org.togetherjava.tjbot.commands.moderation.KickCommand;
10+
import org.togetherjava.tjbot.commands.moderation.ModerationActionsStore;
1011
import org.togetherjava.tjbot.commands.moderation.UnbanCommand;
1112
import org.togetherjava.tjbot.commands.tags.TagCommand;
1213
import org.togetherjava.tjbot.commands.tags.TagManageCommand;
@@ -40,6 +41,7 @@ public enum Commands {
4041
public static @NotNull Collection<SlashCommand> createSlashCommands(
4142
@NotNull Database database) {
4243
TagSystem tagSystem = new TagSystem(database);
44+
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
4345
// NOTE The command system can add special system relevant commands also by itself,
4446
// hence this list may not necessarily represent the full list of all commands actually
4547
// available.
@@ -52,9 +54,9 @@ public enum Commands {
5254
commands.add(new TagManageCommand(tagSystem));
5355
commands.add(new TagsCommand(tagSystem));
5456
commands.add(new VcActivityCommand());
55-
commands.add(new KickCommand());
56-
commands.add(new BanCommand());
57-
commands.add(new UnbanCommand());
57+
commands.add(new KickCommand(actionsStore));
58+
commands.add(new BanCommand(actionsStore));
59+
commands.add(new UnbanCommand(actionsStore));
5860

5961
return commands;
6062
}

application/src/main/java/org/togetherjava/tjbot/commands/moderation/BanCommand.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ public final class BanCommand extends SlashCommandAdapter {
4343
private static final String COMMAND_NAME = "ban";
4444
private static final String ACTION_VERB = "ban";
4545
private final Predicate<String> hasRequiredRole;
46+
private final ModerationActionsStore actionsStore;
4647

4748
/**
4849
* Constructs an instance.
50+
*
51+
* @param actionsStore used to store actions issued by this command
4952
*/
50-
public BanCommand() {
53+
public BanCommand(@NotNull ModerationActionsStore actionsStore) {
5154
super(COMMAND_NAME, "Bans the given user from the server", SlashCommandVisibility.GUILD);
5255

5356
getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to ban", true)
@@ -58,6 +61,7 @@ public BanCommand() {
5861

5962
hasRequiredRole = Pattern.compile(Config.getInstance().getHeavyModerationRolePattern())
6063
.asMatchPredicate();
64+
this.actionsStore = actionsStore;
6165
}
6266

6367
private static RestAction<InteractionHook> handleAlreadyBanned(@NotNull Guild.Ban ban,
@@ -72,9 +76,9 @@ private static RestAction<InteractionHook> handleAlreadyBanned(@NotNull Guild.Ba
7276
}
7377

7478
@SuppressWarnings("MethodWithTooManyParameters")
75-
private static RestAction<InteractionHook> banUserFlow(@NotNull User target,
76-
@NotNull Member author, @NotNull String reason, int deleteHistoryDays,
77-
@NotNull Guild guild, @NotNull SlashCommandEvent event) {
79+
private RestAction<InteractionHook> banUserFlow(@NotNull User target, @NotNull Member author,
80+
@NotNull String reason, int deleteHistoryDays, @NotNull Guild guild,
81+
@NotNull SlashCommandEvent event) {
7882
return sendDm(target, reason, guild, event)
7983
.flatMap(hasSentDm -> banUser(target, author, reason, deleteHistoryDays, guild)
8084
.map(banResult -> hasSentDm))
@@ -97,13 +101,16 @@ private static RestAction<Boolean> sendDm(@NotNull ISnowflake target, @NotNull S
97101
.map(Result::isSuccess);
98102
}
99103

100-
private static AuditableRestAction<Void> banUser(@NotNull User target, @NotNull Member author,
104+
private AuditableRestAction<Void> banUser(@NotNull User target, @NotNull Member author,
101105
@NotNull String reason, int deleteHistoryDays, @NotNull Guild guild) {
102106
logger.info(
103107
"'{}' ({}) banned the user '{}' ({}) from guild '{}' and deleted their message history of the last {} days, for reason '{}'.",
104108
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(),
105109
guild.getName(), deleteHistoryDays, reason);
106110

111+
actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
112+
ModerationUtils.Action.BAN, null, reason);
113+
107114
return guild.ban(target, deleteHistoryDays, reason);
108115
}
109116

application/src/main/java/org/togetherjava/tjbot/commands/moderation/KickCommand.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@ public final class KickCommand extends SlashCommandAdapter {
3636
private static final String COMMAND_NAME = "kick";
3737
private static final String ACTION_VERB = "kick";
3838
private final Predicate<String> hasRequiredRole;
39+
private final ModerationActionsStore actionsStore;
3940

4041
/**
4142
* Constructs an instance.
43+
*
44+
* @param actionsStore used to store actions issued by this command
4245
*/
43-
public KickCommand() {
46+
public KickCommand(@NotNull ModerationActionsStore actionsStore) {
4447
super(COMMAND_NAME, "Kicks the given user from the server", SlashCommandVisibility.GUILD);
4548

4649
getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to kick", true)
4750
.addOption(OptionType.STRING, REASON_OPTION, "Why the user should be kicked", true);
4851

4952
hasRequiredRole = Pattern.compile(Config.getInstance().getSoftModerationRolePattern())
5053
.asMatchPredicate();
54+
this.actionsStore = actionsStore;
5155
}
5256

5357
private static void handleAbsentTarget(@NotNull Interaction event) {
@@ -56,7 +60,7 @@ private static void handleAbsentTarget(@NotNull Interaction event) {
5660
.queue();
5761
}
5862

59-
private static void kickUserFlow(@NotNull Member target, @NotNull Member author,
63+
private void kickUserFlow(@NotNull Member target, @NotNull Member author,
6064
@NotNull String reason, @NotNull Guild guild, @NotNull SlashCommandEvent event) {
6165
sendDm(target, reason, guild, event)
6266
.flatMap(hasSentDm -> kickUser(target, author, reason, guild)
@@ -81,12 +85,15 @@ private static RestAction<Boolean> sendDm(@NotNull ISnowflake target, @NotNull S
8185
.map(Result::isSuccess);
8286
}
8387

84-
private static AuditableRestAction<Void> kickUser(@NotNull Member target,
85-
@NotNull Member author, @NotNull String reason, @NotNull Guild guild) {
88+
private AuditableRestAction<Void> kickUser(@NotNull Member target, @NotNull Member author,
89+
@NotNull String reason, @NotNull Guild guild) {
8690
logger.info("'{}' ({}) kicked the user '{}' ({}) from guild '{}' for reason '{}'.",
8791
author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(),
8892
target.getId(), guild.getName(), reason);
8993

94+
actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
95+
ModerationUtils.Action.KICK, null, reason);
96+
9097
return guild.kick(target, reason).reason(reason);
9198
}
9299

application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationActionsStore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import java.util.Optional;
1212

1313
// FIXME Javadoc on the whole class
14-
final class ModerationActionsStore {
14+
public final class ModerationActionsStore {
1515
private final Database database;
1616

17-
ModerationActionsStore(@NotNull Database database) {
17+
public ModerationActionsStore(@NotNull Database database) {
1818
this.database = database;
1919
}
2020

@@ -75,7 +75,7 @@ public int addAction(long guildId, long authorId, long targetId,
7575
});
7676
}
7777

78-
record ActionRecord(int caseId, @NotNull Instant issuedAt, long guildId, long authorId,
78+
public record ActionRecord(int caseId, @NotNull Instant issuedAt, long guildId, long authorId,
7979
long targetId, @NotNull ModerationUtils.Action actionType,
8080
@Nullable Instant actionExpiresAt, @NotNull String reason) {
8181
private static ActionRecord of(@NotNull ModerationActionsRecord action) {

application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnbanCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ public final class UnbanCommand extends SlashCommandAdapter {
2929
private static final String COMMAND_NAME = "unban";
3030
private static final String ACTION_VERB = "unban";
3131
private final Predicate<String> hasRequiredRole;
32+
private final ModerationActionsStore actionsStore;
3233

3334
/**
3435
* Constructs an instance.
36+
*
37+
* @param actionsStore used to store actions issued by this command
3538
*/
36-
public UnbanCommand() {
39+
public UnbanCommand(@NotNull ModerationActionsStore actionsStore) {
3740
super(COMMAND_NAME, "Unbans the given user from the server", SlashCommandVisibility.GUILD);
3841

3942
getData()
@@ -43,9 +46,10 @@ public UnbanCommand() {
4346

4447
hasRequiredRole = Pattern.compile(Config.getInstance().getHeavyModerationRolePattern())
4548
.asMatchPredicate();
49+
this.actionsStore = actionsStore;
4650
}
4751

48-
private static void unban(@NotNull User target, @NotNull Member author, @NotNull String reason,
52+
private void unban(@NotNull User target, @NotNull Member author, @NotNull String reason,
4953
@NotNull Guild guild, @NotNull Interaction event) {
5054
guild.unban(target).reason(reason).queue(result -> {
5155
MessageEmbed message = ModerationUtils.createActionResponse(author.getUser(),
@@ -55,6 +59,9 @@ private static void unban(@NotNull User target, @NotNull Member author, @NotNull
5559
logger.info("'{}' ({}) unbanned the user '{}' ({}) from guild '{}' for reason '{}'.",
5660
author.getUser().getAsTag(), author.getId(), target.getAsTag(), target.getId(),
5761
guild.getName(), reason);
62+
63+
actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(),
64+
ModerationUtils.Action.UNBAN, null, reason);
5865
}, unbanFailure -> handleFailure(unbanFailure, target, event));
5966
}
6067

0 commit comments

Comments
 (0)