|
39 | 39 | import java.util.List; |
40 | 40 |
|
41 | 41 | /** |
42 | | - * This command doesn't use the normal syntax. |
43 | | - * Instead, it provides a single Consumer<CommandEvent> that the CommandClient can use. |
| 42 | + * Show every registered command, or information on a single command. |
44 | 43 | * |
45 | | - * This should not be turned into a normal Command. |
| 44 | + * Possible forms: |
| 45 | + * !help |
| 46 | + * !help help |
| 47 | + * !help addquote |
| 48 | + * !help [command] |
46 | 49 | * |
47 | 50 | * @author Curle |
48 | 51 | */ |
49 | | -public class CmdHelp { |
| 52 | +public class CmdHelp extends Command { |
50 | 53 |
|
51 | 54 | private static final int COMMANDS_PER_PAGE = 25; |
52 | 55 |
|
| 56 | + public CmdHelp() { |
| 57 | + super(); |
| 58 | + name = "help"; |
| 59 | + help = "Show all commands, or detailed information about a particular command."; |
| 60 | + |
| 61 | + arguments = "[command]"; |
| 62 | + } |
| 63 | + |
53 | 64 | /** |
54 | 65 | * Prepare the potential scrolling buttons for a help command, |
55 | 66 | * and send the message with the proper embeds. |
56 | 67 | * |
57 | 68 | * See {@link #getHelpStartingAt(int)} for the implementation. |
58 | | - * @param e |
59 | 69 | */ |
60 | | - public static void execute(CommandEvent e) { |
61 | | - MessageAction reply = e.getChannel().sendMessageEmbeds(getHelpStartingAt(0).build()); |
62 | | - Component[] buttons = createScrollButtons(0); |
63 | | - if (buttons.length > 0) |
64 | | - reply.setActionRow(buttons); |
| 70 | + public void execute(CommandEvent e) { |
| 71 | + String[] args = e.getArgs().split(" "); |
| 72 | + |
| 73 | + // If no command specified, show all. |
| 74 | + if(args[0].isEmpty()) { |
| 75 | + MessageAction reply = e.getChannel().sendMessageEmbeds(getHelpStartingAt(0).build()); |
| 76 | + Component[] buttons = createScrollButtons(0); |
| 77 | + if (buttons.length > 0) |
| 78 | + reply.setActionRow(buttons); |
| 79 | + |
| 80 | + reply.queue(); |
| 81 | + } else { |
| 82 | + // if there's potentially a command.. |
| 83 | + String commandNameStr = args[0]; |
| 84 | + // Get the command from the CommandClient. |
| 85 | + Command command = CommandModule.getCommandClient().getCommands().stream() |
| 86 | + .filter(com -> com.getName().equals(commandNameStr)) // Find the command with the matching name |
| 87 | + .findFirst() // Get the first (should be only) entry |
| 88 | + .orElseGet(CmdHelp::new); // And return it as Command. |
65 | 89 |
|
66 | | - reply.queue(); |
| 90 | + // Build the embed that summarises the command. |
| 91 | + EmbedBuilder embed = new EmbedBuilder(); |
| 92 | + embed.setAuthor(References.NAME, References.ISSUE_TRACKER, MMDBot.getInstance().getSelfUser().getAvatarUrl()); |
| 93 | + embed.setDescription("Command help:"); |
| 94 | + |
| 95 | + embed.addField(command.getName(), command.getHelp(), false); |
| 96 | + |
| 97 | + // If we have arguments defined and there's content, add it to the embed |
| 98 | + if(command.getArguments() != null && command.getArguments().length() > 0) |
| 99 | + embed.addField("Arguments", command.getArguments(), false); |
| 100 | + |
| 101 | + embed.setFooter(References.NAME).setTimestamp(Instant.now()); |
| 102 | + |
| 103 | + e.getChannel().sendMessageEmbeds(embed.build()).queue(); |
| 104 | + } |
67 | 105 | } |
68 | 106 |
|
69 | 107 | /** |
|
0 commit comments