|
| 1 | +package net.javadiscord.javabot.systems.help.commands.subcommands; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 4 | +import net.dv8tion.jda.api.entities.Emoji; |
| 5 | +import net.dv8tion.jda.api.entities.Guild; |
| 6 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 7 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 8 | +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 10 | +import net.dv8tion.jda.api.interactions.components.ActionRow; |
| 11 | +import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; |
| 12 | +import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; |
| 13 | +import net.dv8tion.jda.internal.interactions.component.ButtonImpl; |
| 14 | +import net.javadiscord.javabot.Bot; |
| 15 | +import net.javadiscord.javabot.command.ResponseException; |
| 16 | +import net.javadiscord.javabot.command.interfaces.SlashCommand; |
| 17 | +import net.javadiscord.javabot.data.h2db.DbHelper; |
| 18 | +import net.javadiscord.javabot.systems.help.dao.HelpAccountRepository; |
| 19 | +import net.javadiscord.javabot.systems.help.model.HelpAccount; |
| 20 | + |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +/** |
| 26 | + * Command that generates a leaderboard based on the help channel experience. |
| 27 | + */ |
| 28 | +public class ExperienceLeaderboardSubcommand implements SlashCommand { |
| 29 | + |
| 30 | + private static final int PAGE_SIZE = 5; |
| 31 | + |
| 32 | + @Override |
| 33 | + public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) throws ResponseException { |
| 34 | + int page = event.getOption("page", 1, OptionMapping::getAsInt); |
| 35 | + DbHelper.doDaoAction(HelpAccountRepository::new, dao -> |
| 36 | + event.getHook().sendMessageEmbeds(buildExperienceLeaderboard(event.getGuild(), dao, page)) |
| 37 | + .addActionRows(buildPageControls(page)) |
| 38 | + .queue()); |
| 39 | + return event.deferReply(false); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Handles all Button Interactions that regard this command. |
| 44 | + * |
| 45 | + * @param event The {@link ButtonInteractionEvent} that was fired upon use. |
| 46 | + * @param id The component's id, split by ":". |
| 47 | + */ |
| 48 | + public static void handleButtons(ButtonInteractionEvent event, String[] id) { |
| 49 | + event.deferEdit().queue(); |
| 50 | + DbHelper.doDaoAction(HelpAccountRepository::new, dao -> { |
| 51 | + int page = Integer.parseInt(id[2]); |
| 52 | + switch (id[1]) { |
| 53 | + case "left" -> page--; |
| 54 | + case "right" -> page++; |
| 55 | + } |
| 56 | + int maxPage = dao.getTotalRankedAccounts() / PAGE_SIZE; |
| 57 | + if (page <= 0) page = maxPage; |
| 58 | + if (page > maxPage) page = 1; |
| 59 | + event.getHook().editOriginalEmbeds(buildExperienceLeaderboard(event.getGuild(), dao, page)) |
| 60 | + .setActionRows(buildPageControls(page)) |
| 61 | + .queue(); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + private static MessageEmbed buildExperienceLeaderboard(Guild guild, HelpAccountRepository dao, int page) throws SQLException { |
| 66 | + int maxPage = dao.getTotalRankedAccounts() / PAGE_SIZE; |
| 67 | + List<HelpAccount> accounts = dao.getAccountsWithRank(Math.min(page, maxPage), PAGE_SIZE); |
| 68 | + EmbedBuilder builder = new EmbedBuilder() |
| 69 | + .setTitle("Experience Leaderboard") |
| 70 | + .setColor(Bot.config.get(guild).getSlashCommand().getDefaultColor()) |
| 71 | + .setFooter(String.format("Page %s/%s", Math.min(page, maxPage), maxPage)); |
| 72 | + accounts.forEach(account -> { |
| 73 | + Map.Entry<Long, Double> currentRole = account.getCurrentExperienceGoal(guild); |
| 74 | + builder.addField( |
| 75 | + // TODO: implement without using blocking complete calls |
| 76 | + String.format("**%s.** %s", (accounts.indexOf(account) + 1) + (page - 1) * PAGE_SIZE, guild.getJDA().retrieveUserById(account.getUserId()).complete().getAsTag()), |
| 77 | + String.format("<@&%s>: `%.0f XP`\n", currentRole.getKey(), account.getExperience()), |
| 78 | + false); |
| 79 | + }); |
| 80 | + return builder.build(); |
| 81 | + } |
| 82 | + |
| 83 | + private static ActionRow buildPageControls(int currentPage) { |
| 84 | + return ActionRow.of( |
| 85 | + new ButtonImpl("experience-leaderboard:left:" + currentPage, "", ButtonStyle.SECONDARY, false, Emoji.fromUnicode("⬅")), |
| 86 | + new ButtonImpl("experience-leaderboard:right:" + currentPage, "", ButtonStyle.SECONDARY, false, Emoji.fromUnicode("➡")) |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments