Skip to content

Feature/leaderboard command#1465

Open
barsh404error wants to merge 3 commits into
Together-Java:developfrom
barsh404error:feature/leaderboard-command
Open

Feature/leaderboard command#1465
barsh404error wants to merge 3 commits into
Together-Java:developfrom
barsh404error:feature/leaderboard-command

Conversation

@barsh404error
Copy link
Copy Markdown
Member

@barsh404error barsh404error commented Apr 25, 2026

What

Adds a /leaderboard command that displays the all-time top helpers. It reads the hall-of-fame channel history, finds messages that mention top helpers, counts how many times each user was awarded, and shows the top 10 ranked with medals.

How

Reads the hall-of-fame channel history, filters messages containing "top helper", extracts mentioned users via getMentions(), counts wins per user and shows the top 10 in an embed.

Screenshot From 2026-05-26 21-56-39

Edge cases

  • Channel not found: yes, returns "Could not find the hall of fame channel."
  • No messages in channel: yes, sorted.isEmpty() catches it, then returns "No top helper data found."
  • Messages exist but none contain "top helper": yes, the same sorted.isEmpty() check covers it.
  • Multiple users in one message: yes, the getMentions().getUsers() loop handles multiple mentions per message.
Screenshot From 2026-05-27 15-32-30 Screenshot From 2026-05-27 15-32-54 Screenshot From 2026-05-27 15-42-14

NOTE: Channel not found and Messages exist but none contain "top helper" returns the same output "No top helper data found."

@barsh404error barsh404error requested a review from a team as a code owner April 25, 2026 09:18
@barsh404error barsh404error force-pushed the feature/leaderboard-command branch from 8a9840f to ff6632d Compare April 25, 2026 09:23
@barsh404error barsh404error self-assigned this Apr 25, 2026
@barsh404error barsh404error force-pushed the feature/leaderboard-command branch from ff6632d to 9bb9883 Compare April 25, 2026 09:36
private final Config config;

private final Map<Long, Map<Long, Integer>> winsByGuild = new ConcurrentHashMap<>();
private final Map<Long, OffsetDateTime> lastFetchedPerGuild = new ConcurrentHashMap<>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instant, not OffsetDateTime

Comment on lines +83 to +118
if (!winsByGuild.containsKey(guildId)) {
hallOfFame.getIterableHistory().takeAsync(HISTORY_LIMIT).thenAccept(messages -> {
Map<Long, Integer> wins = new HashMap<>();
countWinsInto(messages, wins);
winsByGuild.put(guildId, new ConcurrentHashMap<>(wins));

if (sorted.isEmpty()) {
event.getHook().editOriginal("No top helper data found.").queue();
return;
}
if (!messages.isEmpty()) {
lastFetchedPerGuild.put(guildId, messages.getFirst().getTimeCreated());
}

sendLeaderboard(guild, wins, hook);
}).exceptionally(error -> {
logger.error("Failed to read hall of fame channel", error);
hook.editOriginal("Failed to read the hall of fame channel.").queue();
return null;
});
} else {
OffsetDateTime lastFetched = lastFetchedPerGuild.get(guildId);
Map<Long, Integer> cachedWins = winsByGuild.get(guildId);

hallOfFame.getIterableHistory()
.takeWhileAsync(HISTORY_LIMIT, msg -> msg.getTimeCreated().isAfter(lastFetched))
.thenAccept(newMessages -> {
if (!newMessages.isEmpty()) {
countWinsInto(newMessages, cachedWins);
lastFetchedPerGuild.put(guildId, newMessages.getFirst().getTimeCreated());
}
sendLeaderboard(guild, cachedWins, hook);
})
.exceptionally(error -> {
logger.error("Failed to read hall of fame channel", error);
hook.editOriginal("Failed to read the hall of fame channel.").queue();
return null;
});
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor this to get rid of the duplication. 90% of the code in the if-else branches are identical

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
Guild guild = event.getGuild();
if (guild == null) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch can never occur because of CommandVisibility.GUILD , if you're getting a nullable warning then wrap it with Objects.requireNotNull as we have in other places.

.orElse(null);

if (hallOfFame == null) {
event.getHook().editOriginal("Could not find the hall of fame channel.").queue();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps change this to something like "Could not find channel matching "

Map<Long, Integer> wins = new HashMap<>();
for (Message message : messages) {
String content = message.getContentRaw();
if (!content.toLowerCase().contains("top helper")) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked the channel and every message is has been a top helper message so tbh we could remove this if-block and accept any message in that channel.

TBC

return;
}

hallOfFame.getIterableHistory().takeAsync(HISTORY_LIMIT).thenAccept(messages -> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work? Has it been tested?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants