Skip to content

Commit 83c4dc0

Browse files
committed
Add help command
1 parent e25f219 commit 83c4dc0

13 files changed

+100
-15
lines changed

src/main/java/org/javacord/bot/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public final class Constants {
1212
*/
1313
public static final Color JAVACORD_ORANGE = new Color(243, 189, 30);
1414

15+
/**
16+
* The color used for error messages.
17+
*/
18+
public static final Color ERROR_COLOR = Color.RED;
19+
1520
/**
1621
* The ID of the "Discord API" server.
1722
*/

src/main/java/org/javacord/bot/Main.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.javacord.bot.commands.ExampleCommand;
1010
import org.javacord.bot.commands.GitHubCommand;
1111
import org.javacord.bot.commands.GradleCommand;
12+
import org.javacord.bot.commands.HelpCommand;
1213
import org.javacord.bot.commands.InfoCommand;
1314
import org.javacord.bot.commands.InviteCommand;
1415
import org.javacord.bot.commands.MavenCommand;
@@ -66,16 +67,17 @@ public static void main(String[] args) throws IOException {
6667

6768
// Register commands
6869
CommandHandler handler = new JavacordHandler(api);
70+
handler.registerCommand(new InfoCommand());
71+
handler.registerCommand(new WikiCommand());
6972
handler.registerCommand(new DocsCommand());
70-
handler.registerCommand(new ExampleCommand());
7173
handler.registerCommand(new GitHubCommand());
74+
handler.registerCommand(new ExampleCommand());
75+
handler.registerCommand(new SetupCommand(versionFinder));
7276
handler.registerCommand(new GradleCommand(versionFinder));
73-
handler.registerCommand(new InviteCommand());
7477
handler.registerCommand(new MavenCommand(versionFinder));
75-
handler.registerCommand(new SetupCommand(versionFinder));
76-
handler.registerCommand(new WikiCommand());
78+
handler.registerCommand(new InviteCommand());
7779
handler.registerCommand(new Sdcf4jCommand());
78-
handler.registerCommand(new InfoCommand());
80+
handler.registerCommand(new HelpCommand(handler));
7981

8082
api.addMessageDeleteListener(new CommandCleanupListener());
8183
}

src/main/java/org/javacord/bot/commands/DocsCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public class DocsCommand implements CommandExecutor {
5656
* @param args The arguments given to the command.
5757
* @throws IOException If the Javacord icon stream cannot be closed properly.
5858
*/
59-
@Command(aliases = {"!docs"}, async = true)
59+
@Command(aliases = {"!docs"}, async = true, usage = "!docs [method|class] <search>",
60+
description = "Shows a link to the JavaDoc or searches through it")
6061
public void onCommand(Server server, TextChannel channel, Message message, String[] args) throws IOException {
6162
// Only react in #java_javacord channel on Discord API server
6263
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {

src/main/java/org/javacord/bot/commands/ExampleCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ExampleCommand implements CommandExecutor {
2525
* @param message The message the command was issued in.
2626
* @throws IOException If the Javacord icon stream cannot be closed properly.
2727
*/
28-
@Command(aliases = {"!example"}, async = true)
28+
@Command(aliases = {"!example"}, async = true, description = "Shows a link to the example bot")
2929
public void onCommand(Server server, TextChannel channel, Message message) throws IOException {
3030
// Only react in #java_javacord channel on Discord API server
3131
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {

src/main/java/org/javacord/bot/commands/GitHubCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class GitHubCommand implements CommandExecutor {
2525
* @param message The message the command was issued in.
2626
* @throws IOException If the Javacord icon stream cannot be closed properly.
2727
*/
28-
@Command(aliases = {"!github"}, async = true)
28+
@Command(aliases = {"!github"}, async = true, description = "Shows links to the most important GitHub pages")
2929
public void onCommand(Server server, TextChannel channel, Message message) throws IOException {
3030
// Only react in #java_javacord channel on Discord API server
3131
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {

src/main/java/org/javacord/bot/commands/GradleCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public GradleCommand(LatestVersionFinder versionFinder) {
3232
* @param channel The channel where the command was issued.
3333
* @param message The message the command was issued in.
3434
*/
35-
@Command(aliases = {"!gradle"}, async = true)
35+
@Command(aliases = {"!gradle"}, async = true, description = "Shows the Gradle dependency")
3636
public void onCommand(Server server, TextChannel channel, Message message) {
3737
// Only react in #java_javacord channel on Discord API server
3838
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.javacord.bot.commands;
2+
3+
import de.btobastian.sdcf4j.Command;
4+
import de.btobastian.sdcf4j.CommandExecutor;
5+
import de.btobastian.sdcf4j.CommandHandler;
6+
import org.javacord.api.DiscordApi;
7+
import org.javacord.api.entity.channel.TextChannel;
8+
import org.javacord.api.entity.message.Message;
9+
import org.javacord.api.entity.message.embed.EmbedBuilder;
10+
import org.javacord.api.entity.server.Server;
11+
import org.javacord.bot.Constants;
12+
import org.javacord.bot.listeners.CommandCleanupListener;
13+
14+
/**
15+
* The !help command which is used to list all commands.
16+
*/
17+
public class HelpCommand implements CommandExecutor {
18+
19+
private final CommandHandler commandHandler;
20+
21+
/**
22+
* Initializes the command.
23+
*
24+
* @param commandHandler The command handler used to extract command usages and descriptions.
25+
*/
26+
public HelpCommand(CommandHandler commandHandler) {
27+
this.commandHandler = commandHandler;
28+
}
29+
30+
/**
31+
* Executes the {@code !help} command.
32+
*
33+
* @param api The Discord api.
34+
* @param server The server where the command was issued.
35+
* @param channel The channel where the command was issued.
36+
* @param message The message triggering the command.
37+
* @param args The command's arguments.
38+
*/
39+
@Command(aliases = {"!help"}, async = true, description = "Shows the help page")
40+
public void onCommand(DiscordApi api, Server server, TextChannel channel, Message message, String[] args) {
41+
// Only react in #java_javacord channel on Discord API server
42+
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {
43+
return;
44+
}
45+
46+
if (args.length >= 1) { // TODO Provide help for specific commands (e.g., "!help wiki")
47+
EmbedBuilder embed = new EmbedBuilder()
48+
.setTitle("Error")
49+
.setDescription("The `!help` command does not accept arguments!")
50+
.setColor(Constants.ERROR_COLOR);
51+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
52+
channel.sendMessage(embed).join();
53+
return;
54+
}
55+
56+
EmbedBuilder embed = new EmbedBuilder()
57+
.setThumbnail(getClass().getClassLoader().getResourceAsStream("javacord3_icon.png"), "png")
58+
.setTitle("Commands")
59+
.setColor(Constants.JAVACORD_ORANGE);
60+
61+
for (CommandHandler.SimpleCommand simpleCommand : commandHandler.getCommands()) {
62+
if (!simpleCommand.getCommandAnnotation().showInHelpPage()) {
63+
continue; // skip command
64+
}
65+
String usage = simpleCommand.getCommandAnnotation().usage();
66+
if (usage.isEmpty()) { // no usage provided, using the first alias
67+
usage = simpleCommand.getCommandAnnotation().aliases()[0];
68+
}
69+
String description = simpleCommand.getCommandAnnotation().description();
70+
embed.addField("**__" + usage + "__**", description);
71+
}
72+
73+
CommandCleanupListener.insertResponseTracker(embed, message.getId());
74+
channel.sendMessage(embed).join();
75+
}
76+
}

src/main/java/org/javacord/bot/commands/InfoCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class InfoCommand implements CommandExecutor {
2020
* @param channel The channel where the command was issued.
2121
* @param message The message the command was issued in.
2222
*/
23-
@Command(aliases = "!info", async = true)
23+
@Command(aliases = "!info", async = true, description = "Shows information about this bot")
2424
public void handleCommand(Server server, TextChannel channel, Message message) {
2525
// Only react in #java_javacord channel on Discord API server
2626
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {

src/main/java/org/javacord/bot/commands/InviteCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class InviteCommand implements CommandExecutor {
2525
* @param message The message the command was issued in.
2626
* @throws IOException If the Javacord icon stream cannot be closed properly.
2727
*/
28-
@Command(aliases = {"!invite"}, async = true)
28+
@Command(aliases = {"!invite"}, async = true, description = "Shows the invite link to the Javacord server")
2929
public void onCommand(Server server, TextChannel channel, Message message) throws IOException {
3030
// Only react in #java_javacord channel on Discord API server
3131
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {

src/main/java/org/javacord/bot/commands/MavenCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public MavenCommand(LatestVersionFinder versionFinder) {
3232
* @param channel The channel where the command was issued.
3333
* @param message The message the command was issued in.
3434
*/
35-
@Command(aliases = {"!maven"}, async = true)
35+
@Command(aliases = {"!maven"}, async = true, description = "Shows the Maven dependency")
3636
public void onCommand(Server server, TextChannel channel, Message message) {
3737
// Only react in #java_javacord channel on Discord API server
3838
if ((server.getId() == Constants.DAPI_SERVER_ID) && (channel.getId() != Constants.DAPI_JAVACORD_CHANNEL_ID)) {

0 commit comments

Comments
 (0)