package co.phoenixlab.discord;

import org.slf4j.Logger;
import sx.blah.discord.DiscordClient;
import sx.blah.discord.handle.obj.Message;

import java.util.HashMap;
import java.util.Map;

public class CommandDispatcher {

    private static final Logger LOGGER = VahrhedralBot.LOGGER;

    private final VahrhedralBot bot;
    private final DiscordClient discord;

    private final Map<String, CommandWrapper> commands;

    public CommandDispatcher(DiscordClient discord, VahrhedralBot bot) {
        this.discord = discord;
        this.bot = bot;
        commands = new HashMap<>();
    }

    public void registerCommand(String commandName, Command command,  String desc) {
        commands.put(commandName, new CommandWrapper(command, desc));
        LOGGER.debug("Registered command \"{}\"", commandName);
    }

    public void handleCommand(Message msg) {
        String content = msg.getContent();
        LOGGER.info("Received command {} from {} ({})",
                content, msg.getAuthor().getName(), msg.getAuthor().getID());
        //  Remove prefix
        String noPrefix = content.substring(bot.getConfig().getPrefixLength());
        //  Split
        String[] split = noPrefix.split(" ", 2);
        if (split.length == 0) {
            //  Invalid command
            LOGGER.info("Invalid command ignored: {}", content);
        }
        String cmd = split[0];
        String args = split.length > 1 ? split[1] : "";
        CommandWrapper wrapper = commands.get(cmd);
        if (wrapper != null) {
            wrapper.command.handleCommand(new MessageContext(discord, msg, bot), args);
        } else {
            LOGGER.info("Unknown command \"{}\"", cmd);
        }
    }

}

class CommandWrapper {
    final Command command;
    final String helpDesc;

    public CommandWrapper(Command command, String helpDesc) {
        this.command = command;
        this.helpDesc = helpDesc;
    }
}

@@ -130,16 +130,60 @@
 Message;
+%0Aimport sx.blah.discord.util.MessageBuilder;
 %0A%0Aimport
@@ -223,16 +223,47 @@
 til.Map;
+%0Aimport java.util.StringJoiner;
 %0A%0Apublic
@@ -651,24 +651,947 @@
 ashMap%3C%3E();%0A
+        addHelpCommand();%0A    %7D%0A%0A    private void addHelpCommand() %7B%0A        Command helpCommand = (context, args) -%3E %7B%0A            String prefix = bot.getConfig().getCommandPrefix();%0A            StringJoiner joiner = new StringJoiner(%22%5Cr%5Cn%22, %22Available Commands%5Cr%5Cn%22, %22%22);%0A            for (Map.Entry%3CString, CommandWrapper%3E entry : commands.entrySet()) %7B%0A                joiner.add(String.format(%22%25s%25s - %25s%22, prefix, entry.getKey(), entry.getValue().helpDesc));%0A            %7D%0A            final String result = joiner.toString();%0A            final String channelId = context.getMessage().getChannel().getID();%0A            bot.getTaskQueue().runOnMain(() -%3E %7B%0A                new MessageBuilder().%0A                        withChannel(channelId).%0A                        withContent(result).%0A                        send();%0A            %7D);%0A        %7D;%0A        registerCommand(%22help%22, helpCommand, %22Lists available commands%22);%0A
     %7D%0A%0A    p
@@ -1653,17 +1653,16 @@
 command,
- 
  String 
@@ -2511,24 +2511,80 @@
  != null) %7B%0A
+            LOGGER.info(%22Dispatching command %7B%7D%22, cmd);%0A
             

Add Help command