|
| 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 | +} |
0 commit comments