1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-09-19 21:04:02 +00:00

Add commands listing command (!command)

This commit is contained in:
sciwhiz12 2020-11-28 07:52:33 +08:00
parent 199b40b160
commit d214af3eca
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
4 changed files with 58 additions and 3 deletions

View File

@ -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<MessageReceivedEvent> getNode() {
return literal("commands")
.executes(ctx -> {
messages().<String>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();
}
}

View File

@ -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<String, Command> registry = new HashMap<>();
final Map<String, Command> registry = new HashMap<>();
private final CommandDispatcher<MessageReceivedEvent> 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<CommandRegistry, Command> command) {
dispatcher.register(command.apply(this).getNode());
public void addCommand(Function<CommandRegistry, Command> commandCreate) {
final Command command = commandCreate.apply(this);
final LiteralArgumentBuilder<MessageReceivedEvent> 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

View File

@ -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."
}

View File

@ -1,4 +1,5 @@
[
"general/commands_listing",
"general/error/ambiguous_member",
"general/error/guild_only_command",
"general/error/insufficient_permissions",