From 496304c2c311af67d200a127c49fa6720991e477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 20 Dec 2022 17:51:28 +0100 Subject: [PATCH] Make help command use descriptions and usages --- .../hidekobot/commands/base/Alias.java | 23 ++++++++ .../commands/message/AliasCommand.java | 27 +++++---- .../commands/message/AvatarCommand.java | 14 ++++- .../commands/message/BotInfoCommand.java | 12 ++++ .../commands/message/ClearCommand.java | 14 +++++ .../commands/message/CoinFlipCommand.java | 12 ++++ .../commands/message/DiceRollCommand.java | 17 ++++++ .../commands/message/HelloCommand.java | 13 +++++ .../commands/message/HelpCommand.java | 58 +++++++++++++++++++ .../commands/message/InviteCommand.java | 12 ++++ .../message/LoveCalculatorCommand.java | 16 ++++- .../commands/message/MagicBallCommand.java | 12 ++++ .../commands/message/SayCommand.java | 12 ++++ .../message/UrbanDictionaryCommand.java | 12 ++++ .../objects/commands/MessageCommand.java | 17 ++++++ 15 files changed, 258 insertions(+), 13 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java new file mode 100644 index 0000000..4a5d521 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Alias.java @@ -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 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(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java index 6ce907e..4904dd6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/AliasCommand.java @@ -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 ""; + } + @Override public void runCommand(MessageReceivedEvent event, String label, String[] args) { @@ -54,19 +67,11 @@ public class AliasCommand implements MessageCommand return; } - LinkedList 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(); } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java index 8f89d54..8db4e4d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/AvatarCommand.java @@ -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 diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java index 6a22880..3a8665c 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/BotInfoCommand.java @@ -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() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java index b176eb2..e634cb5 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/ClearCommand.java @@ -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) { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java index 668a3d2..26a5bf6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java @@ -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() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java index 263c5a1..a9d13b1 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/DiceRollCommand.java @@ -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() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java index d0e52bf..62390ae 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/HelloCommand.java @@ -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() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java index 799eb64..a2e109e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/HelpCommand.java @@ -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 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(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java index 62bd641..8075545 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/InviteCommand.java @@ -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() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java index 96ef6fe..9ddf55b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/LoveCalculatorCommand.java @@ -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 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; } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java index 2497336..f1faf18 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java @@ -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 ""; + } + @NotNull @Override public CommandCategory getCategory() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/SayCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/SayCommand.java index 05d956d..9f2cd8d 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/SayCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/SayCommand.java @@ -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 ""; + } + @NotNull @Override public CommandCategory getCategory() { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java index 06877bb..2fd5e32 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/UrbanDictionaryCommand.java @@ -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 ""; + } + @NotNull @Override public CommandCategory getCategory() { diff --git a/src/main/java/wtf/beatrice/hidekobot/objects/commands/MessageCommand.java b/src/main/java/wtf/beatrice/hidekobot/objects/commands/MessageCommand.java index f861657..4088447 100644 --- a/src/main/java/wtf/beatrice/hidekobot/objects/commands/MessageCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/objects/commands/MessageCommand.java @@ -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. *