Fixed chat msg loss <@187257785199624194>
Probably Also fixed exception filtering
This commit is contained in:
parent
1ba108fb21
commit
c23d9aee09
3 changed files with 118 additions and 94 deletions
|
@ -48,7 +48,7 @@ public class ExceptionListener implements Listener {
|
|||
sb.append(sourcemessage).append("\n");
|
||||
sb.append("```").append("\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.lang") //
|
||||
|| s.contains("net.minecraft.server") //
|
||||
|
|
|
@ -28,7 +28,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
return;
|
||||
if (e.getSender() instanceof DiscordSender || e.getSender() instanceof DiscordPlayerSender)
|
||||
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 //
|
||||
? ((Player) e.getSender()).getDisplayName() //
|
||||
: e.getSender().getName());
|
||||
|
@ -83,76 +85,78 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
if (CommandListener.runCommand(event.getMessage(), true))
|
||||
return;
|
||||
String dmessage = event.getMessage().getContent();
|
||||
try {
|
||||
DiscordPlayer dp = ChromaGamerBase.getUser(author.getID(), DiscordPlayer.class);
|
||||
final DiscordSenderBase dsender;
|
||||
Player mcp = null; // Offline players can't really run commands
|
||||
final String cid;
|
||||
if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected?
|
||||
&& (mcp = Bukkit.getPlayer(cid)) != null) { // Execute as ingame player, if online
|
||||
if (!ConnectedSenders.containsKey(author.getID()))
|
||||
ConnectedSenders.put(author.getID(),
|
||||
new DiscordPlayerSender(author, event.getMessage().getChannel(), mcp));
|
||||
dsender = ConnectedSenders.get(author.getID());
|
||||
} else {
|
||||
TBMCPlayer p = dp.getAs(TBMCPlayer.class);
|
||||
if (!UnconnectedSenders.containsKey(author.getID()))
|
||||
UnconnectedSenders.put(author.getID(), new DiscordSender(author, 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;
|
||||
synchronized (this) {
|
||||
try {
|
||||
DiscordPlayer dp = ChromaGamerBase.getUser(author.getID(), DiscordPlayer.class);
|
||||
final DiscordSenderBase dsender;
|
||||
Player mcp = null; // Offline players can't really run commands
|
||||
final String cid;
|
||||
if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected?
|
||||
&& (mcp = Bukkit.getPlayer(cid)) != null) { // Execute as ingame player, if online
|
||||
if (!ConnectedSenders.containsKey(author.getID()))
|
||||
ConnectedSenders.put(author.getID(),
|
||||
new DiscordPlayerSender(author, event.getMessage().getChannel(), mcp));
|
||||
dsender = ConnectedSenders.get(author.getID());
|
||||
} else {
|
||||
TBMCPlayer p = dp.getAs(TBMCPlayer.class);
|
||||
if (!UnconnectedSenders.containsKey(author.getID()))
|
||||
UnconnectedSenders.put(author.getID(), new DiscordSender(author,
|
||||
event.getMessage().getChannel(), p == null ? null : p.PlayerName().get())); // Display the playername, if found
|
||||
dsender = UnconnectedSenders.get(author.getID());
|
||||
}
|
||||
if (lastlist > 5) {
|
||||
ListC = 0;
|
||||
lastlist = 0;
|
||||
|
||||
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 (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);
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
DiscordPlugin.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
|
||||
if (lastlist > 5) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
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.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
@ -7,32 +13,46 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
public class AppTest extends TestCase {
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName
|
||||
* name of the test case
|
||||
*/
|
||||
public AppTest(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite() {
|
||||
return new TestSuite(AppTest.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp() {
|
||||
/*String sourcemessage = "Test message";
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue