Skip to content

Commit 796ebb2

Browse files
committed
Better /rank
1 parent 2eaaa58 commit 796ebb2

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/commands/activity/rank.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const RankCommand: Command = {
4141

4242
const authorizedUserIds = new Set<string>([interaction.user.id]);
4343

44-
const embed = await computeRankForExpressionAsync(guildId, expression, userLocale, authorizedUserIds);
44+
const embed = await computeRankForExpression(guildId, expression, userLocale, authorizedUserIds);
4545

4646
const displayMyRankTranslation = translations.responses?.displayMyRank?.[userLocale] ?? "Display my rank";
4747
const displayMyRankButton = new ButtonBuilder()
@@ -74,7 +74,7 @@ const RankCommand: Command = {
7474

7575
const deferUpdatePromise = i.update({ content: "" });
7676

77-
const editedEmbed = await computeRankForExpressionAsync(guildId, expression, userLocale, authorizedUserIds);
77+
const editedEmbed = await computeRankForExpression(guildId, expression, userLocale, authorizedUserIds);
7878
await deferUpdatePromise;
7979
await i.editReply({
8080
embeds: [editedEmbed],
@@ -92,7 +92,7 @@ const RankCommand: Command = {
9292

9393
export default RankCommand;
9494

95-
async function computeRankForExpressionAsync(
95+
async function computeRankForExpression(
9696
guildId: string,
9797
expression: string,
9898
userLocale: string,
@@ -114,9 +114,12 @@ async function computeRankForExpressionAsync(
114114
hashMapOfUserId.set(userIdHash, userId);
115115
});
116116

117+
// Show top 10 AND already displayed users
117118
let embedDescription = rankResult
118-
.map((result, index) => {
119+
.filter((result) => result.rank <= 10 || displayedUsersIds.has(result.author_id))
120+
.map((result) => {
119121
let userName;
122+
// 128 means hashed user ID
120123
if (result.author_id.length === 128) {
121124
const realUserId = hashMapOfUserId.get(result.author_id);
122125
if (realUserId) {
@@ -128,7 +131,7 @@ async function computeRankForExpressionAsync(
128131
userName = userMention(result.author_id);
129132
}
130133

131-
const userRank = index + 1;
134+
const userRank = result.rank;
132135
const userCount = result.count;
133136
const userPercentage = ((userCount / totalCount) * 100).toFixed(2);
134137

src/database/bee-database.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,22 @@ export function getRank(guildId: string, expression: string): RankResult[] {
167167
SELECT rowid
168168
FROM messageindex
169169
WHERE messageindex MATCH ?
170+
),
171+
aggregated AS (
172+
SELECT
173+
IF (i.real_author_id IS NULL, m.author_id, CAST(i.real_author_id AS TEXT)) AS author_id,
174+
COUNT(*) AS count
175+
FROM matched
176+
JOIN message AS m ON m.rowid = matched.rowid
177+
LEFT JOIN identity i ON m.author_id = i.author_id
178+
GROUP BY IF(i.real_author_id IS NULL, m.author_id, CAST(i.real_author_id AS TEXT))
170179
)
171-
SELECT IF (i.real_author_id IS NULL, m.author_id, CAST(i.real_author_id AS TEXT)) as author_id,
172-
COUNT(*) AS count
173-
FROM matched
174-
JOIN message AS m ON m.rowid = matched.rowid
175-
LEFT JOIN "identity" i ON m.author_id = i.author_id
176-
GROUP BY m.author_id
177-
ORDER BY count DESC;
180+
SELECT
181+
RANK() OVER (ORDER BY count DESC) AS rank,
182+
author_id,
183+
count
184+
FROM aggregated
185+
ORDER BY rank;
178186
`);
179187

180188
return statement.all(`"${expression}"`) as RankResult[];

src/models/database/rank-result.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface RankResult {
2+
rank: number;
23
author_id: string;
34
count: number;
45
}

0 commit comments

Comments
 (0)