Added and fixed a bunch of things

- Added exception handler to Discord commands
- Added retry code for getting guilds/servers
- Added max message length for exception sending to Discord
- Made the connect message only appear in Minecraft when needed
- Fixed status formatting in userinfo
- Improved userinfo command
- Other fixes and improvements
This commit is contained in:
Norbi Peti 2016-11-02 22:53:55 +01:00
parent fb5dd60b84
commit b43b7c86ff
6 changed files with 67 additions and 17 deletions

View file

@ -74,8 +74,13 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
@Override @Override
public void handle(ReadyEvent event) { public void handle(ReadyEvent event) {
try { try {
final IGuild mainServer = event.getClient().getGuildByID("125813020357165056"); IGuild mainServer;
final IGuild devServer = event.getClient().getGuildByID("219529124321034241"); IGuild devServer;
do {
mainServer = event.getClient().getGuildByID("125813020357165056");
devServer = event.getClient().getGuildByID("219529124321034241");
Thread.sleep(100);
} while (mainServer == null || devServer == null);
if (!Test) { if (!Test) {
botchannel = mainServer.getChannelByID("209720707188260864"); // bot botchannel = mainServer.getChannelByID("209720707188260864"); // bot
annchannel = mainServer.getChannelByID("126795071927353344"); // announcements annchannel = mainServer.getChannelByID("126795071927353344"); // announcements

View file

@ -17,7 +17,10 @@ public class ExceptionListener implements Listener {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(sourcemessage).append("\n"); sb.append(sourcemessage).append("\n");
sb.append("```").append("\n"); sb.append("```").append("\n");
sb.append(ExceptionUtils.getStackTrace(e)).append("\n"); String stackTrace = ExceptionUtils.getStackTrace(e);
if (stackTrace.length() > 2000)
stackTrace = stackTrace.substring(0, 2000);
sb.append(stackTrace).append("\n");
sb.append("```"); sb.append("```");
DiscordPlugin.sendMessageToChannel(DiscordPlugin.issuechannel, sb.toString()); DiscordPlugin.sendMessageToChannel(DiscordPlugin.issuechannel, sb.toString());
} catch (Exception ex) { } catch (Exception ex) {

View file

@ -10,6 +10,7 @@ import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.lib.TBMCPlayerGetInfoEvent; import buttondevteam.lib.TBMCPlayerGetInfoEvent;
import buttondevteam.lib.TBMCPlayerJoinEvent; import buttondevteam.lib.TBMCPlayerJoinEvent;
import sx.blah.discord.handle.obj.IUser; import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.handle.obj.Status.StatusType;
public class MCListener implements Listener { public class MCListener implements Listener {
@EventHandler @EventHandler
@ -29,10 +30,12 @@ public class MCListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(TBMCPlayerJoinEvent e) { public void onPlayerJoin(TBMCPlayerJoinEvent e) {
final Player p = Bukkit.getPlayer(e.GetPlayer().getUuid()); final Player p = Bukkit.getPlayer(e.GetPlayer().getUuid());
if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().getPlayerName())) {
p.sendMessage("§bTo connect with the Discord account " p.sendMessage("§bTo connect with the Discord account "
+ ConnectCommand.WaitingToConnect.get(e.GetPlayer().getPlayerName()) + " do /discord accept"); + ConnectCommand.WaitingToConnect.get(e.GetPlayer().getPlayerName()) + " do /discord accept");
p.sendMessage("§bIf it wasn't you, do /discord decline"); p.sendMessage("§bIf it wasn't you, do /discord decline");
} }
}
@EventHandler @EventHandler
public void onGetInfo(TBMCPlayerGetInfoEvent e) { public void onGetInfo(TBMCPlayerGetInfoEvent e) {
@ -41,6 +44,12 @@ public class MCListener implements Listener {
return; return;
IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID()); IUser user = DiscordPlugin.dc.getUserByID(dp.getDiscordID());
e.addInfo("Discord tag: " + user.getName() + "#" + user.getDiscriminator()); e.addInfo("Discord tag: " + user.getName() + "#" + user.getDiscriminator());
e.addInfo("Discord status: " + user.getStatus()); if (!user.getStatus().getType().equals(StatusType.NONE)) {
if (user.getStatus().getType().equals(StatusType.GAME))
e.addInfo("Discord status: Playing " + user.getStatus().getStatusMessage());
else if (user.getStatus().getType().equals(StatusType.STREAM))
e.addInfo("Discord status: Streaming " + user.getStatus().getStatusMessage() + " - "
+ user.getStatus().getUrl());
}
} }
} }

View file

@ -3,6 +3,7 @@ package buttondevteam.discordplugin.commands;
import java.util.HashMap; import java.util.HashMap;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import sx.blah.discord.handle.obj.IMessage; import sx.blah.discord.handle.obj.IMessage;
public abstract class DiscordCommandBase { public abstract class DiscordCommandBase {
@ -10,7 +11,7 @@ public abstract class DiscordCommandBase {
public abstract void run(IMessage message, String args); public abstract void run(IMessage message, String args);
private static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>(); static final HashMap<String, DiscordCommandBase> commands = new HashMap<String, DiscordCommandBase>();
static { static {
commands.put("connect", new ConnectCommand()); // TODO: API for adding commands? commands.put("connect", new ConnectCommand()); // TODO: API for adding commands?
@ -24,6 +25,12 @@ public abstract class DiscordCommandBase {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Unknown command: " + cmd + " with args: " + args); DiscordPlugin.sendMessageToChannel(message.getChannel(), "Unknown command: " + cmd + " with args: " + args);
return; return;
} }
try {
command.run(message, args); command.run(message, args);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while executing command " + cmd + "!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"An internal error occured while executing this command. For more technical details see the server-issues channel on the dev Discord.");
}
} }
} }

View file

@ -0,0 +1,21 @@
package buttondevteam.discordplugin.commands;
import java.util.stream.Collectors;
import buttondevteam.discordplugin.DiscordPlugin;
import sx.blah.discord.handle.obj.IMessage;
public class HelpCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "help";
}
@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")));
}
}

View file

@ -40,17 +40,17 @@ public class UserinfoCommand extends DiscordCommandBase {
target = ptarget; target = ptarget;
break; break;
} }
if (target == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The user cannot be found (by discriminator): " + args + "(Found " + targets.size()
+ " users with the name.)");
return;
} }
if (target == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The user cannot be found (by discriminator): "
+ args + "(Found " + targets.size() + " users with the name.)");
return;
} }
} else { } else {
final List<IUser> targets = message.getGuild().getUsersByName(args, true); final List<IUser> targets = message.getGuild().getUsersByName(args, true);
if (targets.size() == 0) { if (targets.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "The user cannot be found: " + args); DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The user cannot be found on Discord: " + args);
return; return;
} }
if (targets.size() > 1) { if (targets.size() > 1) {
@ -60,15 +60,20 @@ public class UserinfoCommand extends DiscordCommandBase {
} }
target = targets.get(0); target = targets.get(0);
} }
boolean found = false;
for (TBMCPlayer player : TBMCPlayer.getLoadedPlayers().values()) { for (TBMCPlayer player : TBMCPlayer.getLoadedPlayers().values()) {
DiscordPlayer dp = player.asPluginPlayer(DiscordPlayer.class); DiscordPlayer dp = player.asPluginPlayer(DiscordPlayer.class);
if (target.getID().equals(dp.getDiscordID())) { if (target.getID().equals(dp.getDiscordID())) {
StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":"); StringBuilder uinfo = new StringBuilder("User info for ").append(target.getName()).append(":\n");
uinfo.append(player.getInfo(InfoTarget.Discord)); uinfo.append(player.getInfo(InfoTarget.Discord));
DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString()); DiscordPlugin.sendMessageToChannel(message.getChannel(), uinfo.toString());
found = true;
break; break;
} }
} }
if (!found)
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The user is not found in our system (player has to be on the MC server for now)!");
} }
} }