Skip to content

Commit cbe56db

Browse files
committed
Introduced abstract Command class for commands.
1 parent cee1bd4 commit cbe56db

File tree

4 files changed

+159
-6
lines changed

4 files changed

+159
-6
lines changed

src/bitbot/BitBot.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.File;
44
import java.io.IOException;
55

6+
import bitbot.commands.GreetCommand;
7+
68

79
public class BitBot {
810

@@ -23,6 +25,7 @@ public static void main(String[] args) {
2325
public BitBot(BotConfig config) {
2426
this.config = config;
2527
MessageHandler messageHandler = new MessageHandler(config.getCommandPrefix());
28+
messageHandler.addCommand(new GreetCommand());
2629
server = new ChatServer(config.getChatServerConfig(), messageHandler);
2730
}
2831

src/bitbot/Command.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package bitbot;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
6+
7+
public abstract class Command {
8+
9+
private final String command;
10+
private final String[] aliases;
11+
12+
public Command(String command, String ...aliases) {
13+
this.command = command;
14+
this.aliases = aliases;
15+
}
16+
17+
public String getCommand() {
18+
return command;
19+
}
20+
21+
public Collection<String> getAliases() {
22+
return Arrays.asList(aliases);
23+
}
24+
25+
public abstract String getDescription();
26+
27+
public abstract String getArgumentTemplate();
28+
29+
public abstract String getHelpText();
30+
31+
public abstract void execute(String command, String args, User user, boolean pm, Replier replier);
32+
33+
}

src/bitbot/MessageHandler.java

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
package bitbot;
22

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+
39

410

511
public class MessageHandler {
612

713
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+
});
821

922
public MessageHandler(char commandPrefix) {
1023
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+
}
1133
}
1234

1335
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)
1638
text = text.substring(1);
1739
}
1840
String[] parts = text.split("\\s", 2);
19-
String cmd = parts[0];
41+
String cmd = parts[0].toLowerCase();
2042
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');
2669
}
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("\nAlias: ").append(cmd.getAliases().toString());
90+
}
91+
replier.replyPrivately(sb.toString());
92+
} else {
93+
replier.replyPrivately("Jag känner inte till kommandot '" + args + "'");
2794
}
2895
}
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+
29113
}
30114

31115
}

src/bitbot/commands/GreetCommand.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package bitbot.commands;
2+
3+
import bitbot.Command;
4+
import bitbot.Replier;
5+
import bitbot.User;
6+
7+
8+
public class GreetCommand extends Command {
9+
10+
public GreetCommand() {
11+
super("hej", "tja", "hallå", "hi", "hello");
12+
}
13+
14+
@Override
15+
public void execute(String command, String args, User user, boolean pm, Replier replier) {
16+
replier.reply("Hej, " + user.getNick());
17+
}
18+
19+
@Override
20+
public String getArgumentTemplate() {
21+
return "";
22+
}
23+
24+
@Override
25+
public String getDescription() {
26+
return "Hej, hej!";
27+
}
28+
29+
@Override
30+
public String getHelpText() {
31+
return "Om ingen annan säger hej så är i alla fall jag trevlig =)";
32+
}
33+
}

0 commit comments

Comments
 (0)