Make help command use descriptions and usages
This commit is contained in:
parent
e08fefbda3
commit
496304c2c3
@ -0,0 +1,23 @@
|
||||
package wtf.beatrice.hidekobot.commands.base;
|
||||
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Alias
|
||||
{
|
||||
public static String generateNiceAliases(MessageCommand command)
|
||||
{
|
||||
LinkedList<String> aliases = command.getCommandLabels();
|
||||
StringBuilder aliasesStringBuilder = new StringBuilder();
|
||||
for(int i = 0; i < aliases.size(); i++)
|
||||
{
|
||||
aliasesStringBuilder.append("`").append(aliases.get(i)).append("`");
|
||||
|
||||
if(i + 1 != aliases.size())
|
||||
aliasesStringBuilder.append(", "); // separate with comma except on last iteration
|
||||
}
|
||||
|
||||
return aliasesStringBuilder.toString();
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.commands.base.Alias;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
|
||||
@ -37,6 +38,18 @@ public class AliasCommand implements MessageCommand
|
||||
return CommandCategory.TOOLS;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "See other command aliases.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<command>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runCommand(MessageReceivedEvent event, String label, String[] args)
|
||||
{
|
||||
@ -54,19 +67,11 @@ public class AliasCommand implements MessageCommand
|
||||
return;
|
||||
}
|
||||
|
||||
LinkedList<String> aliases = command.getCommandLabels();
|
||||
StringBuilder aliasesStringBuilder = new StringBuilder();
|
||||
aliasesStringBuilder.append("Aliases for **").append(aliases.get(0)).append("**: ");
|
||||
for(int i = 0; i < aliases.size(); i++)
|
||||
{
|
||||
aliasesStringBuilder.append("`").append(aliases.get(i)).append("`");
|
||||
|
||||
if(i + 1 != aliases.size())
|
||||
aliasesStringBuilder.append(", "); // separate with comma except on last iteration
|
||||
}
|
||||
String aliases = Alias.generateNiceAliases(command);
|
||||
aliases = "Aliases for **" + command.getCommandLabels().get(0) + "**: " + aliases;
|
||||
|
||||
event.getMessage()
|
||||
.reply(aliasesStringBuilder.toString())
|
||||
.reply(aliases)
|
||||
.queue();
|
||||
|
||||
}
|
||||
|
@ -36,6 +36,18 @@ public class AvatarCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Get someone's avatar, or your own. You can additionally specify a resolution.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "[mentioned user] [resolution]";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
@ -47,7 +59,7 @@ public class AvatarCommand implements MessageCommand
|
||||
{
|
||||
int[] acceptedSizes = Cache.getSupportedAvatarResolutions();
|
||||
|
||||
User user = null;
|
||||
User user;
|
||||
int resolution = -1;
|
||||
|
||||
// we have no specific order for user and resolution, so let's try parsing any arg as resolution
|
||||
|
@ -33,6 +33,18 @@ public class BotInfoCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Get general info about the bot.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -5,6 +5,7 @@ import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.commands.base.ClearChat;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
@ -29,12 +30,25 @@ public class ClearCommand implements MessageCommand
|
||||
public boolean passRawArgs() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
return CommandCategory.MODERATION;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Clear the current channel's chat history.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "[amount]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runCommand(MessageReceivedEvent event, String label, String[] args)
|
||||
{
|
||||
|
@ -31,6 +31,18 @@ public class CoinFlipCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Flip a coin.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -30,6 +30,23 @@ public class DiceRollCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Roll dice. You can roll multiple dice at the same time." +
|
||||
"\nExamples:" +
|
||||
"\n - `d8 10` to roll an 8-sided die 10 times." +
|
||||
"\n - `d12 3 d5 10` to roll a 12-sided die 3 times, and then a 5-sided die 10 times." +
|
||||
"\n - `30` to roll a standard 6-sided die 30 times." +
|
||||
"\n - `d10` to roll a 10-sided die once.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "[dice size] [rolls]";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -3,6 +3,7 @@ package wtf.beatrice.hidekobot.commands.message;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
|
||||
@ -26,6 +27,18 @@ public class HelloCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Get pinged by the bot.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -7,6 +7,7 @@ import org.apache.commons.text.WordUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import wtf.beatrice.hidekobot.Cache;
|
||||
import wtf.beatrice.hidekobot.commands.base.Alias;
|
||||
import wtf.beatrice.hidekobot.commands.base.Say;
|
||||
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
|
||||
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
|
||||
@ -34,6 +35,18 @@ public class HelpCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Get general help on the bot. Specify a command if you want specific help about that command.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "[command]";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
@ -91,6 +104,51 @@ public class HelpCommand implements MessageCommand
|
||||
embedBuilder.addField(niceCategoryName, commandsList.toString(), false);
|
||||
}
|
||||
|
||||
event.getMessage().replyEmbeds(embedBuilder.build()).queue();
|
||||
} else {
|
||||
|
||||
String commandLabel = args[0].toLowerCase();
|
||||
MessageCommand command = Cache.getMessageCommandListener().getRegisteredCommand(commandLabel);
|
||||
if(command == null)
|
||||
{
|
||||
event.getMessage().reply("Unrecognized command: `" + commandLabel + "`!").queue(); // todo prettier
|
||||
return;
|
||||
}
|
||||
|
||||
commandLabel = command.getCommandLabels().get(0);
|
||||
String usage = "`" + Cache.getBotPrefix() + " " + commandLabel;
|
||||
String internalUsage = command.getUsage();
|
||||
if(internalUsage != null) usage += " " + internalUsage;
|
||||
usage += "`";
|
||||
|
||||
String aliases = Alias.generateNiceAliases(command);
|
||||
|
||||
List<Permission> permissions = command.getPermissions();
|
||||
StringBuilder permissionsStringBuilder = new StringBuilder();
|
||||
if(permissions == null)
|
||||
{
|
||||
permissionsStringBuilder = new StringBuilder("Available to everyone");
|
||||
} else {
|
||||
for(int i = 0; i < permissions.size(); i++)
|
||||
{
|
||||
Permission permission = permissions.get(i);
|
||||
permissionsStringBuilder.append("`").append(permission.getName()).append("`");
|
||||
|
||||
if(i + 1 != permissions.size())
|
||||
permissionsStringBuilder.append(", "); // separate with comma expect on last iteration
|
||||
}
|
||||
}
|
||||
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||
|
||||
embedBuilder.setColor(Cache.getBotColor());
|
||||
embedBuilder.setTitle(WordUtils.capitalizeFully(commandLabel + " help"));
|
||||
|
||||
embedBuilder.addField("Description", command.getDescription(), false);
|
||||
embedBuilder.addField("Usage", usage, false);
|
||||
embedBuilder.addField("Aliases", aliases, false);
|
||||
embedBuilder.addField("Permissions", permissionsStringBuilder.toString(), false);
|
||||
|
||||
event.getMessage().replyEmbeds(embedBuilder.build()).queue();
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,18 @@ public class InviteCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Get the bot's invite link.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -39,6 +39,18 @@ public class LoveCalculatorCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Calculate how much two people love each other. You can mention two people or just one.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<person 1> [person 2]";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
@ -55,7 +67,9 @@ public class LoveCalculatorCommand implements MessageCommand
|
||||
|
||||
if(args.length == 0 || mentions.isEmpty())
|
||||
{
|
||||
event.getMessage().reply("\uD83D\uDE22 I need to know who to check!").queue();
|
||||
event.getMessage()
|
||||
.reply("\uD83D\uDE22 I need to know who to check! Please mention them.")
|
||||
.queue();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,18 @@ public class MagicBallCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Ask a question to the Magic Ball.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<question>";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -30,6 +30,18 @@ public class SayCommand implements MessageCommand
|
||||
return true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Make the bot say something for you.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<text>";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -36,6 +36,18 @@ public class UrbanDictionaryCommand implements MessageCommand
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Look something up in the Urban Dictionary.";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "<query>";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CommandCategory getCategory() {
|
||||
|
@ -48,6 +48,23 @@ public interface MessageCommand
|
||||
@NotNull
|
||||
CommandCategory getCategory();
|
||||
|
||||
/**
|
||||
* Say what this command does.
|
||||
*
|
||||
* @return a String explaining what this command does.
|
||||
*/
|
||||
@NotNull
|
||||
String getDescription();
|
||||
|
||||
/**
|
||||
* Say how people should use this command.
|
||||
*
|
||||
* @return a String explaining how to use the command, excluding the bot prefix and command name. Null if no parameter is needed
|
||||
*/
|
||||
@Nullable
|
||||
String getUsage();
|
||||
|
||||
|
||||
/**
|
||||
* Run the command logic by parsing the event and replying accordingly.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user