Skip to content

Commit 94b6922

Browse files
committed
refactor: use Instant instead of OffsetDateTime and extract fetchNewMessages to remove duplication
1 parent 23b6d5f commit 94b6922

1 file changed

Lines changed: 29 additions & 38 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/features/leaderboard/LeaderboardCommand.java

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import org.togetherjava.tjbot.features.tophelper.TopHelpersService;
1818
import org.togetherjava.tjbot.features.utils.Colors;
1919

20-
import java.time.OffsetDateTime;
20+
import java.time.Instant;
2121
import java.util.Comparator;
22-
import java.util.HashMap;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.StringJoiner;
25+
import java.util.concurrent.CompletableFuture;
2626
import java.util.concurrent.ConcurrentHashMap;
2727
import java.util.regex.Pattern;
2828

@@ -45,7 +45,7 @@ public final class LeaderboardCommand extends SlashCommandAdapter {
4545
private final Config config;
4646

4747
private final Map<Long, Map<Long, Integer>> winsByGuild = new ConcurrentHashMap<>();
48-
private final Map<Long, OffsetDateTime> lastFetchedPerGuild = new ConcurrentHashMap<>();
48+
private final Map<Long, Instant> lastFetchedPerGuild = new ConcurrentHashMap<>();
4949

5050
public LeaderboardCommand(Config config) {
5151
super(COMMAND_NAME, "Show the all-time top helpers leaderboard", CommandVisibility.GUILD);
@@ -80,47 +80,38 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
8080
long guildId = guild.getIdLong();
8181
InteractionHook hook = event.getHook();
8282

83-
if (!winsByGuild.containsKey(guildId)) {
84-
hallOfFame.getIterableHistory().takeAsync(HISTORY_LIMIT).thenAccept(messages -> {
85-
Map<Long, Integer> wins = new HashMap<>();
86-
countWinsInto(messages, wins);
87-
winsByGuild.put(guildId, new ConcurrentHashMap<>(wins));
88-
89-
if (!messages.isEmpty()) {
90-
lastFetchedPerGuild.put(guildId, messages.getFirst().getTimeCreated());
91-
}
92-
93-
sendLeaderboard(guild, wins, hook);
94-
}).exceptionally(error -> {
95-
logger.error("Failed to read hall of fame channel", error);
96-
hook.editOriginal("Failed to read the hall of fame channel.").queue();
97-
return null;
98-
});
99-
} else {
100-
OffsetDateTime lastFetched = lastFetchedPerGuild.get(guildId);
101-
Map<Long, Integer> cachedWins = winsByGuild.get(guildId);
102-
103-
hallOfFame.getIterableHistory()
104-
.takeWhileAsync(HISTORY_LIMIT, msg -> msg.getTimeCreated().isAfter(lastFetched))
105-
.thenAccept(newMessages -> {
106-
if (!newMessages.isEmpty()) {
107-
countWinsInto(newMessages, cachedWins);
108-
lastFetchedPerGuild.put(guildId, newMessages.getFirst().getTimeCreated());
109-
}
110-
sendLeaderboard(guild, cachedWins, hook);
111-
})
112-
.exceptionally(error -> {
113-
logger.error("Failed to read hall of fame channel", error);
114-
hook.editOriginal("Failed to read the hall of fame channel.").queue();
115-
return null;
116-
});
83+
Map<Long, Integer> cachedWins =
84+
winsByGuild.computeIfAbsent(guildId, _ -> new ConcurrentHashMap<>());
85+
Instant lastFetched = lastFetchedPerGuild.get(guildId);
86+
87+
fetchNewMessages(hallOfFame, lastFetched).thenAccept(newMessages -> {
88+
if (!newMessages.isEmpty()) {
89+
countWinsInto(newMessages, cachedWins);
90+
lastFetchedPerGuild.put(guildId,
91+
newMessages.getFirst().getTimeCreated().toInstant());
92+
}
93+
sendLeaderboard(guild, cachedWins, hook);
94+
}).exceptionally(error -> {
95+
logger.error("Failed to read hall of fame channel", error);
96+
hook.editOriginal("Failed to read the hall of fame channel.").queue();
97+
return null;
98+
});
99+
}
100+
101+
private static CompletableFuture<List<Message>> fetchNewMessages(TextChannel channel,
102+
Instant lastFetched) {
103+
if (lastFetched == null) {
104+
return channel.getIterableHistory().takeAsync(HISTORY_LIMIT);
117105
}
106+
return channel.getIterableHistory()
107+
.takeWhileAsync(HISTORY_LIMIT,
108+
msg -> msg.getTimeCreated().toInstant().isAfter(lastFetched));
118109
}
119110

120111
private void sendLeaderboard(Guild guild, Map<Long, Integer> wins, InteractionHook hook) {
121112
List<Map.Entry<Long, Integer>> sorted = wins.entrySet()
122113
.stream()
123-
.sorted(Map.Entry.<Long, Integer>comparingByValue(Comparator.reverseOrder()))
114+
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
124115
.limit(TOP_LIMIT)
125116
.toList();
126117

0 commit comments

Comments
 (0)