From 5758c9c0b23b0ca02fad8b4e8d432eb584a7ea8f Mon Sep 17 00:00:00 2001 From: Chudy#1294 <111442524+ChudziudgiToJa@users.noreply.github.com> Date: Wed, 6 Dec 2023 03:22:46 +0100 Subject: [PATCH] GH-138 Fix leaderboards null user. (#138) --- .../eternalcode/discordapp/DiscordApp.java | 13 ++++-- .../LeaderboardButtonController.java | 41 +++++++++---------- .../leaderboard/LeaderboardCommand.java | 31 ++++++++------ .../leaderboard/LeaderboardService.java | 14 ++++++- 4 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/eternalcode/discordapp/DiscordApp.java b/src/main/java/com/eternalcode/discordapp/DiscordApp.java index 61ebef55..284bcba1 100644 --- a/src/main/java/com/eternalcode/discordapp/DiscordApp.java +++ b/src/main/java/com/eternalcode/discordapp/DiscordApp.java @@ -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; @@ -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; @@ -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(); @@ -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) diff --git a/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardButtonController.java b/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardButtonController.java index 15e1b193..5661fdbe 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardButtonController.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardButtonController.java @@ -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; @@ -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++; @@ -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) { @@ -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(); } } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardCommand.java b/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardCommand.java index e3a534ba..1b19b472 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardCommand.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardCommand.java @@ -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; @@ -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(); } } diff --git a/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardService.java b/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardService.java index 0459f631..8cad40f4 100644 --- a/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardService.java +++ b/src/main/java/com/eternalcode/discordapp/leveling/leaderboard/LeaderboardService.java @@ -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 { @@ -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; + } } - }