23
23
import org .togetherjava .tjbot .config .Config ;
24
24
import org .togetherjava .tjbot .db .Database ;
25
25
26
+ import java .math .BigDecimal ;
26
27
import java .time .*;
27
28
import java .time .format .TextStyle ;
28
29
import java .util .*;
38
39
/**
39
40
* Command that displays the top helpers of a given time range.
40
41
* <p>
41
- * Top helpers are measured by their message count in help channels, as set by
42
+ * Top helpers are measured by their message length in help channels, as set by
42
43
* {@link TopHelpersMessageListener}.
43
44
*/
44
45
public final class TopHelpersCommand extends SlashCommandAdapter {
@@ -53,7 +54,7 @@ public final class TopHelpersCommand extends SlashCommandAdapter {
53
54
/**
54
55
* Creates a new instance.
55
56
*
56
- * @param database the database containing the message counts of top helpers
57
+ * @param database the database containing the message records of top helpers
57
58
* @param config the config to use for this
58
59
*/
59
60
public TopHelpersCommand (@ NotNull Database database , @ NotNull Config config ) {
@@ -138,12 +139,13 @@ private boolean handleHasAuthorRole(@NotNull Member author, @NotNull IReplyCallb
138
139
139
140
private @ NotNull List <TopHelperResult > computeTopHelpersDescending (long guildId ,
140
141
@ NotNull TimeRange timeRange ) {
141
- return database .read (context -> context .select (HELP_CHANNEL_MESSAGES .AUTHOR_ID , DSL .count ())
142
+ return database .read (context -> context
143
+ .select (HELP_CHANNEL_MESSAGES .AUTHOR_ID , DSL .sum (HELP_CHANNEL_MESSAGES .MESSAGE_LENGTH ))
142
144
.from (HELP_CHANNEL_MESSAGES )
143
145
.where (HELP_CHANNEL_MESSAGES .GUILD_ID .eq (guildId )
144
146
.and (HELP_CHANNEL_MESSAGES .SENT_AT .between (timeRange .start (), timeRange .end ())))
145
147
.groupBy (HELP_CHANNEL_MESSAGES .AUTHOR_ID )
146
- .orderBy (DSL .count ().desc ())
148
+ .orderBy (DSL .two ().desc ())
147
149
.limit (TOP_HELPER_LIMIT )
148
150
.fetch (Records .mapping (TopHelperResult ::new )));
149
151
}
@@ -174,9 +176,9 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
174
176
@ Nullable Member member ) {
175
177
String id = Long .toString (topHelper .authorId ());
176
178
String name = member == null ? "UNKNOWN_USER" : member .getEffectiveName ();
177
- String messageCount = Integer .toString (topHelper .messageCount ());
179
+ String messageLengths = Long .toString (topHelper .messageLengths (). longValue ());
178
180
179
- return List .of (id , name , messageCount );
181
+ return List .of (id , name , messageLengths );
180
182
}
181
183
182
184
private static @ NotNull String dataTableToString (@ NotNull Collection <List <String >> dataTable ,
@@ -185,7 +187,7 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
185
187
List .of (new ColumnSetting ("Id" , HorizontalAlign .RIGHT ),
186
188
new ColumnSetting ("Name" , HorizontalAlign .RIGHT ),
187
189
new ColumnSetting (
188
- "Message count (for %s)" .formatted (timeRange .description ()),
190
+ "Message lengths (for %s)" .formatted (timeRange .description ()),
189
191
HorizontalAlign .RIGHT )));
190
192
}
191
193
@@ -206,14 +208,15 @@ private static void handleTopHelpers(@NotNull Collection<TopHelperResult> topHel
206
208
return AsciiTable .getTable (AsciiTable .BASIC_ASCII_NO_DATA_SEPARATORS , dataTable , columns );
207
209
}
208
210
209
- private record TimeRange (Instant start , Instant end , String description ) {
211
+ private record TimeRange (@ NotNull Instant start , @ NotNull Instant end ,
212
+ @ NotNull String description ) {
210
213
}
211
214
212
215
213
- private record TopHelperResult (long authorId , int messageCount ) {
216
+ private record TopHelperResult (long authorId , @ NotNull BigDecimal messageLengths ) {
214
217
}
215
218
216
219
217
- private record ColumnSetting (String headerName , HorizontalAlign alignment ) {
220
+ private record ColumnSetting (@ NotNull String headerName , @ NotNull HorizontalAlign alignment ) {
218
221
}
219
222
}
0 commit comments