From 2c5c1ecc411f1a549fde1e8981b9804c034afab6 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 27 Jun 2024 17:38:28 +0100 Subject: [PATCH] cleanup home command --- .../bat/command/impl/HelpCommand.java | 95 +++++++++++++------ .../fascinated/bat/service/UserService.java | 2 +- 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java b/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java index 090e0b9..ce7ec65 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/HelpCommand.java @@ -11,11 +11,14 @@ import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.service.CommandService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.LayoutComponent; import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle; import net.dv8tion.jda.api.interactions.components.selections.SelectOption; @@ -23,6 +26,7 @@ import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -42,34 +46,18 @@ public class HelpCommand extends BatCommand implements EventListener { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { - String categories = ""; - for (Category category : Category.values()) { - long commandCount = commandService.getCommandsByCategory(category).size(); - categories += "**%s** - %s Commands\n".formatted(category.getName(), commandCount); - } - - SelectOption[] options = Arrays.stream(Category.values()).map(category -> - SelectOption.of(category.getName(), category.getName()).withEmoji(category.getEmoji())) - .toArray(SelectOption[]::new); - - interaction.replyEmbeds(EmbedUtils.genericEmbed() - .setDescription("Here are the available command categories: \n\n" + categories) - .build()).addComponents( - ActionRow.of( - Button.of(ButtonStyle.LINK, Consts.INVITE_URL, "Invite"), - Button.of(ButtonStyle.LINK, Consts.SUPPORT_INVITE_URL, "Support") - ), - ActionRow.of( - StringSelectMenu.create("help-menu") - .addOptions(options) - .build() - ) - ).queue(); + interaction.replyEmbeds(createHomeEmbed()).addComponents(createHomeActions()).queue(); } @Override public void onStringSelectInteraction(BatGuild guild, @NonNull BatUser user, @NonNull StringSelectInteractionEvent event) { - Category category = Category.getByName(event.getSelectedOptions().get(0).getValue()); + String item = event.getSelectedOptions().get(0).getValue(); + if (item.equalsIgnoreCase("home")) { + event.editMessageEmbeds(createHomeEmbed()).queue(); + return; + } + + Category category = Category.getByName(item); if (category == null) { event.reply("Invalid category selected.").queue(); return; @@ -102,8 +90,61 @@ public class HelpCommand extends BatCommand implements EventListener { } } + int subCommands = categoryCommands.stream().mapToInt(command -> command.getSubCommands().size()).sum(); event.editMessageEmbeds(EmbedUtils.genericEmbed() - .setDescription("There is %S commands in the **%s** category: \n\n%s".formatted(categoryCommands.size(), category.getName(), commands)) - .build()).queue(); + .setAuthor("%s Category".formatted(category.getName())) + .setDescription("%s command%s (with %s sub-command%s)\n\n**Commands:**\n%s".formatted( + categoryCommands.size(), + categoryCommands.size() == 1 ? "" : "s", + subCommands, + subCommands == 1 ? "" : "s", + commands + )).build()).queue(); } -} + + /** + * Creates the home embed for the help command + * + * @return The home embed + */ + private MessageEmbed createHomeEmbed() { + String categories = ""; + for (Category category : Category.values()) { + long commandCount = commandService.getCommandsByCategory(category).size(); + categories += "➜ %s - **%s Command%s**\n".formatted( + category.getName(), + commandCount, + commandCount == 1 ? "" : "s" + ); + } + + return EmbedUtils.genericEmbed() + .setDescription("Here are the available command categories: \n\n" + categories) + .build(); + } + + /** + * Creates the home actions for the help command + * + * @return The layout components + */ + private LayoutComponent[] createHomeActions() { + List options = new ArrayList<>(); + options.add(SelectOption.of("Home", "home").withEmoji(Emoji.fromUnicode("U+1F3E0"))); + options.addAll(Arrays.stream(Category.values()).map(category -> + SelectOption.of(category.getName(), category.getName()).withEmoji(category.getEmoji())) + .toList()); + + return new LayoutComponent[] { + ActionRow.of( + Button.of(ButtonStyle.LINK, Consts.INVITE_URL, "Invite"), + Button.of(ButtonStyle.LINK, Consts.SUPPORT_INVITE_URL, "Support") + ), + ActionRow.of( + StringSelectMenu.create("help-menu") + .addOptions(options) + .build() + ) + }; + } + } diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 18438c6..37b8aed 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -53,7 +53,7 @@ public class UserService { return user; } BatUser user = userRepository.save(new BatUser(id)); - log.info("Created user \"{}\" in {}ms", id, System.currentTimeMillis() - start); + log.info("Created user for \"{}\" in {}ms", user.getDiscordUser().getName(), System.currentTimeMillis() - start); return user; }