2024-06-27 16:01:27 +01:00
|
|
|
package cc.fascinated.bat.command.impl;
|
|
|
|
|
|
|
|
import cc.fascinated.bat.Consts;
|
|
|
|
import cc.fascinated.bat.command.BatCommand;
|
2024-06-27 16:24:08 +01:00
|
|
|
import cc.fascinated.bat.command.BatSubCommand;
|
2024-06-27 16:01:27 +01:00
|
|
|
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;
|
2024-06-27 17:38:28 +01:00
|
|
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
2024-06-27 16:01:27 +01:00
|
|
|
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
2024-06-27 17:38:28 +01:00
|
|
|
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
2024-06-27 16:01:27 +01:00
|
|
|
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
2024-06-27 16:24:08 +01:00
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
2024-06-27 16:01:27 +01:00
|
|
|
import net.dv8tion.jda.api.interactions.components.ActionRow;
|
2024-06-27 17:38:28 +01:00
|
|
|
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
|
2024-06-27 16:01:27 +01:00
|
|
|
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;
|
|
|
|
|
2024-06-27 17:38:28 +01:00
|
|
|
import java.util.ArrayList;
|
2024-06-27 16:01:27 +01:00
|
|
|
import java.util.Arrays;
|
2024-06-27 16:24:08 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2024-06-27 16:01:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) {
|
2024-06-27 17:38:28 +01:00
|
|
|
interaction.replyEmbeds(createHomeEmbed()).addComponents(createHomeActions()).queue();
|
2024-06-27 16:01:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStringSelectInteraction(BatGuild guild, @NonNull BatUser user, @NonNull StringSelectInteractionEvent event) {
|
2024-06-27 17:38:28 +01:00
|
|
|
String item = event.getSelectedOptions().get(0).getValue();
|
|
|
|
if (item.equalsIgnoreCase("home")) {
|
|
|
|
event.editMessageEmbeds(createHomeEmbed()).queue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Category category = Category.getByName(item);
|
2024-06-27 16:01:27 +01:00
|
|
|
if (category == null) {
|
|
|
|
event.reply("Invalid category selected.").queue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String commands = "";
|
2024-06-27 16:30:47 +01:00
|
|
|
List<BatCommand> categoryCommands = commandService.getCommandsByCategory(category);
|
2024-06-27 16:24:08 +01:00
|
|
|
if (categoryCommands.isEmpty()) {
|
|
|
|
commands = "No commands available in this category.";
|
|
|
|
} else {
|
|
|
|
for (BatCommand command : categoryCommands) {
|
|
|
|
if (!command.getSubCommands().isEmpty()) {
|
|
|
|
for (Map.Entry<String, BatSubCommand> entry : command.getSubCommands().entrySet()) {
|
|
|
|
BatSubCommand subCommand = entry.getValue();
|
|
|
|
SubcommandData commandData = subCommand.getCommandData();
|
|
|
|
commands += "</%s %s:%s> - %s\n".formatted(
|
|
|
|
command.getName(),
|
|
|
|
commandData.getName(),
|
|
|
|
subCommand.getCommandSnowflake(),
|
|
|
|
commandData.getDescription()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-27 16:01:27 +01:00
|
|
|
commands += "</%s:%s> - %s\n".formatted(
|
|
|
|
command.getName(),
|
|
|
|
command.getCommandSnowflake(),
|
|
|
|
command.getDescription()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 17:38:28 +01:00
|
|
|
int subCommands = categoryCommands.stream().mapToInt(command -> command.getSubCommands().size()).sum();
|
2024-06-27 16:01:27 +01:00
|
|
|
event.editMessageEmbeds(EmbedUtils.genericEmbed()
|
2024-06-27 17:38:28 +01:00
|
|
|
.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<SelectOption> 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()
|
|
|
|
)
|
|
|
|
};
|
2024-06-27 16:01:27 +01:00
|
|
|
}
|
2024-06-27 17:38:28 +01:00
|
|
|
}
|