1
1
package bitbot ;
2
2
3
+ import java .util .Comparator ;
4
+ import java .util .HashMap ;
5
+ import java .util .Map ;
6
+ import java .util .SortedSet ;
7
+ import java .util .TreeSet ;
8
+
3
9
4
10
5
11
public class MessageHandler {
6
12
7
13
private final char commandPrefix ;
14
+ private final Map <String , Command > commandIndex = new HashMap <String , Command >();
15
+ private final SortedSet <Command > commands = new TreeSet <Command >(new Comparator <Command >() {
16
+ @ Override
17
+ public int compare (Command o1 , Command o2 ) {
18
+ return o1 .getCommand ().compareTo (o2 .getCommand ());
19
+ }
20
+ });
8
21
9
22
public MessageHandler (char commandPrefix ) {
10
23
this .commandPrefix = commandPrefix ;
24
+ addCommand (new HelpCommand ());
25
+ }
26
+
27
+ public void addCommand (Command command ) {
28
+ commands .add (command );
29
+ commandIndex .put (command .getCommand (), command );
30
+ for (String alias : command .getAliases ()) {
31
+ commandIndex .put (alias , command );
32
+ }
11
33
}
12
34
13
35
public void processMessage (String text , User user , boolean pm , Replier replier ) {
@@ -16,16 +38,78 @@ public void processMessage(String text, User user, boolean pm, Replier replier)
16
38
text = text .substring (1 );
17
39
}
18
40
String [] parts = text .split ("\\ s" , 2 );
19
- String cmd = parts [0 ];
41
+ String cmd = parts [0 ]. toLowerCase () ;
20
42
String args = parts .length == 1 ? "" : parts [1 ];
21
- if (cmd .equalsIgnoreCase ("hej" )) {
22
- replier .reply ("Hej, " + user .getNick ());
23
- } else if (cmd .equalsIgnoreCase ("test" )) {
24
- for (String s : args .split ("\\ s" )) {
25
- replier .replyPrivately (s );
43
+ Command command = commandIndex .get (cmd );
44
+ if (command != null ) {
45
+ command .execute (cmd , args , user , pm , replier );
46
+ } else {
47
+ replier .replyPrivately ("Jag känner inte till kommandot '" + cmd + "'" );
48
+ }
49
+ }
50
+ }
51
+
52
+
53
+ private class HelpCommand extends Command {
54
+
55
+ public HelpCommand () {
56
+ super ("hjälp" , "help" );
57
+ }
58
+
59
+ @ Override
60
+ public void execute (String command , String args , User user , boolean pm , Replier replier ) {
61
+ if (args .isEmpty ()) {
62
+ StringBuilder sb = new StringBuilder ();
63
+ sb .append ("Jag förstår följande kommandon:\n " );
64
+ for (Command cmd : commands ) {
65
+ if (!pm ) {
66
+ sb .append (commandPrefix );
67
+ }
68
+ sb .append (cmd .getCommand ()).append ('\t' ).append (cmd .getDescription ()).append ('\n' );
26
69
}
70
+ sb .append ("För specifik hjälp skriv: " );
71
+ if (!pm ) {
72
+ sb .append (commandPrefix );
73
+ }
74
+ sb .append (getCommand ()).append (" <kommando>'" );
75
+ replier .replyPrivately (sb .toString ());
76
+ } else if (commandIndex .containsKey (args )) {
77
+ Command cmd = commandIndex .get (args );
78
+ StringBuilder sb = new StringBuilder ();
79
+ if (!pm ) {
80
+ sb .append (commandPrefix );
81
+ }
82
+ sb .append (cmd .getCommand ());
83
+ if (!cmd .getArgumentTemplate ().isEmpty ()) {
84
+ sb .append (' ' ).append (cmd .getArgumentTemplate ());
85
+ }
86
+ sb .append ('\n' );
87
+ sb .append (cmd .getHelpText ());
88
+ if (!cmd .getAliases ().isEmpty ()) {
89
+ sb .append ("\n Alias: " ).append (cmd .getAliases ().toString ());
90
+ }
91
+ replier .replyPrivately (sb .toString ());
92
+ } else {
93
+ replier .replyPrivately ("Jag känner inte till kommandot '" + args + "'" );
27
94
}
28
95
}
96
+
97
+ @ Override
98
+ public String getDescription () {
99
+ return "Hjälp för kommandon" ;
100
+ }
101
+
102
+ @ Override
103
+ public String getArgumentTemplate () {
104
+ return "[kommando]" ;
105
+ }
106
+
107
+ @ Override
108
+ public String getHelpText () {
109
+ return ("Utan argument visas listan med kommandon.\n " +
110
+ "Med ett kommando som argument visas specifik hjälp." );
111
+ }
112
+
29
113
}
30
114
31
115
}
0 commit comments