Added code to actually run commands

Also made it not respond to bots...
This commit is contained in:
Norbi Peti 2016-10-29 23:56:30 +02:00
parent 01ee8d7e94
commit c6b831e035
2 changed files with 29 additions and 6 deletions

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import sx.blah.discord.api.events.IListener; import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.MentionEvent; import sx.blah.discord.handle.impl.events.MentionEvent;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent; import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
@ -12,6 +13,8 @@ public class CommandListener {
return new IListener[] { new IListener<MentionEvent>() { return new IListener[] { new IListener<MentionEvent>() {
@Override @Override
public void handle(MentionEvent event) { public void handle(MentionEvent event) {
if (event.getMessage().getAuthor().isBot())
return;
final IChannel channel = event.getMessage().getChannel(); final IChannel channel = event.getMessage().getChannel();
if (!channel.getID().equals(DiscordPlugin.botchannel.getID()) && !channel.isPrivate()) if (!channel.getID().equals(DiscordPlugin.botchannel.getID()) && !channel.isPrivate())
return; return;
@ -22,12 +25,28 @@ public class CommandListener {
public void handle(MessageReceivedEvent event) { public void handle(MessageReceivedEvent event) {
if (!event.getMessage().getChannel().isPrivate()) if (!event.getMessage().getChannel().isPrivate())
return; return;
if (event.getMessage().getAuthor().isBot())
return;
runCommand(event.getMessage()); runCommand(event.getMessage());
} }
} }; } };
} }
private static void runCommand(IMessage message) { private static void runCommand(IMessage message) {
System.out.println(message.getContent()); // TODO: Resolve mentions String cmdwithargs = message.getContent();
final String mention = DiscordPlugin.dc.getOurUser().mention();
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
cmdwithargs = cmdwithargs.substring(mention.length());
int index = cmdwithargs.indexOf(' ');
String cmd;
String args;
if (index == -1) {
cmd = cmdwithargs;
args = "";
} else {
cmd = cmdwithargs.substring(0, index);
args = cmdwithargs.substring(index);
}
DiscordCommandBase.runCommand(cmd, args, message);
} }
} }

View file

@ -2,6 +2,7 @@ package buttondevteam.discordplugin.commands;
import java.util.HashMap; import java.util.HashMap;
import buttondevteam.discordplugin.DiscordPlugin;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
public abstract class DiscordCommandBase { public abstract class DiscordCommandBase {
@ -11,14 +12,17 @@ public abstract class DiscordCommandBase {
private static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>(); private static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
protected void respond(IMessage message, String messagetosend) {
}
static { static {
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands? commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
} }
public static final void runCommand(IMessage message) { public static void runCommand(String cmd, String args, IMessage message) {
DiscordCommandBase command = commands.get(cmd);
if (command == null) {
// TODO: Help command
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Unknown command: " + cmd + " with args: " + args);
return;
}
command.run(message, args);
} }
} }