Skip to content
This repository was archived by the owner on Jan 19, 2018. It is now read-only.

Commit 0b7cdfe

Browse files
webpigeonSuborbitalPigeon
authored andcommitted
documented command processor
1 parent a734869 commit 0b7cdfe

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

src/main/java/uk/co/unitycoders/pircbotx/commandprocessor/Command.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,33 @@
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
2525

26+
/**
27+
* Command annotation.
28+
*
29+
* This annotation is used to tag a method as being a command the bot should
30+
* recognise and call when certian words or phrases are mentioned in a channel.
31+
*
32+
* The method must take exactly 1 argument, of type MessageEvent<PircBotX>, this
33+
* will be passed directly from pircbotx and so will have all features which the
34+
* framework provides to it.
35+
*
36+
* @author webpigeon
37+
*
38+
*/
2639
@Retention(RetentionPolicy.RUNTIME)
2740
@Target({ ElementType.METHOD })
2841
public @interface Command
2942
{
43+
44+
/**
45+
* Command trigger words.
46+
*
47+
* This is used to tell the bot what keywords it should respond to when
48+
* processing actions. The keyword "default" will be called automatically if
49+
* no keyword is specified. If an array of keywords is passed the bot will
50+
* register all keywords for this method.
51+
*
52+
* @return The keywords for this command
53+
*/
3054
public String[] value() default "default";
3155
}

src/main/java/uk/co/unitycoders/pircbotx/commandprocessor/CommandListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import org.pircbotx.hooks.events.MessageEvent;
2424

2525
/**
26-
*
26+
* Command Processor wrapper class.
27+
*
28+
* This class is notified by pircbotx when the bot gets a message. It's sole
29+
* purpose is to act as an adapter between the command processor and pircbotx.
2730
*/
2831
public class CommandListener extends ListenerAdapter<PircBotX>
2932
{

src/main/java/uk/co/unitycoders/pircbotx/commandprocessor/CommandProcessor.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,47 @@
3232
/**
3333
* centrally managed command parsing.
3434
*
35+
* This class is responsible to breaking IRC messages into commands which the
36+
* bot can understand. It contains a list of modules and the methods which are
37+
* tagged with the command annotation. Classes must be registered with the
38+
* Command Processor in order for the processor to recognise them as commands.
39+
*
3540
*/
3641
public class CommandProcessor
3742
{
3843
private final Pattern regex;
3944
private final Map<String, Object> commands;
4045
private final Map<String, Map<String, Method>> callbacks;
4146

47+
/**
48+
* Create a new command processor.
49+
*
50+
* This will create a new command processor and will initialise the regex
51+
* pattern the bot will use to match commands. It will also create the maps
52+
* needed to store information about the commands.
53+
*
54+
* @param trigger the first character of any line directed at the bot
55+
*/
4256
public CommandProcessor(char trigger)
4357
{
4458
this.regex = Pattern.compile(trigger + "([a-z0-9]+)(?: ([a-z0-9]+))?(?: (.*))?");
4559
this.commands = new HashMap<String, Object>();
4660
this.callbacks = new HashMap<String, Map<String, Method>>();
4761
}
4862

63+
/**
64+
* Register a new module and extract it's commands.
65+
*
66+
* This method will look at target and process any method which has been
67+
* annotated with the command annotation. It will then remember the module
68+
* and command names for use when processing messages.
69+
*
70+
* The module's name will need to be put before any command. This is to
71+
* prevent two modules conflicting with the same named commands.
72+
*
73+
* @param name the name of the module
74+
* @param target the module object
75+
*/
4976
public void register(String name, Object target)
5077
{
5178
Map<String, Method> methods = new HashMap<String, Method>();
@@ -69,6 +96,16 @@ public void register(String name, Object target)
6996
callbacks.put(name, methods);
7097
}
7198

99+
/**
100+
* Process an IRC message to see if the bot needs to respond.
101+
*
102+
* This method takes an IRC message and splits it into it's component parts.
103+
* if the action is valid it will then call the call method to process the
104+
* event.
105+
*
106+
* @param event the event to be processed
107+
* @throws Exception
108+
*/
72109
public void invoke(MessageEvent<PircBotX> event) throws Exception
73110
{
74111
Matcher matcher = regex.matcher(event.getMessage());
@@ -108,6 +145,15 @@ public void invoke(MessageEvent<PircBotX> event) throws Exception
108145
}
109146
}
110147

148+
/**
149+
* Call a module's command with required arguments.
150+
*
151+
* @param type the name of the module
152+
* @param cmd the name of the command
153+
* @param args the arguments to pass to the method associated with command
154+
* @return true if method was called, false if not
155+
* @throws Exception if the method throws an exception.
156+
*/
111157
private boolean call(String type, String cmd, Object... args) throws Exception
112158
{
113159
Object obj = commands.get(type);
@@ -126,13 +172,24 @@ private boolean call(String type, String cmd, Object... args) throws Exception
126172
return true;
127173
}
128174

175+
/**
176+
* Get a list of modules registered with the command processor.
177+
*
178+
* @return the list of module names
179+
*/
129180
public String[] getModules()
130181
{
131182
Collection<String> modules = callbacks.keySet();
132183
String[] moduleArray = new String[modules.size()];
133184
return modules.toArray(moduleArray);
134185
}
135186

187+
/**
188+
* Gets a list of commands which are registered with the command processor
189+
*
190+
* @param moduleName the name of the module to get the commands from.
191+
* @return the list of command names, or null if command doesn't exist.
192+
*/
136193
public String[] getCommands(String moduleName)
137194
{
138195
Map<String, Method> commands = callbacks.get(moduleName);

0 commit comments

Comments
 (0)