Fixed chat msg loss <@187257785199624194>

Probably

Also fixed exception filtering
This commit is contained in:
Norbi Peti 2017-04-28 22:41:23 +02:00
parent 1ba108fb21
commit c23d9aee09
3 changed files with 118 additions and 94 deletions

View file

@ -48,7 +48,7 @@ public class ExceptionListener implements Listener {
sb.append(sourcemessage).append("\n"); sb.append(sourcemessage).append("\n");
sb.append("```").append("\n"); sb.append("```").append("\n");
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n")) String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
.filter(s -> !(s.contains(" at ") && ( // .filter(s -> !(s.contains("\tat ") && ( //
s.contains("java.util") // s.contains("java.util") //
|| s.contains("java.lang") // || s.contains("java.lang") //
|| s.contains("net.minecraft.server") // || s.contains("net.minecraft.server") //

View file

@ -28,7 +28,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
return; return;
if (e.getSender() instanceof DiscordSender || e.getSender() instanceof DiscordPlayerSender) if (e.getSender() instanceof DiscordSender || e.getSender() instanceof DiscordPlayerSender)
return; return;
if (e.getChannel().equals(Channel.GlobalChat)) { if (!e.getChannel().equals(Channel.GlobalChat))
return;
synchronized (this) {
final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player // final String authorPlayer = DiscordPlugin.sanitizeString(e.getSender() instanceof Player //
? ((Player) e.getSender()).getDisplayName() // ? ((Player) e.getSender()).getDisplayName() //
: e.getSender().getName()); : e.getSender().getName());
@ -83,76 +85,78 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (CommandListener.runCommand(event.getMessage(), true)) if (CommandListener.runCommand(event.getMessage(), true))
return; return;
String dmessage = event.getMessage().getContent(); String dmessage = event.getMessage().getContent();
try { synchronized (this) {
DiscordPlayer dp = ChromaGamerBase.getUser(author.getID(), DiscordPlayer.class); try {
final DiscordSenderBase dsender; DiscordPlayer dp = ChromaGamerBase.getUser(author.getID(), DiscordPlayer.class);
Player mcp = null; // Offline players can't really run commands final DiscordSenderBase dsender;
final String cid; Player mcp = null; // Offline players can't really run commands
if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected? final String cid;
&& (mcp = Bukkit.getPlayer(cid)) != null) { // Execute as ingame player, if online if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected?
if (!ConnectedSenders.containsKey(author.getID())) && (mcp = Bukkit.getPlayer(cid)) != null) { // Execute as ingame player, if online
ConnectedSenders.put(author.getID(), if (!ConnectedSenders.containsKey(author.getID()))
new DiscordPlayerSender(author, event.getMessage().getChannel(), mcp)); ConnectedSenders.put(author.getID(),
dsender = ConnectedSenders.get(author.getID()); new DiscordPlayerSender(author, event.getMessage().getChannel(), mcp));
} else { dsender = ConnectedSenders.get(author.getID());
TBMCPlayer p = dp.getAs(TBMCPlayer.class); } else {
if (!UnconnectedSenders.containsKey(author.getID())) TBMCPlayer p = dp.getAs(TBMCPlayer.class);
UnconnectedSenders.put(author.getID(), new DiscordSender(author, event.getMessage().getChannel(), if (!UnconnectedSenders.containsKey(author.getID()))
p == null ? null : p.PlayerName().get())); // Display the playername, if found UnconnectedSenders.put(author.getID(), new DiscordSender(author,
dsender = UnconnectedSenders.get(author.getID()); event.getMessage().getChannel(), p == null ? null : p.PlayerName().get())); // Display the playername, if found
} dsender = UnconnectedSenders.get(author.getID());
for (IUser u : event.getMessage().getMentions()) {
dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting
final Optional<String> nick = u.getNicknameForGuild(DiscordPlugin.mainServer);
dmessage = dmessage.replace(u.mention(true), "@" + (nick.isPresent() ? nick.get() : u.getName()));
}
if (dmessage.startsWith("/")) {
final String cmd = dmessage.substring(1).toLowerCase();
if (mcp == null
&& !Arrays.stream(UnconnectedCmds).anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
// Command not whitelisted
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
"Sorry, you need to be online on the server and have your accounts connected, you can only access these commands:\n"
+ Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
.collect(Collectors.joining(", "))
+ "\nTo connect your accounts, use @ChromaBot connect in "
+ DiscordPlugin.botchannel.mention());
return;
} }
if (lastlist > 5) {
ListC = 0; for (IUser u : event.getMessage().getMentions()) {
lastlist = 0; dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting
final Optional<String> nick = u.getNicknameForGuild(DiscordPlugin.mainServer);
dmessage = dmessage.replace(u.mention(true), "@" + (nick.isPresent() ? nick.get() : u.getName()));
} }
if (cmd.equals("list") && Bukkit.getOnlinePlayers().size() == lastlistp && ListC++ > 2) // Lowered already
{ if (dmessage.startsWith("/")) {
dsender.sendMessage("Stop it. You know the answer."); final String cmd = dmessage.substring(1).toLowerCase();
lastlist = 0; if (mcp == null && !Arrays.stream(UnconnectedCmds)
} else .anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
Bukkit.dispatchCommand(dsender, cmd); // Command not whitelisted
lastlistp = (short) Bukkit.getOnlinePlayers().size(); DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
if (!event.getMessage().isDeleted()) "Sorry, you need to be online on the server and have your accounts connected, you can only access these commands:\n"
event.getMessage().delete(); + Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
} else { .collect(Collectors.joining(", "))
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender, + "\nTo connect your accounts, use @ChromaBot connect in "
dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage() + DiscordPlugin.botchannel.mention());
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n")) return;
: ""));
event.getMessage().getChannel().getMessages().stream().forEach(m -> {
try {
final IReaction reaction = m.getReactionByName(DiscordPlugin.DELIVERED_REACTION);
if (reaction != null)
DiscordPlugin.perform(() -> m.removeReaction(reaction));
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
} }
}); if (lastlist > 5) {
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION)); ListC = 0;
lastlist = 0;
}
if (cmd.equals("list") && Bukkit.getOnlinePlayers().size() == lastlistp && ListC++ > 2) // Lowered already
{
dsender.sendMessage("Stop it. You know the answer.");
lastlist = 0;
} else
Bukkit.dispatchCommand(dsender, cmd);
lastlistp = (short) Bukkit.getOnlinePlayers().size();
if (!event.getMessage().isDeleted())
event.getMessage().delete();
} else {
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender,
dmessage + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n"))
: ""));
event.getMessage().getChannel().getMessages().stream().forEach(m -> {
try {
final IReaction reaction = m.getReactionByName(DiscordPlugin.DELIVERED_REACTION);
if (reaction != null)
DiscordPlugin.perform(() -> m.removeReaction(reaction));
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
}
});
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);
return;
} }
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);
return;
} }
} }
} }

View file

@ -1,5 +1,11 @@
package buttondevteam.DiscordPlugin; package buttondevteam.DiscordPlugin;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.commons.lang.exception.ExceptionUtils;
import buttondevteam.lib.TBMCCoreAPI;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -7,32 +13,46 @@ import junit.framework.TestSuite;
/** /**
* Unit test for simple App. * Unit test for simple App.
*/ */
public class AppTest public class AppTest extends TestCase {
extends TestCase /**
{ * Create the test case
/** *
* Create the test case * @param testName
* * name of the test case
* @param testName name of the test case */
*/ public AppTest(String testName) {
public AppTest( String testName ) super(testName);
{ }
super( testName );
}
/** /**
* @return the suite of tests being tested * @return the suite of tests being tested
*/ */
public static Test suite() public static Test suite() {
{ return new TestSuite(AppTest.class);
return new TestSuite( AppTest.class ); }
}
/** /**
* Rigourous Test :-) * Rigourous Test :-)
*/ */
public void testApp() public void testApp() {
{ /*String sourcemessage = "Test message";
assertTrue( true ); Exception e = new Exception("Test exception");
} StringBuilder sb = TBMCCoreAPI.IsTestServer() ? new StringBuilder() : new StringBuilder("").append("\n");
sb.append(sourcemessage).append("\n");
sb.append("```").append("\n");
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
.filter(s -> !(s.contains("\tat ") && ( //
s.contains("java.util") //
|| s.contains("java.lang") //
|| s.contains("net.minecraft.server") //
|| s.contains("sun.reflect") //
|| s.contains("org.bukkit") //
))).collect(Collectors.joining("\n"));
if (stackTrace.length() > 1800)
stackTrace = stackTrace.substring(0, 1800);
sb.append(stackTrace).append("\n");
sb.append("```");
System.out.println(sb.toString()); */
assertTrue(true);
}
} }