Skip to content

Commit

Permalink
Fix leaderboards null user.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChudziudgiToJa committed Dec 6, 2023
1 parent 8f509b9 commit 4568d6e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -70,30 +71,34 @@ 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());

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(currentPage == 1);
.withEmoji(Emoji.fromUnicode("U+23EE"))
.withDisabled(currentPage == 1);

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

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

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

event.editMessageEmbeds(embedBuilder.setDescription(leaderboardContent.toString()).build())
.setActionRow(firstButton, prevButton, nextButton, lastButton)
.queue();
.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();
}
}

0 comments on commit 4568d6e

Please sign in to comment.