make the help command better

This commit is contained in:
Lee 2024-06-27 16:24:08 +01:00
parent a7c3e2d745
commit 50b921c66d
7 changed files with 41 additions and 6 deletions

@ -1,6 +1,7 @@
package cc.fascinated.bat.command;
import lombok.Getter;
import lombok.Setter;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
@ -10,7 +11,7 @@ import java.util.List;
/**
* @author Fascinated (fascinated7)
*/
@Getter
@Getter @Setter
public class BatSubCommand implements BatCommandExecutor {
/**
* The command data for the slash command
@ -22,6 +23,11 @@ public class BatSubCommand implements BatCommandExecutor {
*/
private final List<Permission> requiredPermissions;
/**
* The commands snowflake from Discord
*/
private long commandSnowflake;
public BatSubCommand(String name, String description, Permission... permissions) {
this.commandData = new SubcommandData(name, description);
this.requiredPermissions = List.of(permissions);

@ -16,6 +16,7 @@ public enum Category {
GENERAL(Emoji.fromUnicode("U+2699"), "General"),
FUN(Emoji.fromFormatted("U+1F973"), "Fun"),
SERVER(Emoji.fromFormatted("U+1F5A5"), "Server"),
UTILITY(Emoji.fromFormatted("U+1F6E0"), "Utility"),
BEAT_SABER(Emoji.fromFormatted("U+1FA84"), "Beat Saber");
/**

@ -2,6 +2,7 @@ package cc.fascinated.bat.command.impl;
import cc.fascinated.bat.Consts;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.event.EventListener;
@ -13,6 +14,7 @@ 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.commands.build.SubcommandData;
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;
@ -22,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* @author Fascinated (fascinated7)
@ -72,8 +76,26 @@ public class HelpCommand extends BatCommand implements EventListener {
}
String commands = "";
for (BatCommand command : commandService.getCommands().values()) {
if (command.getCategory() == category) {
List<BatCommand> categoryCommands = commandService.getCommands().values().stream()
.filter(command -> command.getCategory() == category)
.toList();
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;
}
commands += "</%s:%s> - %s\n".formatted(
command.getName(),
command.getCommandSnowflake(),

@ -18,6 +18,6 @@ public class AutoRoleFeature extends Feature {
public AutoRoleFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) {
super("AutoRole", Category.SERVER);
commandService.registerCommand(context.getBean(AutoRoleCommand.class));
registerCommand(commandService, context.getBean(AutoRoleCommand.class));
}
}

@ -20,7 +20,7 @@ public class BirthdayFeature extends Feature {
private final GuildService guildService;
public BirthdayFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService, @NonNull GuildService guildService) {
super("Birthday", Category.GENERAL);
super("Birthday", Category.UTILITY);
this.guildService = guildService;
registerCommand(commandService, context.getBean(BirthdayCommand.class));

@ -89,6 +89,11 @@ public class CommandService extends ListenerAdapter {
List<Command> discordCommands = jda.updateCommands().addCommands(commands.values().stream().map(BatCommand::getCommandData).toList()).complete();
for (Command discordCommand : discordCommands) {
commands.get(discordCommand.getName()).setCommandSnowflake(discordCommand.getIdLong());
if (!discordCommand.getSubcommands().isEmpty()) {
for (Command.Subcommand subCommand : discordCommand.getSubcommands()) {
commands.get(discordCommand.getName()).getSubCommands().get(subCommand.getName()).setCommandSnowflake(subCommand.getIdLong());
}
}
}
log.info("Registered all slash commands in {}ms", System.currentTimeMillis() - before);
}

@ -28,7 +28,8 @@ public class DiscordService {
"over {guilds} guilds",
"over {users} users",
"over ScoreSaber scores",
"your messages"
"your messages",
"/help for help"
);
@Autowired