diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java index 567bfbde553..4733e7d2a7b 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java @@ -57,6 +57,7 @@ public static final class DefaultTypes { public final static MessageType FIRST_JOIN = new MessageType("first-join", true); public final static MessageType LEAVE = new MessageType("leave", true); public final static MessageType CHAT = new MessageType("chat", true); + public final static MessageType PRIVATE_CHAT = new MessageType("private-chat", true); public final static MessageType DEATH = new MessageType("death", true); public final static MessageType AFK = new MessageType("afk", true); public final static MessageType ADVANCEMENT = new MessageType("advancement", true); @@ -68,7 +69,7 @@ public static final class DefaultTypes { public final static MessageType LOCAL = new MessageType("local", true); public final static MessageType QUESTION = new MessageType("question", true); public final static MessageType SHOUT = new MessageType("shout", true); - private final static MessageType[] VALUES = new MessageType[]{JOIN, FIRST_JOIN, LEAVE, CHAT, DEATH, AFK, ADVANCEMENT, ACTION, SERVER_START, SERVER_STOP, KICK, MUTE, LOCAL, QUESTION, SHOUT}; + private final static MessageType[] VALUES = new MessageType[]{JOIN, FIRST_JOIN, LEAVE, CHAT, PRIVATE_CHAT, DEATH, AFK, ADVANCEMENT, ACTION, SERVER_START, SERVER_STOP, KICK, MUTE, LOCAL, QUESTION, SHOUT}; /** * Gets an array of all the default {@link MessageType MessageTypes}. diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java index 3327a34b23e..ced5b861236 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java @@ -48,6 +48,7 @@ public class DiscordSettings implements IConf { private MessageFormat permMuteReasonFormat; private MessageFormat unmuteFormat; private MessageFormat kickFormat; + private MessageFormat pmToDiscordFormat; public DiscordSettings(EssentialsDiscord plugin) { this.plugin = plugin; @@ -438,6 +439,10 @@ public MessageFormat getKickFormat() { return kickFormat; } + public MessageFormat getPmToDiscordFormat() { + return pmToDiscordFormat; + } + private String getFormatString(String node) { final String pathPrefix = node.startsWith(".") ? "" : "messages."; return config.getString(pathPrefix + (pathPrefix.isEmpty() ? node.substring(1) : node), null); @@ -574,6 +579,8 @@ public void reloadConfig() { "username", "displayname", "controllername", "controllerdisplayname", "reason"); kickFormat = generateMessageFormat(getFormatString("kick"), "{displayname} was kicked with reason: {reason}", false, "username", "displayname", "reason"); + pmToDiscordFormat = generateMessageFormat(getFormatString("private-chat"), "{sender-username} -> {receiver-username}: {message}", false, + "sender-username", "sender-displayname", "receiver-username", "receiver-displayname", "message"); plugin.onReload(); } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java index ebc023d8b9d..fd7b515a9a0 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java @@ -75,7 +75,7 @@ public class JDADiscordService implements DiscordService, IEssentialsModule { private final static Logger logger = EssentialsDiscord.getWrappedLogger(); private final EssentialsDiscord plugin; private final Unsafe unsafe = this::getJda; - + private JDA jda; private Guild guild; private TextChannel primaryChannel; @@ -89,24 +89,24 @@ public class JDADiscordService implements DiscordService, IEssentialsModule { private InteractionControllerImpl interactionController; private Listener chatListener; private boolean invalidStartup = false; - + public JDADiscordService(EssentialsDiscord plugin) { this.plugin = plugin; for (final MessageType type : MessageType.DefaultTypes.values()) { registerMessageType(plugin, type); } } - + public TextChannel getChannel(String key, boolean primaryFallback) { if (NumberUtil.isLong(key)) { return getDefinedChannel(key, primaryFallback); } return getDefinedChannel(getSettings().getMessageChannel(key), primaryFallback); } - + public TextChannel getDefinedChannel(String key, boolean primaryFallback) { final long resolvedId = getSettings().getChannelId(key); - + if (isDebug()) { logger.log(Level.INFO, "Channel definition " + key + " resolved as " + resolvedId); } @@ -139,6 +139,7 @@ public void sendMessage(DiscordMessageEvent event, String message, boolean group final String strippedContent = FormatUtil.stripFormat(message); final String webhookChannelId = typeToChannelId.get(event.getType()); + if (webhookChannelId != null) { final WrappedWebhookClient client = channelIdToWebhook.get(webhookChannelId); if (client != null) { @@ -153,6 +154,7 @@ public void sendMessage(DiscordMessageEvent event, String message, boolean group logger.warning(tlLiteral("discordNoSendPermission", channel.getName())); return; } + channel.sendMessage(strippedContent) .setAllowedMentions(groupMentions ? null : DiscordUtil.NO_GROUP_MENTIONS) .queue(); diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java index 6e471786d52..fcf0c4b4078 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.VersionUtil; +import net.ess3.api.events.PrivateMessageSentEvent; import net.ess3.api.IUser; import net.ess3.api.events.AfkStatusChangeEvent; import net.ess3.api.events.MuteStatusChangeEvent; @@ -16,6 +17,7 @@ import net.essentialsx.discord.JDADiscordService; import net.essentialsx.discord.util.DiscordUtil; import net.essentialsx.discord.util.MessageUtil; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameRule; import org.bukkit.entity.Player; @@ -47,6 +49,25 @@ public void onDiscordMessage(DiscordMessageEvent event) { // Bukkit Events + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPrivateMessage(PrivateMessageSentEvent event) { + final Player sender = Bukkit.getPlayer(event.getSender().getUUID()); + final Player recipient = Bukkit.getPlayer(event.getRecipient().getUUID()); + + if (sender.hasPermission("essentials.chat.spy.exempt")) { + return; + } + + sendDiscordMessage(MessageType.DefaultTypes.PRIVATE_CHAT, + MessageUtil.formatMessage(jda.getSettings().getPmToDiscordFormat(), + MessageUtil.sanitizeDiscordMarkdown(sender.getName()), + MessageUtil.sanitizeDiscordMarkdown(sender.getDisplayName()), + MessageUtil.sanitizeDiscordMarkdown(recipient.getName()), + MessageUtil.sanitizeDiscordMarkdown(recipient.getDisplayName()), + MessageUtil.sanitizeDiscordMarkdown(event.getMessage())), + sender); + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onMute(MuteStatusChangeEvent event) { if (!event.getValue()) { diff --git a/EssentialsDiscord/src/main/resources/config.yml b/EssentialsDiscord/src/main/resources/config.yml index 331be18df2c..2994661bf30 100644 --- a/EssentialsDiscord/src/main/resources/config.yml +++ b/EssentialsDiscord/src/main/resources/config.yml @@ -144,6 +144,8 @@ message-types: kick: staff # Message sent when a player's mute state is changed on the Minecraft server. mute: staff + # Message sent when a private message (/msg, /whisper, etc.) is sent on the Minecraft Server. + private-chat: none # Message sent when a player talks in local chat. # use-essentials-events must be set to "true" for this to work. local: none @@ -432,3 +434,11 @@ messages: # - {displayname}: The display name of the user who got kicked # - {reason}: The reason the player was kicked kick: "{displayname} was kicked with reason: {reason}" + # This is the message that is used to relay minecraft private messages in Discord. + # The following placeholders can be used here: + # - {sender-username}: The username of the player sending the message + # - {sender-displayname}: The display name of the player sending the message (This would be their nickname) + # - {receiver-username}: The username of the player receiving the message + # - {receiver-displayname}: The display name of the player receiving the message (This would be their nickname) + # - {message}: The content of the message being sent + pms-to-discord: "{sender-username} -> {receiver-username}: {message}"