Discord->MC fixes (mcchat)

This commit is contained in:
Norbi Peti 2018-01-07 19:44:39 +01:00
parent ee2e979e12
commit 3af44a053a
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -44,7 +44,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> { sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
try { // Runs forever - Not good, but most plugins don't support reloading the server anyways try { // Runs forever - Not good, but most plugins don't support reloading the server anyways
while (true) { while (true) {
val e = sendevents.take(); // Wait until an element is available TBMCChatEvent e;
try {
e = sendevents.take(); // Wait until an element is available
} catch (InterruptedException ex) {
sendtask.cancel();
return;
}
final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " // final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " //
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") // + (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
+ (DPUtils.sanitizeString(e.getSender() instanceof Player // + (DPUtils.sanitizeString(e.getSender() instanceof Player //
@ -188,6 +194,11 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(dp.getDiscordID())); lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(dp.getDiscordID()));
} }
public static boolean isMinecraftChatEnabled(String did) { // Don't load the player data just for this
return lastmsgPerUser.stream()
.anyMatch(lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(did));
}
/** /**
* May contain P&lt;DiscordID&gt; as key for public chat * May contain P&lt;DiscordID&gt; as key for public chat
*/ */
@ -236,30 +247,46 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
action.accept(data.channel); action.accept(data.channel);
} }
private BukkitTask rectask;
private LinkedBlockingQueue<MessageReceivedEvent> recevents = new LinkedBlockingQueue<>();
@Override // Discord @Override // Discord
public void handle(MessageReceivedEvent event) { public void handle(MessageReceivedEvent ev) {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return;
val author = event.getMessage().getAuthor(); val author = ev.getMessage().getAuthor();
val user = DiscordPlayer.getUser(author.getStringID(), DiscordPlayer.class); if (!ev.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID()) && !(ev.getMessage().getChannel().isPrivate() && isMinecraftChatEnabled(author.getStringID())))
&& !(event.getMessage().getChannel().isPrivate() && user.isMinecraftChatEnabled()))
return; return;
if (author.isBot()) if (author.isBot())
return; return;
if (event.getMessage().getContent().equalsIgnoreCase("mcchat")) if (ev.getMessage().getContent().equalsIgnoreCase("mcchat"))
return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommandListener) return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommandListener)
if (CommandListener.runCommand(event.getMessage(), true)) if (CommandListener.runCommand(ev.getMessage(), true))
return; return;
if (!event.getMessage().getChannel().isPrivate()) if (!ev.getMessage().getChannel().isPrivate())
resetLastMessage(); resetLastMessage();
else else
resetLastMessage(event.getMessage().getChannel()); resetLastMessage(ev.getMessage().getChannel());
lastlist++; lastlist++;
String dmessage = event.getMessage().getContent(); recevents.add(ev);
synchronized (this) { if (rectask != null)
return;
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
while (true) {
@val
sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event;
try { try {
final DiscordSenderBase dsender = getSender(event.getMessage().getChannel(), author, user); event = recevents.take();
} catch (InterruptedException e1) {
rectask.cancel();
return;
}
val sender = event.getMessage().getAuthor();
val user = DiscordPlayer.getUser(sender.getStringID(), DiscordPlayer.class);
String dmessage = event.getMessage().getContent();
try {
final DiscordSenderBase dsender = getSender(event.getMessage().getChannel(), sender, user);
for (IUser u : event.getMessage().getMentions()) { for (IUser u : event.getMessage().getMentions()) {
dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting
@ -267,8 +294,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
dmessage = dmessage.replace(u.mention(true), "@" + (nick != null ? nick : u.getName())); dmessage = dmessage.replace(u.mention(true), "@" + (nick != null ? nick : u.getName()));
} }
BiConsumer<Channel, String> sendChatMessage = (channel, msg) -> TBMCChatAPI.SendChatMessage(channel, BiConsumer<Channel, String> sendChatMessage = (channel, msg) -> //
dsender, TBMCChatAPI.SendChatMessage(channel, dsender,
msg + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage() msg + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n")) .getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n"))
: "")); : ""));
@ -285,7 +312,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
.anyMatch(s -> cmd.equals(s) || cmd.startsWith(s + " "))) { .anyMatch(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).collect(Collectors.joining(", ")) + Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
.collect(Collectors.joining(", "))
+ (user.getConnectedID(TBMCPlayer.class) == null + (user.getConnectedID(TBMCPlayer.class) == null
? "\nTo access your commands, first please connect your accounts, using @ChromaBot connect in " ? "\nTo access your commands, first please connect your accounts, using @ChromaBot connect in "
+ DiscordPlugin.botchannel.mention() + DiscordPlugin.botchannel.mention()
@ -304,14 +332,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
} else { } else {
int spi = cmd.indexOf(' '); int spi = cmd.indexOf(' ');
final String topcmd = spi == -1 ? cmd : cmd.substring(0, spi); final String topcmd = spi == -1 ? cmd : cmd.substring(0, spi);
Optional<Channel> ch = Channel.getChannels().stream().filter(c -> c.ID.equalsIgnoreCase(topcmd)) Optional<Channel> ch = Channel.getChannels().stream()
.findAny(); .filter(c -> c.ID.equalsIgnoreCase(topcmd)).findAny();
if (!ch.isPresent()) if (!ch.isPresent())
Bukkit.getScheduler().runTask(DiscordPlugin.plugin, Bukkit.getScheduler().runTask(DiscordPlugin.plugin,
() -> VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd)); () -> VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd));
else { else {
Channel chc = ch.get(); Channel chc = ch.get();
if (!chc.ID.equals(Channel.GlobalChat.ID) && !event.getMessage().getChannel().isPrivate()) if (!chc.ID.equals(Channel.GlobalChat.ID)
&& !event.getMessage().getChannel().isPrivate())
dsender.sendMessage( dsender.sendMessage(
"You can only talk in global in the public chat. DM `mcchat` to enable private chat to talk in the other channels."); "You can only talk in global in the public chat. DM `mcchat` to enable private chat to talk in the other channels.");
else { else {
@ -340,15 +369,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0 if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0
&& !event.getChannel().isPrivate()) && !event.getChannel().isPrivate())
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, 0, TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, 0,
(dsender instanceof Player ? ((Player) dsender).getDisplayName() : dsender.getName()) (dsender instanceof Player ? ((Player) dsender).getDisplayName()
+ " pinned a message on Discord."); : dsender.getName()) + " pinned a message on Discord.");
else { else {
sendChatMessage.accept(dsender.getMcchannel(), dmessage); sendChatMessage.accept(dsender.getMcchannel(), dmessage);
react = true; react = true;
} }
} }
if (react) { if (react) {
event.getMessage().getChannel().getMessageHistory().stream().forEach(m -> { event.getMessage().getChannel().getMessageHistory(2).stream().forEach(m -> {
try { try {
final IReaction reaction = m.getReactionByEmoji(DiscordPlugin.DELIVERED_REACTION); final IReaction reaction = m.getReactionByEmoji(DiscordPlugin.DELIVERED_REACTION);
if (reaction != null) if (reaction != null)
@ -357,13 +386,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e); TBMCCoreAPI.SendException("An error occured while removing reactions from chat!", e);
} }
}); });
DPUtils.performNoWait(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION)); DPUtils.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
} }
} 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; return;
} }
} }
});
} }
/** /**