From d214af3ecabda35c2ff0e6eb3c5787d3d1cddc46 Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Sat, 28 Nov 2020 07:52:33 +0800 Subject: [PATCH] Add commands listing command (!command) --- .../janitor/commands/CmdListCommand.java | 34 +++++++++++++++++++ .../janitor/commands/CommandRegistryImpl.java | 15 ++++++-- .../messages/general/commands_listing.json | 11 ++++++ .../src/main/resources/messages/messages.json | 1 + 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java create mode 100644 core/src/main/resources/messages/general/commands_listing.json diff --git a/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java b/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java new file mode 100644 index 0000000..76679a2 --- /dev/null +++ b/core/src/main/java/sciwhiz12/janitor/commands/CmdListCommand.java @@ -0,0 +1,34 @@ +package sciwhiz12.janitor.commands; + +import com.google.common.collect.Lists; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import sciwhiz12.janitor.api.command.BaseCommand; + +import static sciwhiz12.janitor.api.config.CoreConfigs.COMMAND_PREFIX; +import static sciwhiz12.janitor.api.utils.CommandHelper.literal; + +public class CmdListCommand extends BaseCommand { + public CmdListCommand(CommandRegistryImpl registry) { + super(registry); + } + + @Override + public LiteralArgumentBuilder getNode() { + return literal("commands") + .executes(ctx -> { + messages().getListingMessage("general/commands_listing") + .amountPerPage(12) + .with("commands_prefix", () -> config(ctx.getSource()).forGuild(COMMAND_PREFIX)) + .setEntryApplier((command, subs) -> subs.with("command", () -> command)) + .build(ctx.getSource().getChannel(), getBot(), ctx.getSource().getMessage(), + Lists.newArrayList(getRegistry().registry.keySet())); + return 1; + }); + } + + @Override + public CommandRegistryImpl getRegistry() { + return (CommandRegistryImpl) super.getRegistry(); + } +} diff --git a/core/src/main/java/sciwhiz12/janitor/commands/CommandRegistryImpl.java b/core/src/main/java/sciwhiz12/janitor/commands/CommandRegistryImpl.java index f55d3c2..aad3350 100644 --- a/core/src/main/java/sciwhiz12/janitor/commands/CommandRegistryImpl.java +++ b/core/src/main/java/sciwhiz12/janitor/commands/CommandRegistryImpl.java @@ -3,6 +3,7 @@ package sciwhiz12.janitor.commands; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; @@ -27,7 +28,7 @@ import static sciwhiz12.janitor.api.Logging.JANITOR; public class CommandRegistryImpl implements CommandRegistry, EventListener { private final JanitorBotImpl bot; - private final Map registry = new HashMap<>(); + final Map registry = new HashMap<>(); private final CommandDispatcher dispatcher; public CommandRegistryImpl(JanitorBotImpl bot) { @@ -39,6 +40,7 @@ public class CommandRegistryImpl implements CommandRegistry, EventListener { addCommand(OKCommand::new); addCommand(HelloCommand::new); addCommand(ShutdownCommand::new); + addCommand((reg) -> new CmdListCommand((CommandRegistryImpl) reg)); } @Override @@ -52,8 +54,15 @@ public class CommandRegistryImpl implements CommandRegistry, EventListener { } @Override - public void addCommand(Function command) { - dispatcher.register(command.apply(this).getNode()); + public void addCommand(Function commandCreate) { + final Command command = commandCreate.apply(this); + final LiteralArgumentBuilder node = command.getNode(); + final String literal = node.getLiteral(); + if (registry.containsKey(literal)) { + throw new IllegalArgumentException("Command " + literal + " already exists"); + } + dispatcher.register(node); + registry.put(literal, command); } @Override diff --git a/core/src/main/resources/messages/general/commands_listing.json b/core/src/main/resources/messages/general/commands_listing.json new file mode 100644 index 0000000..292b6b6 --- /dev/null +++ b/core/src/main/resources/messages/general/commands_listing.json @@ -0,0 +1,11 @@ +{ + "type": "listing", + "author": { + "name": "List of Commands (Page ${page.current}/${page.max})" + }, + "entry": { + "type": "description", + "text": "${commands_prefix}${command}" + }, + "empty": "_**No commands listed.** Please report this to your bot maintainer/administrator." +} \ No newline at end of file diff --git a/core/src/main/resources/messages/messages.json b/core/src/main/resources/messages/messages.json index d2f6d88..91a28fb 100644 --- a/core/src/main/resources/messages/messages.json +++ b/core/src/main/resources/messages/messages.json @@ -1,4 +1,5 @@ [ + "general/commands_listing", "general/error/ambiguous_member", "general/error/guild_only_command", "general/error/insufficient_permissions",