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:
parent
68e99e0000
commit
155311b019
4 changed files with 264 additions and 261 deletions
|
@ -324,8 +324,8 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IMessage sendMessageToChannel(IChannel channel, String message, EmbedObject embed, boolean wait) {
|
private static IMessage sendMessageToChannel(IChannel channel, String message, EmbedObject embed, boolean wait) {
|
||||||
if (message.length() > 1900) {
|
if (message.length() > 1980) {
|
||||||
message = message.substring(0, 1900);
|
message = message.substring(0, 1980);
|
||||||
Bukkit.getLogger()
|
Bukkit.getLogger()
|
||||||
.warning("Message was too long to send to discord and got truncated. In " + channel.getName());
|
.warning("Message was too long to send to discord and got truncated. In " + channel.getName());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
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.DiscordPlayer;
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.commands.DiscordCommandBase;
|
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.handle.obj.StatusType;
|
||||||
import sx.blah.discord.util.EmbedBuilder;
|
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 {
|
public class CommandListener {
|
||||||
|
|
||||||
private static final String[] serverReadyStrings = new String[] { "In one week from now", // Ali
|
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);
|
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
|
||||||
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
|
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
|
||||||
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
|
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
|
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
|
||||||
if (mentionedonly && !gotmention) {
|
if (mentionedonly && !gotmention) {
|
||||||
message.getChannel().setTypingStatus(false);
|
message.getChannel().setTypingStatus(false);
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
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 {
|
public class ExceptionListener implements Listener {
|
||||||
private List<Throwable> lastthrown = new ArrayList<>();
|
private List<Throwable> lastthrown = new ArrayList<>();
|
||||||
private List<String> lastsourcemsg = new ArrayList<>();
|
private List<String> lastsourcemsg = new ArrayList<>();
|
||||||
|
@ -49,13 +48,8 @@ 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("\tat ") && ( //
|
.filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam."))
|
||||||
s.contains("java.util") //
|
.collect(Collectors.joining("\n"));
|
||||||
|| 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)
|
if (stackTrace.length() > 1800)
|
||||||
stackTrace = stackTrace.substring(0, 1800);
|
stackTrace = stackTrace.substring(0, 1800);
|
||||||
sb.append(stackTrace).append("\n");
|
sb.append(stackTrace).append("\n");
|
||||||
|
|
|
@ -31,10 +31,8 @@ import sx.blah.discord.util.EmbedBuilder;
|
||||||
import sx.blah.discord.util.MissingPermissionsException;
|
import sx.blah.discord.util.MissingPermissionsException;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
@ -45,21 +43,31 @@ import java.util.stream.Stream;
|
||||||
|
|
||||||
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
public class MCChatListener implements Listener, IListener<MessageReceivedEvent> {
|
||||||
private BukkitTask sendtask;
|
private BukkitTask sendtask;
|
||||||
private LinkedBlockingQueue<TBMCChatEvent> sendevents = new LinkedBlockingQueue<>();
|
private LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>();
|
||||||
private Runnable sendrunnable;
|
private Runnable sendrunnable;
|
||||||
|
|
||||||
@EventHandler // Minecraft
|
@EventHandler // Minecraft
|
||||||
public void onMCChat(TBMCChatEvent ev) {
|
public void onMCChat(TBMCChatEvent ev) {
|
||||||
if (ev.isCancelled())
|
if (ev.isCancelled())
|
||||||
return;
|
return;
|
||||||
sendevents.add(ev);
|
sendevents.add(new AbstractMap.SimpleEntry<>(ev, Instant.now()));
|
||||||
if (sendtask != null)
|
if (sendtask != null)
|
||||||
return;
|
return;
|
||||||
sendrunnable = () -> {
|
sendrunnable = () -> {
|
||||||
|
processMCToDiscord();
|
||||||
|
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
|
||||||
|
};
|
||||||
|
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processMCToDiscord() {
|
||||||
try {
|
try {
|
||||||
TBMCChatEvent e;
|
TBMCChatEvent e;
|
||||||
|
Instant time;
|
||||||
try {
|
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) {
|
} catch (InterruptedException ex) {
|
||||||
sendtask.cancel();
|
sendtask.cancel();
|
||||||
sendtask = null;
|
sendtask = null;
|
||||||
|
@ -85,6 +93,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=discord&id="
|
.withAuthorUrl("https://tbmcplugins.github.io/profile.html?type=discord&id="
|
||||||
+ ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this
|
+ ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this
|
||||||
// embed.withFooterText(e.getChannel().DisplayName);
|
// embed.withFooterText(e.getChannel().DisplayName);
|
||||||
|
embed.withTimestamp(time);
|
||||||
final long nanoTime = System.nanoTime();
|
final long nanoTime = System.nanoTime();
|
||||||
Consumer<LastMsgData> doit = lastmsgdata -> {
|
Consumer<LastMsgData> doit = lastmsgdata -> {
|
||||||
final EmbedObject embedObject = embed.build();
|
final EmbedObject embedObject = embed.build();
|
||||||
|
@ -104,7 +113,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
final LastMsgData _lastmsgdata = lastmsgdata;
|
final LastMsgData _lastmsgdata = lastmsgdata;
|
||||||
DPUtils.perform(() -> _lastmsgdata.message.edit("", embedObject));
|
DPUtils.perform(() -> _lastmsgdata.message.edit("", embedObject));
|
||||||
} catch (MissingPermissionsException | DiscordException e1) {
|
} 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
|
// 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)))
|
&& e.shouldSendTo(getSender(data.channel, data.user, data.dp)))
|
||||||
doit.accept(data);
|
doit.accept(data);
|
||||||
}
|
}
|
||||||
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
|
|
||||||
} catch (Exception ex) {
|
} 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
|
@RequiredArgsConstructor
|
||||||
|
@ -265,6 +271,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
private BukkitTask rectask;
|
private BukkitTask rectask;
|
||||||
private LinkedBlockingQueue<MessageReceivedEvent> recevents = new LinkedBlockingQueue<>();
|
private LinkedBlockingQueue<MessageReceivedEvent> recevents = new LinkedBlockingQueue<>();
|
||||||
private IMessage lastmsgfromd; // Last message sent by a Discord user, used for clearing checkmarks
|
private IMessage lastmsgfromd; // Last message sent by a Discord user, used for clearing checkmarks
|
||||||
|
private Runnable recrun;
|
||||||
|
|
||||||
@Override // Discord
|
@Override // Discord
|
||||||
public void handle(MessageReceivedEvent ev) {
|
public void handle(MessageReceivedEvent ev) {
|
||||||
|
@ -288,8 +295,14 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
recevents.add(ev);
|
recevents.add(ev);
|
||||||
if (rectask != null)
|
if (rectask != null)
|
||||||
return;
|
return;
|
||||||
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
|
recrun = () -> { //Don't return in a while loop next time
|
||||||
while (true) {
|
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
|
@val
|
||||||
sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event;
|
sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event;
|
||||||
try {
|
try {
|
||||||
|
@ -324,8 +337,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
event.getMessage().delete();
|
event.getMessage().delete();
|
||||||
});
|
});
|
||||||
final String cmd = dmessage.substring(1).toLowerCase();
|
final String cmd = dmessage.substring(1).toLowerCase();
|
||||||
if (dsender instanceof DiscordSender && !Arrays.stream(UnconnectedCmds)
|
if (dsender instanceof DiscordSender && Arrays.stream(UnconnectedCmds)
|
||||||
.anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
|
.noneMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) {
|
||||||
// Command not whitelisted
|
// Command not whitelisted
|
||||||
dsender.sendMessage("Sorry, you can only access these commands:\n"
|
dsender.sendMessage("Sorry, you can only access these commands:\n"
|
||||||
+ Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
|
+ Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
|
||||||
|
@ -410,12 +423,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", 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.
|
* This method will find the best sender to use: if the player is online, use that, if not but connected then use that etc.
|
||||||
|
|
Loading…
Reference in a new issue