Skip to content

Commit

Permalink
GH-138 Fix leaderboards null user. (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChudziudgiToJa committed Dec 8, 2023
1 parent 8f509b9 commit 5758c9c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 39 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/eternalcode/discordapp/DiscordApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.eternalcode.discordapp.review.GitHubReviewService;
import com.eternalcode.discordapp.review.GitHubReviewTask;
import com.eternalcode.discordapp.review.command.GitHubReviewCommand;
import com.eternalcode.discordapp.ticket.command.TicketButtonController;
import com.eternalcode.discordapp.ticket.command.TicketCommand;
import com.eternalcode.discordapp.user.UserRepositoryImpl;
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
Expand All @@ -61,7 +63,6 @@
import java.io.File;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.EnumSet;
import java.util.Timer;

Expand Down Expand Up @@ -144,7 +145,10 @@ public static void main(String... args) throws InterruptedException {

// Leveling
new LevelCommand(levelService),
new LeaderboardCommand(leaderboardService)
new LeaderboardCommand(leaderboardService),

// Ticket
new TicketCommand(config)
)
.build();

Expand All @@ -168,7 +172,10 @@ public static void main(String... args) throws InterruptedException {
new CodeGameAnswerController(codeImageGameData, codeGameConfiguration, data, experienceService),

// leaderboard
new LeaderboardButtonController(leaderboardService)
new LeaderboardButtonController(leaderboardService),

// Ticket
new TicketButtonController()
)

.setAutoReconnect(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.eternalcode.discordapp.leveling.Level;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.components.buttons.Button;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -31,7 +33,8 @@ public void onButtonInteraction(ButtonInteractionEvent event) {
int totalRecords = this.leaderboardService.getTotalRecords();
int totalPages = this.leaderboardService.getTotalPages(totalRecords);

int currentPage = this.currentPageMap.getOrDefault(messageId, 1);
int oldPage = this.currentPageMap.getOrDefault(messageId, 1);
int currentPage = oldPage;

if (componentId.equals("leaderboard_next")) {
currentPage++;
Expand All @@ -50,9 +53,11 @@ public void onButtonInteraction(ButtonInteractionEvent event) {
}

currentPage = Math.max(1, Math.min(currentPage, totalPages));
this.currentPageMap.put(messageId, currentPage);

this.updateLeaderboard(event, currentPage);
if (oldPage != currentPage) {
this.currentPageMap.put(messageId, currentPage);
this.updateLeaderboard(event, currentPage);
}
}

private void updateLeaderboard(ButtonInteractionEvent event, int currentPage) {
Expand All @@ -70,30 +75,24 @@ private void updateLeaderboard(ButtonInteractionEvent event, int currentPage) {

for (Level level : top) {
int userLevel = level.getCurrentLevel();
String effectiveName = event.getGuild().getMemberById(level.getId()).getEffectiveName();
Member memberById = event.getGuild().getMemberById(level.getId());

if (memberById != null) {
String effectiveName = memberById.getEffectiveName();

leaderboardContent.append(this.leaderboardService.formatLeaderboardEntry(index, effectiveName, userLevel)).append("\n");
index++;
leaderboardContent.append(this.leaderboardService.formatLeaderboardEntry(index, effectiveName, userLevel)).append("\n");
index++;
}
}

Button firstButton = Button.success("leaderboard_first", "First")
.withEmoji(Emoji.fromUnicode("U+23EE"))
.withDisabled(currentPage == 1);
Button firstButton = Button.success("leaderboard_first", "First").withEmoji(Emoji.fromUnicode("U+23EE")).withDisabled(currentPage == 1);

Button prevButton = Button.primary("leaderboard_prev", "Previous")
.withEmoji(Emoji.fromFormatted("U+25C0"))
.withDisabled(currentPage <= 1);
Button prevButton = Button.primary("leaderboard_prev", "Previous").withEmoji(Emoji.fromFormatted("U+25C0")).withDisabled(currentPage <= 1);

Button nextButton = Button.primary("leaderboard_next", "Next")
.withEmoji(Emoji.fromUnicode("U+25B6"))
.withDisabled(currentPage >= totalPages);
Button nextButton = Button.primary("leaderboard_next", "Next").withEmoji(Emoji.fromUnicode("U+25B6")).withDisabled(currentPage >= totalPages);

Button lastButton = Button.success("leaderboard_last", "Last")
.withEmoji(Emoji.fromUnicode("U+23ED"))
.withDisabled(currentPage == totalPages);
Button lastButton = Button.success("leaderboard_last", "Last").withEmoji(Emoji.fromUnicode("U+23ED")).withDisabled(currentPage == totalPages);

event.editMessageEmbeds(embedBuilder.setDescription(leaderboardContent.toString()).build())
.setActionRow(firstButton, prevButton, nextButton, lastButton)
.queue();
event.editMessageEmbeds(embedBuilder.setDescription(leaderboardContent.toString()).build()).setActionRow(firstButton, prevButton, nextButton, lastButton).queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.entities.emoji.Emoji;

Expand Down Expand Up @@ -45,30 +46,34 @@ public void execute(SlashCommandEvent event) {

for (Level level : top) {
int userLevel = level.getCurrentLevel();
String effectiveName = event.getGuild().getMemberById(level.getId()).getEffectiveName();
Member memberById = event.getGuild().getMemberById(level.getId());

leaderboardContent.append(this.leaderboardService.formatLeaderboardEntry(index, effectiveName, userLevel)).append("\n");
index++;
if (memberById != null) {
String effectiveName = memberById.getEffectiveName();

leaderboardContent.append(this.leaderboardService.formatLeaderboardEntry(index, effectiveName, userLevel)).append("\n");
index++;
}
}

Button firstButton = Button.success("leaderboard_first", "First")
.withEmoji(Emoji.fromUnicode("U+23EE"))
.withDisabled(page == 1);
.withEmoji(Emoji.fromUnicode("U+23EE"))
.withDisabled(page == 1);

Button prevButton = Button.primary("leaderboard_prev", "Previous")
.withEmoji(Emoji.fromFormatted("U+25C0"))
.withDisabled(page <= 1);
.withEmoji(Emoji.fromFormatted("U+25C0"))
.withDisabled(page <= 1);

Button nextButton = Button.primary("leaderboard_next", "Next")
.withEmoji(Emoji.fromUnicode("U+25B6"))
.withDisabled(page >= totalPages);
.withEmoji(Emoji.fromUnicode("U+25B6"))
.withDisabled(page >= totalPages);

Button lastButton = Button.success("leaderboard_last", "Last")
.withEmoji(Emoji.fromUnicode("U+23ED"))
.withDisabled(page == totalPages);
.withEmoji(Emoji.fromUnicode("U+23ED"))
.withDisabled(page == totalPages);

event.replyEmbeds(embedBuilder.setDescription(leaderboardContent.toString()).build())
.addActionRow(firstButton, prevButton, nextButton, lastButton)
.queue();
.addActionRow(firstButton, prevButton, nextButton, lastButton)
.queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import net.dv8tion.jda.api.EmbedBuilder;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class LeaderboardService {

Expand Down Expand Up @@ -39,7 +45,11 @@ public int getTotalPages(int totalRecords) {
}

public int getTotalRecords() {
return this.levelService.getTotalRecordsCount().join();
try {
return this.levelService.getTotalRecordsCount().get(5, TimeUnit.SECONDS);
}
catch (InterruptedException | ExecutionException | TimeoutException e) {
return 0;
}
}

}

0 comments on commit 5758c9c

Please sign in to comment.