MCchat and other fixes

- Fixed disallowed command stopping message processing
- Fixed failure sending a message to Discord stopping msg proc.
- Added timestamps for messages showing when the message was sent
- Fixed mentioning in mcchat (now it only detects it's own roles)
- Fixed exception reporting (shouldn't break the code block, also only shows buttondevteam lines)
This commit is contained in:
Norbi Peti 2018-04-17 19:05:52 +02:00
parent 68e99e0000
commit 155311b019
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 264 additions and 261 deletions

View file

@ -324,8 +324,8 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
}
private static IMessage sendMessageToChannel(IChannel channel, String message, EmbedObject embed, boolean wait) {
if (message.length() > 1900) {
message = message.substring(0, 1900);
if (message.length() > 1980) {
message = message.substring(0, 1980);
Bukkit.getLogger()
.warning("Message was too long to send to discord and got truncated. In " + channel.getName());
}

View file

@ -1,11 +1,5 @@
package buttondevteam.discordplugin.listeners;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
@ -20,6 +14,12 @@ import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.StatusType;
import sx.blah.discord.util.EmbedBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class CommandListener {
private static final String[] serverReadyStrings = new String[] { "In one week from now", // Ali
@ -141,7 +141,7 @@ public class CommandListener {
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().map(r -> r.mention())::iterator)
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().filter(r -> DiscordPlugin.dc.getOurUser().hasRole(r)).map(r -> r.mention())::iterator)
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
if (mentionedonly && !gotmention) {
message.getChannel().setTypingStatus(false);

View file

@ -1,19 +1,18 @@
package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCExceptionEvent;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import sx.blah.discord.handle.obj.IRole;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCExceptionEvent;
import sx.blah.discord.handle.obj.IRole;
public class ExceptionListener implements Listener {
private List<Throwable> lastthrown = new ArrayList<>();
private List<String> lastsourcemsg = new ArrayList<>();
@ -49,13 +48,8 @@ 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("\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"));
.filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam."))
.collect(Collectors.joining("\n"));
if (stackTrace.length() > 1800)
stackTrace = stackTrace.substring(0, 1800);
sb.append(stackTrace).append("\n");

View file

@ -31,10 +31,8 @@ import sx.blah.discord.util.EmbedBuilder;
import sx.blah.discord.util.MissingPermissionsException;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Optional;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@ -45,21 +43,31 @@ import java.util.stream.Stream;
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
private BukkitTask sendtask;
private LinkedBlockingQueue<TBMCChatEvent> sendevents = new LinkedBlockingQueue<>();
private LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>();
private Runnable sendrunnable;
@EventHandler // Minecraft
public void onMCChat(TBMCChatEvent ev) {
if (ev.isCancelled())
return;
sendevents.add(ev);
sendevents.add(new AbstractMap.SimpleEntry<>(ev, Instant.now()));
if (sendtask != null)
return;
sendrunnable = () -> {
processMCToDiscord();
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
};
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
}
private void processMCToDiscord() {
try {
TBMCChatEvent e;
Instant time;
try {
e = sendevents.take(); // Wait until an element is available
val se = sendevents.take(); // Wait until an element is available
e = se.getKey();
time = se.getValue();
} catch (InterruptedException ex) {
sendtask.cancel();
sendtask = null;
@ -85,6 +93,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=discord&id="
+ ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this
// embed.withFooterText(e.getChannel().DisplayName);
embed.withTimestamp(time);
final long nanoTime = System.nanoTime();
Consumer<LastMsgData> doit = lastmsgdata -> {
final EmbedObject embedObject = embed.build();
@ -104,7 +113,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
final LastMsgData _lastmsgdata = lastmsgdata;
DPUtils.perform(() -> _lastmsgdata.message.edit("", embedObject));
} catch (MissingPermissionsException | DiscordException e1) {
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
TBMCCoreAPI.SendException("An error occurred while editing chat message!", e1);
}
};
// Checks if the given channel is different than where the message was sent from
@ -122,12 +131,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
&& e.shouldSendTo(getSender(data.channel, data.user, data.dp)))
doit.accept(data);
}
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
} catch (Exception ex) {
TBMCCoreAPI.SendException("Error while sending mesasge to Discord!", ex);
TBMCCoreAPI.SendException("Error while sending message to Discord!", ex);
}
};
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
}
@RequiredArgsConstructor
@ -265,6 +271,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
private BukkitTask rectask;
private LinkedBlockingQueue<MessageReceivedEvent> recevents = new LinkedBlockingQueue<>();
private IMessage lastmsgfromd; // Last message sent by a Discord user, used for clearing checkmarks
private Runnable recrun;
@Override // Discord
public void handle(MessageReceivedEvent ev) {
@ -288,8 +295,14 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
recevents.add(ev);
if (rectask != null)
return;
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
while (true) {
recrun = () -> { //Don't return in a while loop next time
processDiscordToMC();
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
};
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
}
private void processDiscordToMC() {
@val
sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event;
try {
@ -324,8 +337,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
event.getMessage().delete();
});
final String cmd = dmessage.substring(1).toLowerCase();
if (dsender instanceof DiscordSender && !Arrays.stream(UnconnectedCmds)
.anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
if (dsender instanceof DiscordSender && Arrays.stream(UnconnectedCmds)
.noneMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
// Command not whitelisted
dsender.sendMessage("Sorry, you can only access these commands:\n"
+ Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
@ -410,12 +423,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);
return;
}
}
});
}
/**
* This method will find the best sender to use: if the player is online, use that, if not but connected then use that etc.