Discord->MC fixes (mcchat)
This commit is contained in:
parent
ee2e979e12
commit
3af44a053a
1 changed files with 139 additions and 108 deletions
|
@ -44,7 +44,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, () -> {
|
||||
try { // Runs forever - Not good, but most plugins don't support reloading the server anyways
|
||||
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) + "] " //
|
||||
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
|
||||
+ (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()));
|
||||
}
|
||||
|
||||
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<DiscordID> as key for public chat
|
||||
*/
|
||||
|
@ -236,30 +247,46 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
action.accept(data.channel);
|
||||
}
|
||||
|
||||
private BukkitTask rectask;
|
||||
private LinkedBlockingQueue<MessageReceivedEvent> recevents = new LinkedBlockingQueue<>();
|
||||
|
||||
@Override // Discord
|
||||
public void handle(MessageReceivedEvent event) {
|
||||
public void handle(MessageReceivedEvent ev) {
|
||||
if (DiscordPlugin.SafeMode)
|
||||
return;
|
||||
val author = event.getMessage().getAuthor();
|
||||
val user = DiscordPlayer.getUser(author.getStringID(), DiscordPlayer.class);
|
||||
if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
|
||||
&& !(event.getMessage().getChannel().isPrivate() && user.isMinecraftChatEnabled()))
|
||||
val author = ev.getMessage().getAuthor();
|
||||
if (!ev.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
|
||||
&& !(ev.getMessage().getChannel().isPrivate() && isMinecraftChatEnabled(author.getStringID())))
|
||||
return;
|
||||
if (author.isBot())
|
||||
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)
|
||||
if (CommandListener.runCommand(event.getMessage(), true))
|
||||
if (CommandListener.runCommand(ev.getMessage(), true))
|
||||
return;
|
||||
if (!event.getMessage().getChannel().isPrivate())
|
||||
if (!ev.getMessage().getChannel().isPrivate())
|
||||
resetLastMessage();
|
||||
else
|
||||
resetLastMessage(event.getMessage().getChannel());
|
||||
resetLastMessage(ev.getMessage().getChannel());
|
||||
lastlist++;
|
||||
String dmessage = event.getMessage().getContent();
|
||||
synchronized (this) {
|
||||
recevents.add(ev);
|
||||
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 {
|
||||
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()) {
|
||||
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()));
|
||||
}
|
||||
|
||||
BiConsumer<Channel, String> sendChatMessage = (channel, msg) -> TBMCChatAPI.SendChatMessage(channel,
|
||||
dsender,
|
||||
BiConsumer<Channel, String> sendChatMessage = (channel, msg) -> //
|
||||
TBMCChatAPI.SendChatMessage(channel, dsender,
|
||||
msg + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
|
||||
.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 + " "))) {
|
||||
// Command not whitelisted
|
||||
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
|
||||
? "\nTo access your commands, first please connect your accounts, using @ChromaBot connect in "
|
||||
+ DiscordPlugin.botchannel.mention()
|
||||
|
@ -304,14 +332,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
} else {
|
||||
int spi = cmd.indexOf(' ');
|
||||
final String topcmd = spi == -1 ? cmd : cmd.substring(0, spi);
|
||||
Optional<Channel> ch = Channel.getChannels().stream().filter(c -> c.ID.equalsIgnoreCase(topcmd))
|
||||
.findAny();
|
||||
Optional<Channel> ch = Channel.getChannels().stream()
|
||||
.filter(c -> c.ID.equalsIgnoreCase(topcmd)).findAny();
|
||||
if (!ch.isPresent())
|
||||
Bukkit.getScheduler().runTask(DiscordPlugin.plugin,
|
||||
() -> VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd));
|
||||
else {
|
||||
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(
|
||||
"You can only talk in global in the public chat. DM `mcchat` to enable private chat to talk in the other channels.");
|
||||
else {
|
||||
|
@ -340,15 +369,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0
|
||||
&& !event.getChannel().isPrivate())
|
||||
TBMCChatAPI.SendSystemMessage(Channel.GlobalChat, 0,
|
||||
(dsender instanceof Player ? ((Player) dsender).getDisplayName() : dsender.getName())
|
||||
+ " pinned a message on Discord.");
|
||||
(dsender instanceof Player ? ((Player) dsender).getDisplayName()
|
||||
: dsender.getName()) + " pinned a message on Discord.");
|
||||
else {
|
||||
sendChatMessage.accept(dsender.getMcchannel(), dmessage);
|
||||
react = true;
|
||||
}
|
||||
}
|
||||
if (react) {
|
||||
event.getMessage().getChannel().getMessageHistory().stream().forEach(m -> {
|
||||
event.getMessage().getChannel().getMessageHistory(2).stream().forEach(m -> {
|
||||
try {
|
||||
final IReaction reaction = m.getReactionByEmoji(DiscordPlugin.DELIVERED_REACTION);
|
||||
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);
|
||||
}
|
||||
});
|
||||
DPUtils.performNoWait(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
|
||||
DPUtils.perform(() -> event.getMessage().addReaction(DiscordPlugin.DELIVERED_REACTION));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("An error occured while handling message \"" + dmessage + "\"!", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue