Skip to content

Commit 1512361

Browse files
authored
Merge pull request #92 from TheCurle/3.0-help-again
Implement targetted help command.
2 parents 5868a22 + e0f7681 commit 1512361

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/main/java/com/mcmoddev/mmdbot/modules/commands/CommandModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public static void setupCommandModule() {
8989
.setOwnerId(MMDBot.getConfig().getOwnerID())
9090
.setPrefix(MMDBot.getConfig().getMainPrefix())
9191
.setAlternativePrefix(MMDBot.getConfig().getAlternativePrefix())
92-
.setHelpConsumer(CmdHelp::execute)
92+
.useHelpBuilder(false) // We want help to show as a command, so add it as one.
93+
.addCommand(new CmdHelp())
9394
.addCommand(new CmdGuild())
9495
.addCommand(new CmdAbout())
9596
.addCommand(new CmdMe())

src/main/java/com/mcmoddev/mmdbot/modules/commands/bot/info/CmdHelp.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,69 @@
3939
import java.util.List;
4040

4141
/**
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.
4443
*
45-
* This should not be turned into a normal Command.
44+
* Possible forms:
45+
* !help
46+
* !help help
47+
* !help addquote
48+
* !help [command]
4649
*
4750
* @author Curle
4851
*/
49-
public class CmdHelp {
52+
public class CmdHelp extends Command {
5053

5154
private static final int COMMANDS_PER_PAGE = 25;
5255

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+
5364
/**
5465
* Prepare the potential scrolling buttons for a help command,
5566
* and send the message with the proper embeds.
5667
*
5768
* See {@link #getHelpStartingAt(int)} for the implementation.
58-
* @param e
5969
*/
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.
6589

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+
}
67105
}
68106

69107
/**

0 commit comments

Comments
 (0)