5
5
import com .github .freva .asciitable .ColumnData ;
6
6
import com .github .freva .asciitable .HorizontalAlign ;
7
7
import net .dv8tion .jda .api .entities .Member ;
8
+ import net .dv8tion .jda .api .entities .Role ;
8
9
import net .dv8tion .jda .api .events .interaction .SlashCommandEvent ;
9
10
import net .dv8tion .jda .api .interactions .Interaction ;
10
11
import org .jetbrains .annotations .NotNull ;
15
16
import org .slf4j .LoggerFactory ;
16
17
import org .togetherjava .tjbot .commands .SlashCommandAdapter ;
17
18
import org .togetherjava .tjbot .commands .SlashCommandVisibility ;
19
+ import org .togetherjava .tjbot .config .Config ;
18
20
import org .togetherjava .tjbot .db .Database ;
19
21
20
22
import java .time .Instant ;
24
26
import java .util .Map ;
25
27
import java .util .function .Function ;
26
28
import java .util .function .IntFunction ;
29
+ import java .util .function .Predicate ;
30
+ import java .util .regex .Pattern ;
27
31
import java .util .stream .Collectors ;
28
32
import java .util .stream .IntStream ;
29
33
30
34
import static org .togetherjava .tjbot .db .generated .tables .HelpChannelMessages .HELP_CHANNEL_MESSAGES ;
31
35
32
36
/**
33
37
* Command that displays the top helpers of a given time range.
34
- *
38
+ * <p>
35
39
* Top helpers are measured by their message count in help channels, as set by
36
40
* {@link TopHelpersMessageListener}.
37
41
*/
@@ -41,19 +45,27 @@ public final class TopHelpersCommand extends SlashCommandAdapter {
41
45
private static final int TOP_HELPER_LIMIT = 20 ;
42
46
43
47
private final Database database ;
48
+ private final Predicate <String > hasRequiredRole ;
44
49
45
50
/**
46
51
* Creates a new instance.
47
- *
52
+ *
48
53
* @param database the database containing the message counts of top helpers
49
54
*/
50
55
public TopHelpersCommand (@ NotNull Database database ) {
51
56
super (COMMAND_NAME , "Lists top helpers for the last 30 days" , SlashCommandVisibility .GUILD );
52
57
this .database = database ;
58
+
59
+ hasRequiredRole = Pattern .compile (Config .getInstance ().getSoftModerationRolePattern ())
60
+ .asMatchPredicate ();
53
61
}
54
62
55
63
@ Override
56
64
public void onSlashCommand (@ NotNull SlashCommandEvent event ) {
65
+ if (!handleHasAuthorRole (event .getMember (), event )) {
66
+ return ;
67
+ }
68
+
57
69
List <TopHelperResult > topHelpers =
58
70
computeTopHelpersDescending (event .getGuild ().getIdLong ());
59
71
@@ -69,6 +81,17 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
69
81
.onSuccess (members -> handleTopHelpers (topHelpers , members , event ));
70
82
}
71
83
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
+
72
95
private @ NotNull List <TopHelperResult > computeTopHelpersDescending (long guildId ) {
73
96
return database .read (context -> context .select (HELP_CHANNEL_MESSAGES .AUTHOR_ID , DSL .count ())
74
97
.from (HELP_CHANNEL_MESSAGES )
0 commit comments