Skip to content

Commit 0811a8a

Browse files
committed
Use previous month as default time range
* with proper java.time computation instead of just "last 30 days"
1 parent 464d790 commit 0811a8a

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
import org.togetherjava.tjbot.db.Database;
2121

2222
import java.time.Instant;
23-
import java.time.Period;
23+
import java.time.ZoneOffset;
24+
import java.time.ZonedDateTime;
25+
import java.time.format.TextStyle;
26+
import java.time.temporal.TemporalAdjusters;
2427
import java.util.Collection;
2528
import java.util.List;
29+
import java.util.Locale;
2630
import java.util.Map;
2731
import java.util.function.Function;
2832
import java.util.function.IntFunction;
@@ -53,11 +57,11 @@ public final class TopHelpersCommand extends SlashCommandAdapter {
5357
* @param database the database containing the message counts of top helpers
5458
*/
5559
public TopHelpersCommand(@NotNull Database database) {
56-
super(COMMAND_NAME, "Lists top helpers for the last 30 days", SlashCommandVisibility.GUILD);
57-
this.database = database;
58-
60+
super(COMMAND_NAME, "Lists top helpers for the last month", SlashCommandVisibility.GUILD);
61+
// TODO Add options to optionally pick a time range once JDA/Discord offers a date-picker
5962
hasRequiredRole = Pattern.compile(Config.getInstance().getSoftModerationRolePattern())
6063
.asMatchPredicate();
64+
this.database = database;
6165
}
6266

6367
@Override
@@ -66,11 +70,15 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
6670
return;
6771
}
6872

73+
TimeRange timeRange = computeDefaultTimeRange();
6974
List<TopHelperResult> topHelpers =
70-
computeTopHelpersDescending(event.getGuild().getIdLong());
75+
computeTopHelpersDescending(event.getGuild().getIdLong(), timeRange);
7176

7277
if (topHelpers.isEmpty()) {
73-
event.reply("No entries for the selected time range.").queue();
78+
event
79+
.reply("No entries for the selected time range (%s)."
80+
.formatted(timeRange.description()))
81+
.queue();
7482
return;
7583
}
7684
event.deferReply().queue();
@@ -79,7 +87,7 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
7987
event.getGuild()
8088
.retrieveMembersByIds(topHelperIds)
8189
.onError(error -> handleError(error, event))
82-
.onSuccess(members -> handleTopHelpers(topHelpers, members, event));
90+
.onSuccess(members -> handleTopHelpers(topHelpers, members, timeRange, event));
8391
}
8492

8593
@SuppressWarnings("BooleanMethodNameMustStartWithQuestion")
@@ -93,12 +101,24 @@ private boolean handleHasAuthorRole(@NotNull Member author, @NotNull Interaction
93101
return false;
94102
}
95103

96-
private @NotNull List<TopHelperResult> computeTopHelpersDescending(long guildId) {
104+
private static @NotNull TimeRange computeDefaultTimeRange() {
105+
// Last month
106+
ZonedDateTime start = Instant.now()
107+
.atZone(ZoneOffset.UTC)
108+
.minusMonths(1)
109+
.with(TemporalAdjusters.firstDayOfMonth());
110+
ZonedDateTime end = start.with(TemporalAdjusters.lastDayOfMonth());
111+
String description = start.getMonth().getDisplayName(TextStyle.FULL_STANDALONE, Locale.US);
112+
113+
return new TimeRange(start.toInstant(), end.toInstant(), description);
114+
}
115+
116+
private @NotNull List<TopHelperResult> computeTopHelpersDescending(long guildId,
117+
@NotNull TimeRange timeRange) {
97118
return database.read(context -> context.select(HELP_CHANNEL_MESSAGES.AUTHOR_ID, DSL.count())
98119
.from(HELP_CHANNEL_MESSAGES)
99120
.where(HELP_CHANNEL_MESSAGES.GUILD_ID.eq(guildId)
100-
.and(HELP_CHANNEL_MESSAGES.SENT_AT
101-
.greaterOrEqual(Instant.now().minus(Period.ofDays(30)))))
121+
.and(HELP_CHANNEL_MESSAGES.SENT_AT.between(timeRange.start(), timeRange.end())))
102122
.groupBy(HELP_CHANNEL_MESSAGES.AUTHOR_ID)
103123
.orderBy(DSL.count().desc())
104124
.limit(TOP_HELPER_LIMIT)
@@ -111,7 +131,8 @@ private static void handleError(@NotNull Throwable error, @NotNull Interaction e
111131
}
112132

113133
private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHelpers,
114-
@NotNull Collection<? extends Member> members, @NotNull Interaction event) {
134+
@NotNull Collection<? extends Member> members, @NotNull TimeRange timeRange,
135+
@NotNull Interaction event) {
115136
Map<Long, Member> userIdToMember =
116137
members.stream().collect(Collectors.toMap(Member::getIdLong, Function.identity()));
117138

@@ -120,7 +141,8 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
120141
userIdToMember.get(topHelper.authorId())))
121142
.toList();
122143

123-
String message = "```java%n%s%n```".formatted(dataTableToString(topHelpersDataTable));
144+
String message =
145+
"```java%n%s%n```".formatted(dataTableToString(topHelpersDataTable, timeRange));
124146

125147
event.getHook().editOriginal(message).queue();
126148
}
@@ -134,11 +156,14 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
134156
return List.of(id, name, messageCount);
135157
}
136158

137-
private static @NotNull String dataTableToString(@NotNull Collection<List<String>> dataTable) {
159+
private static @NotNull String dataTableToString(@NotNull Collection<List<String>> dataTable,
160+
@NotNull TimeRange timeRange) {
138161
return dataTableToAsciiTable(dataTable,
139162
List.of(new ColumnSetting("Id", HorizontalAlign.RIGHT),
140163
new ColumnSetting("Name", HorizontalAlign.RIGHT),
141-
new ColumnSetting("Message count (30 days)", HorizontalAlign.RIGHT)));
164+
new ColumnSetting(
165+
"Message count (for %s)".formatted(timeRange.description()),
166+
HorizontalAlign.RIGHT)));
142167
}
143168

144169
private static @NotNull String dataTableToAsciiTable(
@@ -158,6 +183,9 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
158183
return AsciiTable.getTable(AsciiTable.BASIC_ASCII_NO_DATA_SEPARATORS, dataTable, columns);
159184
}
160185

186+
private record TimeRange(Instant start, Instant end, String description) {
187+
}
188+
161189
private record TopHelperResult(long authorId, int messageCount) {
162190
}
163191

0 commit comments

Comments
 (0)