Question

Yes, this question is for a bot, but it requires little knowledge of bots. So please, don't be put off by that.

This question may seem similar to other PircBot questions, but bear with me, it isn't.
So, I've recently been working on an IRC bot based off of PircBot, and I've come upon a snag. I have a rapidly growing command list, and currently I'm using a horribly inefficient and very ugly block of these:

if(message.equalsIgnoreCase("!thatcommand"))
            processMessage(channel,"returnforthatcommand"+sender);

I have a huge block of these where it just checks the message time after time for if it matches. That's my main problem. The process message method just sends the message after checking if I'm running the logger (printing the message to the console).
What I really want to do is to streamline it. I want to make some sort of chart, like a HashMap, that will process the strings accordingly. I've also considered making a Command class that stores the response and a boolean that tells whether or not a chatter has to be "opped" to use it. I've tried making files and started making a few attempts at HashMaps, but I could really use some direction on the simplest and/or most efficient way to go about this. I do have a few different "modes" for the bot, so making separate HashMaps for different privilege levels may be a good option.

One last thing: I later want to put the bot into a Jar file with a GUI for outside running (Eclipse takes ages to start up on this laptop), and I want to be able to make permanent changes to the command list through the bot itself as opposed to recoding it... That's why I was considering trying to reference a file, but then I had tons of problems with the program not being able to find said file.

Any suggestions?

Alright, for the people that don't like reading long things: I have sets of data that are called by a trigger String (message) and return a certain response String. How can I store these and call them in the simplest fashion, and how can I make it so that I can write things onto the list on the fly?

Was it helpful?

Solution

You could the use the Abstract Factory design pattern. It would be something like this:

public abstract class CommandFactory {
    private static Map<String,Command> commands;

    public static Command getInstance(String commandName) {
        if (commands == null) initialize();
        return commands.get(commandName);
    }

    private static void initialize() {
        commands.put("commandA", new CommandA());
        commands.put("commandB", new CommandB());
    }

}

And write CommandFactory.getInstance("commandString") in the place of your if clauses.

OTHER TIPS

Suggestion: Hashtable of objects implementing an interface which includes a method such as execute(). Use the hashtable to go from command to object, call its execute, done.

Yes, you'll need to initialize that table as your server starts up. Whether you do that from literal strings or have each actor announce its own keyword is up to you. The latter's probably more elegant but doesn't permit aliases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top