From 8fec0271cc7e7f0dd9faf23d0ce3bf409a5dd457 Mon Sep 17 00:00:00 2001 From: anthorob Date: Thu, 19 Dec 2024 01:28:40 -0500 Subject: [PATCH] show available choices, types, maximum and minimum length in command help message --- .../context/MessageCommandContext.java | 13 +-- .../core/command/options/CommandOptions.java | 90 +++++++++++++------ 2 files changed, 69 insertions(+), 34 deletions(-) diff --git a/src/main/java/me/bobert/netneutrality/context/MessageCommandContext.java b/src/main/java/me/bobert/netneutrality/context/MessageCommandContext.java index f6a1796..91fcbf8 100644 --- a/src/main/java/me/bobert/netneutrality/context/MessageCommandContext.java +++ b/src/main/java/me/bobert/netneutrality/context/MessageCommandContext.java @@ -129,7 +129,6 @@ public class MessageCommandContext extends CommandContext { try { switch (type) { case STRING -> { - //TODO: check length String str = args[index]; if (str.contains("<@") && str.contains(">")) { @@ -254,12 +253,11 @@ public class MessageCommandContext extends CommandContext { invalidateContext(data, "provided argument is not a valid user or role."); } case CHANNEL -> { - //TODO: check types if (mentions.getChannels().size() > channelMentionIndex) { GuildChannel channel = mentions.getChannels().get(channelMentionIndex++); if (!requiredChannelTypes.contains(channel.getType())) { - invalidateContext(data, "not a valid channel type at index " + channelMentionIndex + + invalidateContext(data, "not a valid channel type at index " + (channelMentionIndex -1) + " , valid types are: " + validTypes(requiredChannelTypes)); break; } @@ -312,14 +310,11 @@ public class MessageCommandContext extends CommandContext { @Override public String parseCommandName() { - String rawMessage = message.getContentDisplay().trim(); + String rawMessage = message.getContentRaw().trim(); String prefix = Settings.getInstance().getPrefix(); if (rawMessage.startsWith(prefix)) { - rawMessage = rawMessage.replaceFirst(String.format("^[%s\\s]{1,}", prefix), "").trim(); - // rawMessage = rawMessage.substring(prefix.length()).trim(); - - return rawMessage.split(" ", 2)[0]; + return getFullCommand().split(" ", 2)[0]; } return null; @@ -330,6 +325,6 @@ public class MessageCommandContext extends CommandContext { String rawMessage = message.getContentRaw().trim(); String prefix = Settings.getInstance().getPrefix(); - return rawMessage.replaceFirst(String.format("^[%s\\s]{1,}", prefix), "").trim(); + return rawMessage.replaceFirst("^[" + prefix + "\\s]+", "").trim(); } } diff --git a/src/main/java/me/bobert/netneutrality/core/command/options/CommandOptions.java b/src/main/java/me/bobert/netneutrality/core/command/options/CommandOptions.java index 0d60fd5..70d55cd 100644 --- a/src/main/java/me/bobert/netneutrality/core/command/options/CommandOptions.java +++ b/src/main/java/me/bobert/netneutrality/core/command/options/CommandOptions.java @@ -6,6 +6,7 @@ package me.bobert.netneutrality.core.command.options; */ import me.bobert.netneutrality.core.command.Command; +import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; @@ -13,6 +14,8 @@ import net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData; import net.dv8tion.jda.internal.interactions.CommandDataImpl; import java.util.List; +import java.util.Set; +import java.util.StringJoiner; public class CommandOptions { private final Command command; @@ -54,7 +57,6 @@ public class CommandOptions { } public String printUsage() { - //TODO: show available choices if option has them StringBuilder usage = new StringBuilder(); if (!options.isEmpty()) { usage.append("- Usage: /").append(command.getCommandName()); @@ -62,29 +64,19 @@ public class CommandOptions { usage.append(" <").append(option.getName()).append(">"); } - for (OptionData option : options) { - usage.append("\n - "); - if (option.isRequired()) { - usage.append("*"); - } - usage.append(option.getName()).append(": ").append(option.getDescription()); - } + usage.append(printOptions(options, null, false)); } if (!subcommands.isEmpty()) { for (SubcommandData subcommand : subcommands) { usage.append("- Usage: /").append(command.getCommandName()).append(" ").append(subcommand.getName()); + for (OptionData option : subcommand.getOptions()) { usage.append(" <").append(option.getName()).append(">"); } - for (OptionData option : subcommand.getOptions()) { - usage.append("\n - "); - if (option.isRequired()) { - usage.append("*"); - } - usage.append(option.getName()).append(": ").append(option.getDescription()); - } + usage.append(" (").append(subcommand.getDescription()).append(")"); + usage.append(printOptions(subcommand.getOptions(), null, false)); usage.append("\n"); } @@ -94,7 +86,7 @@ public class CommandOptions { for (SubcommandGroupData subcommandGroup : subcommandGroups) { usage.append("- Subcommand: /").append(command.getCommandName()) .append(" ").append(subcommandGroup.getName()) - .append(" (Description: ").append(subcommandGroup.getDescription()).append(")") + .append(" (").append(subcommandGroup.getDescription()).append(")") .append("\n"); for (SubcommandData subcommand : subcommandGroup.getSubcommands()) { @@ -105,18 +97,10 @@ public class CommandOptions { for (OptionData option : subcommand.getOptions()) { usage.append(" <").append(option.getName()).append(">"); } - usage.append(" (Description: ").append(subcommand.getDescription()).append(")"); - for (OptionData option : subcommand.getOptions()) { - usage.append("\n - "); - if (option.isRequired()) { - usage.append("*"); - } - usage.append(option.getName()).append(": ").append(option.getDescription()); - } + usage.append(printOptions(subcommand.getOptions(), subcommand.getDescription(), true)); usage.append("\n"); - } } } @@ -127,4 +111,60 @@ public class CommandOptions { return usage.toString(); } + + private String printOptions(List options, String description, boolean group) { + StringBuilder usage = new StringBuilder(); + + if (description != null) { + usage.append(" (").append(description).append(")"); + } + + for (OptionData option : options) { + usage.append("\n"); + if (group) usage.append(" "); + usage.append(" - "); + if (option.isRequired()) { + usage.append("*"); + } + usage.append(option.getName()).append(": ").append(option.getDescription()); + + if (option.getMinValue() != null) { + usage.append(" [Min value: ").append(option.getMinValue().longValue()).append("]"); + } + + if (option.getMaxValue() != null) { + usage.append(" [Max value: ").append(option.getMaxValue().longValue()).append("]"); + } + + if (option.getMinLength() != null) { + usage.append(" [Min length: ").append(option.getMinLength().longValue()).append("]"); + } + + if (option.getMaxLength() != null) { + usage.append(" [Max length: ").append(option.getMaxLength().longValue()).append("]"); + } + + List choices = option.getChoices(); + if (!choices.isEmpty()) { + StringJoiner joiner = new StringJoiner(", "); + for (net.dv8tion.jda.api.interactions.commands.Command.Choice choice : choices) { + joiner.add(choice.getAsString()); + } + + usage.append(" [Available choices: ").append(joiner).append("]"); + } + + Set requiredChannelTypes = option.getChannelTypes(); + if (!requiredChannelTypes.isEmpty()) { + StringJoiner joiner = new StringJoiner(", "); + for (ChannelType type : requiredChannelTypes) { + joiner.add(type.name()); + } + + usage.append(" [Required channel types: ").append(joiner).append("]"); + } + } + + return usage.toString(); + } }