package cc.fascinated.bat.command.impl; import cc.fascinated.bat.Consts; import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.Category; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.event.EventListener; import cc.fascinated.bat.model.BatGuild; 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.channel.middleman.MessageChannel; import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.components.ActionRow; 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; import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Arrays; /** * @author Fascinated (fascinated7) */ @Component public class HelpCommand extends BatCommand implements EventListener { private final CommandService commandService; @Autowired public HelpCommand(@NonNull CommandService commandService) { super("help", "View the bots command categories."); this.commandService = commandService; } @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.getCommands().values().stream().filter(command -> command.getCategory() == category).count(); 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(); } @Override public void onStringSelectInteraction(BatGuild guild, @NonNull BatUser user, @NonNull StringSelectInteractionEvent event) { Category category = Category.getByName(event.getSelectedOptions().get(0).getValue()); if (category == null) { event.reply("Invalid category selected.").queue(); return; } String commands = ""; for (BatCommand command : commandService.getCommands().values()) { if (command.getCategory() == category) { commands += " - %s\n".formatted( command.getName(), command.getCommandSnowflake(), command.getDescription() ); } } event.editMessageEmbeds(EmbedUtils.genericEmbed() .setDescription("The available commands in the **%s** category: \n\n%s".formatted(category.getName(), commands)) .build()).queue(); } }