Skip to content

Commit 1d5583b

Browse files
committed
Require soft mod role for top-helpers
1 parent 57c7536 commit 1d5583b

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/tophelper/TopHelpersCommand.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.github.freva.asciitable.ColumnData;
66
import com.github.freva.asciitable.HorizontalAlign;
77
import net.dv8tion.jda.api.entities.Member;
8+
import net.dv8tion.jda.api.entities.Role;
89
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
910
import net.dv8tion.jda.api.interactions.Interaction;
1011
import org.jetbrains.annotations.NotNull;
@@ -15,6 +16,7 @@
1516
import org.slf4j.LoggerFactory;
1617
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
1718
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
19+
import org.togetherjava.tjbot.config.Config;
1820
import org.togetherjava.tjbot.db.Database;
1921

2022
import java.time.Instant;
@@ -24,14 +26,16 @@
2426
import java.util.Map;
2527
import java.util.function.Function;
2628
import java.util.function.IntFunction;
29+
import java.util.function.Predicate;
30+
import java.util.regex.Pattern;
2731
import java.util.stream.Collectors;
2832
import java.util.stream.IntStream;
2933

3034
import static org.togetherjava.tjbot.db.generated.tables.HelpChannelMessages.HELP_CHANNEL_MESSAGES;
3135

3236
/**
3337
* Command that displays the top helpers of a given time range.
34-
*
38+
* <p>
3539
* Top helpers are measured by their message count in help channels, as set by
3640
* {@link TopHelpersMessageListener}.
3741
*/
@@ -41,19 +45,27 @@ public final class TopHelpersCommand extends SlashCommandAdapter {
4145
private static final int TOP_HELPER_LIMIT = 20;
4246

4347
private final Database database;
48+
private final Predicate<String> hasRequiredRole;
4449

4550
/**
4651
* Creates a new instance.
47-
*
52+
*
4853
* @param database the database containing the message counts of top helpers
4954
*/
5055
public TopHelpersCommand(@NotNull Database database) {
5156
super(COMMAND_NAME, "Lists top helpers for the last 30 days", SlashCommandVisibility.GUILD);
5257
this.database = database;
58+
59+
hasRequiredRole = Pattern.compile(Config.getInstance().getSoftModerationRolePattern())
60+
.asMatchPredicate();
5361
}
5462

5563
@Override
5664
public void onSlashCommand(@NotNull SlashCommandEvent event) {
65+
if (!handleHasAuthorRole(event.getMember(), event)) {
66+
return;
67+
}
68+
5769
List<TopHelperResult> topHelpers =
5870
computeTopHelpersDescending(event.getGuild().getIdLong());
5971

@@ -69,6 +81,17 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
6981
.onSuccess(members -> handleTopHelpers(topHelpers, members, event));
7082
}
7183

84+
@SuppressWarnings("BooleanMethodNameMustStartWithQuestion")
85+
private boolean handleHasAuthorRole(@NotNull Member author, @NotNull Interaction event) {
86+
if (author.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole)) {
87+
return true;
88+
}
89+
event.reply("You can not compute the top-helpers since you do not have the required role.")
90+
.setEphemeral(true)
91+
.queue();
92+
return false;
93+
}
94+
7295
private @NotNull List<TopHelperResult> computeTopHelpersDescending(long guildId) {
7396
return database.read(context -> context.select(HELP_CHANNEL_MESSAGES.AUTHOR_ID, DSL.count())
7497
.from(HELP_CHANNEL_MESSAGES)

0 commit comments

Comments
 (0)