Merge remote-tracking branch 'refs/remotes/origin/master' into Ali

# Conflicts:
#	src/main/java/buttondevteam/discordplugin/DiscordPlugin.java
This commit is contained in:
alisolarflare 2016-11-21 21:20:53 -05:00
commit f7c6614c2a
10 changed files with 113 additions and 18 deletions

View file

@ -14,6 +14,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import buttondevteam.discordplugin.listeners.ChatListener;
import buttondevteam.discordplugin.listeners.CommandListener;
import buttondevteam.discordplugin.listeners.ExceptionListener;
import buttondevteam.discordplugin.listeners.MCListener;
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
@ -57,6 +61,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
dc.getDispatcher().registerListener(this);
for (IListener<?> listener : CommandListener.getListeners())
dc.getDispatcher().registerListener(listener);
dc.getDispatcher().registerListener(new ChatListener());
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
@ -71,6 +76,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public static IChannel genchannel;
public static IChannel issuechannel;
public static IChannel debugchannel;
public static IChannel chatchannel;
public static boolean Test = true;
@ -90,6 +96,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
genchannel = mainServer.getChannelByID("125813020357165056"); // general
issuechannel = devServer.getChannelByID("219643416496046081"); // server-issues
debugchannel = devServer.getChannelByID("250332016199860224"); // debug-channel
chatchannel = mainServer.getChannelByID("249663564057411596"); // minecraft_chat
dc.changeStatus(Status.game("on TBMC"));
} else {
botchannel = devServer.getChannelByID("239519012529111040"); // bottest
@ -97,6 +104,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
genchannel = devServer.getChannelByID("239519012529111040"); // bottest
issuechannel = devServer.getChannelByID("239519012529111040"); // bottest
debugchannel = devServer.getChannelByID("239519012529111040"); //bottest
chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test
dc.changeStatus(Status.game("testing"));
}
// sendMessageToChannel(botchannel, "Minecraft server started up");

View file

@ -65,4 +65,13 @@ public class ConnectCommand extends DiscordCommandBase {
+ message.getAuthor().getDiscriminator() + " do /discord accept");
}
@Override
public String[] getHelpText() {
return new String[] { //
"---- Connect command ----", //
"This commands let's you connect your acoount with a Minecraft account. This'd allow using the Minecraft chat and other things.", //
"Usage: connect <Minecraftname>" //
};
}
}

View file

@ -11,18 +11,21 @@ public abstract class DiscordCommandBase {
public abstract void run(IMessage message, String args);
public abstract String[] getHelpText();
static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
static {
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
commands.put("userinfo", new UserinfoCommand());
commands.put("help", new HelpCommand());
}
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);
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Unknown command: " + cmd + " with args: " + args + "\nDo @ChromaBot help for help");
return;
}
try {

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.commands;
import java.util.Arrays;
import java.util.stream.Collectors;
import buttondevteam.discordplugin.DiscordPlugin;
@ -14,8 +15,24 @@ public class HelpCommand extends DiscordCommandBase {
@Override
public void run(IMessage message, String args) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Available commands:\n" + DiscordCommandBase.commands
.values().stream().map(dc -> dc.getCommandName()).collect(Collectors.joining("\n")));
DiscordCommandBase argdc;
if (args.length() == 0)
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Available commands:\n" + DiscordCommandBase.commands.values().stream()
.map(dc -> dc.getCommandName()).collect(Collectors.joining("\n")));
else
DiscordPlugin.sendMessageToChannel(message.getChannel(),
(argdc = DiscordCommandBase.commands.get(args)) == null ? "Command not found: " + args
: Arrays.stream(argdc.getHelpText()).collect(Collectors.joining("\n")));
}
@Override
public String[] getHelpText() {
return new String[] { //
"---- Help command ----", //
"Shows some info about a command or lists the available commands.", //
"Usage: help [command]"//
};
}
}

View file

@ -31,7 +31,7 @@ public class UserinfoCommand extends DiscordCommandBase {
target = message.getMentions().get(0);
else if (args.contains("#")) {
String[] targettag = args.split("#");
final List<IUser> targets = message.getGuild().getUsersByName(targettag[0], true);
final List<IUser> targets = getUsers(message, targettag[0]);
if (targets.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The user cannot be found (by name): " + args);
return;
@ -48,12 +48,7 @@ public class UserinfoCommand extends DiscordCommandBase {
return;
}
} else {
final List<IUser> targets;
if (message.getChannel().isPrivate())
targets = DiscordPlugin.dc.getUsers().stream().filter(u -> u.getName().equalsIgnoreCase(args))
.collect(Collectors.toList());
else
targets = message.getGuild().getUsersByName(args, true);
final List<IUser> targets = getUsers(message, args);
if (targets.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The user cannot be found on Discord: " + args);
@ -82,4 +77,23 @@ public class UserinfoCommand extends DiscordCommandBase {
"The user is not found in our system (player has to be on the MC server for now)!");
}
private List<IUser> getUsers(IMessage message, String args) {
final List<IUser> targets;
if (message.getChannel().isPrivate())
targets = DiscordPlugin.dc.getUsers().stream().filter(u -> u.getName().equalsIgnoreCase(args))
.collect(Collectors.toList());
else
targets = message.getGuild().getUsersByName(args, true);
return targets;
}
@Override
public String[] getHelpText() {
return new String[] { //
"---- User information ----", //
"Shows some information about users, from Discord, from Minecraft or from Reddit if they have these accounts connected.", //
"Usage: userinfo <Discordname>" //
};
}
}

View file

@ -0,0 +1,19 @@
package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DiscordPlugin;
import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
public class ChatListener implements IListener<MessageReceivedEvent> {
@Override
public void handle(MessageReceivedEvent event) {
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID()))
return;
if (event.getMessage().getContent().startsWith("/"))
; // Call API method
else
;
}
}

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin;
package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import sx.blah.discord.api.events.IListener;
import sx.blah.discord.handle.impl.events.MentionEvent;
@ -33,13 +34,14 @@ public class CommandListener {
}
private static void runCommand(IMessage message) {
message.getChannel().setTypingStatus(true);
String cmdwithargs = message.getContent();
final String mention = DiscordPlugin.dc.getOurUser().mention(false);
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
if (message.getContent().startsWith(mention) && cmdwithargs.length() > mention.length() + 1) // TODO: Resolve mentions: Compound arguments, either a mention or text
cmdwithargs = cmdwithargs.substring(mention.length() + 1);
if (message.getContent().startsWith(mentionNick) && cmdwithargs.length() > mentionNick.length() + 1)
cmdwithargs = cmdwithargs.substring(mentionNick.length() + 1);
cmdwithargs = checkanddeletemention(cmdwithargs, mention, message);
cmdwithargs = checkanddeletemention(cmdwithargs, mentionNick, message);
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().map(r -> r.mention())::iterator)
cmdwithargs = checkanddeletemention(cmdwithargs, mentionRole, message);
int index = cmdwithargs.indexOf(' ');
String cmd;
String args;
@ -51,5 +53,18 @@ public class CommandListener {
args = cmdwithargs.substring(index + 1);
}
DiscordCommandBase.runCommand(cmd, args, message);
message.getChannel().setTypingStatus(false);
}
private static String checkanddeletemention(String cmdwithargs, String mention, IMessage message) {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1)
cmdwithargs = cmdwithargs.substring(
cmdwithargs.charAt(mention.length() + 1) == ' ' ? mention.length() + 1 : mention.length());
else
cmdwithargs = "help";
if (cmdwithargs.length() == 0)
cmdwithargs = "help";
return cmdwithargs;
}
}

View file

@ -1,9 +1,10 @@
package buttondevteam.discordplugin;
package buttondevteam.discordplugin.listeners;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCExceptionEvent;
public class ExceptionListener implements Listener {

View file

@ -0,0 +1,7 @@
package buttondevteam.discordplugin.listeners;
import org.bukkit.event.Listener;
public class MCChatListener implements Listener {
// Custom event
}

View file

@ -1,4 +1,4 @@
package buttondevteam.discordplugin;
package buttondevteam.discordplugin.listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -6,6 +6,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.lib.TBMCPlayerGetInfoEvent;
import buttondevteam.lib.TBMCPlayerJoinEvent;