Skip to content

Commit 50b696b

Browse files
authored
Adds social spy message type for in EssentialsDiscord (#5620)
1 parent cb6187b commit 50b696b

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

Essentials/src/main/resources/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ nickname-prefix: '~'
3434
max-nick-length: 15
3535

3636
# The regex pattern used to determine if a requested nickname should be allowed for use.
37-
# If the a request nickname does not matched this pattern, the nickname will be rejected.
37+
# If the a requested nickname does not matched this pattern, the nickname will be rejected.
3838
# Users with essentials.nick.allowunsafe will be able to bypass this check.
3939
allowed-nicks-regex: '^[a-zA-Z_0-9§]+$'
4040

EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/MessageType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static final class DefaultTypes {
5757
public final static MessageType FIRST_JOIN = new MessageType("first-join", true);
5858
public final static MessageType LEAVE = new MessageType("leave", true);
5959
public final static MessageType CHAT = new MessageType("chat", true);
60+
public final static MessageType PRIVATE_CHAT = new MessageType("private-chat", true);
6061
public final static MessageType DEATH = new MessageType("death", true);
6162
public final static MessageType AFK = new MessageType("afk", true);
6263
public final static MessageType ADVANCEMENT = new MessageType("advancement", true);
@@ -68,7 +69,7 @@ public static final class DefaultTypes {
6869
public final static MessageType LOCAL = new MessageType("local", true);
6970
public final static MessageType QUESTION = new MessageType("question", true);
7071
public final static MessageType SHOUT = new MessageType("shout", true);
71-
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};
72+
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};
7273

7374
/**
7475
* Gets an array of all the default {@link MessageType MessageTypes}.

EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class DiscordSettings implements IConf {
5050
private MessageFormat permMuteReasonFormat;
5151
private MessageFormat unmuteFormat;
5252
private MessageFormat kickFormat;
53+
private MessageFormat pmToDiscordFormat;
5354

5455
public DiscordSettings(EssentialsDiscord plugin) {
5556
this.plugin = plugin;
@@ -445,6 +446,10 @@ public MessageFormat getKickFormat() {
445446
return kickFormat;
446447
}
447448

449+
public MessageFormat getPmToDiscordFormat() {
450+
return pmToDiscordFormat;
451+
}
452+
448453
private String getFormatString(String node) {
449454
final String pathPrefix = node.startsWith(".") ? "" : "messages.";
450455
return config.getString(pathPrefix + (pathPrefix.isEmpty() ? node.substring(1) : node), null);
@@ -581,6 +586,8 @@ public void reloadConfig() {
581586
"username", "displayname", "controllername", "controllerdisplayname", "reason");
582587
kickFormat = generateMessageFormat(getFormatString("kick"), "{displayname} was kicked with reason: {reason}", false,
583588
"username", "displayname", "reason");
589+
pmToDiscordFormat = generateMessageFormat(getFormatString("private-chat"), "[SocialSpy] {sender-username} -> {receiver-username}: {message}", false,
590+
"sender-username", "sender-displayname", "receiver-username", "receiver-displayname", "message");
584591

585592
plugin.onReload();
586593
}

EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/BukkitListener.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.earth2me.essentials.utils.DateUtil;
55
import com.earth2me.essentials.utils.FormatUtil;
66
import com.earth2me.essentials.utils.VersionUtil;
7+
import net.ess3.api.events.PrivateMessageSentEvent;
78
import net.ess3.api.IUser;
89
import net.ess3.api.events.AfkStatusChangeEvent;
910
import net.ess3.api.events.MuteStatusChangeEvent;
@@ -47,6 +48,23 @@ public void onDiscordMessage(DiscordMessageEvent event) {
4748

4849
// Bukkit Events
4950

51+
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
52+
public void onPrivateMessage(PrivateMessageSentEvent event) {
53+
54+
if (event.getSender() instanceof IUser && ((IUser) event.getSender()).isAuthorized("essentials.chat.spy.exempt")) {
55+
return;
56+
}
57+
58+
sendDiscordMessage(MessageType.DefaultTypes.PRIVATE_CHAT,
59+
MessageUtil.formatMessage(jda.getSettings().getPmToDiscordFormat(),
60+
MessageUtil.sanitizeDiscordMarkdown(event.getSender().getName()),
61+
MessageUtil.sanitizeDiscordMarkdown(event.getSender().getDisplayName()),
62+
MessageUtil.sanitizeDiscordMarkdown(event.getRecipient().getName()),
63+
MessageUtil.sanitizeDiscordMarkdown(event.getRecipient().getDisplayName()),
64+
MessageUtil.sanitizeDiscordMarkdown(event.getMessage())),
65+
event.getSender() instanceof IUser ? ((IUser) event.getSender()).getBase() : null);
66+
}
67+
5068
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
5169
public void onMute(MuteStatusChangeEvent event) {
5270
if (!event.getValue()) {

EssentialsDiscord/src/main/resources/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ message-types:
144144
kick: staff
145145
# Message sent when a player's mute state is changed on the Minecraft server.
146146
mute: staff
147+
# Message sent when a private message (/msg, /whisper, etc.) is sent on the Minecraft Server.
148+
private-chat: none
147149
# Message sent when a player talks in local chat.
148150
# use-essentials-events must be set to "true" for this to work.
149151
local: none
@@ -433,3 +435,11 @@ messages:
433435
# - {displayname}: The display name of the user who got kicked
434436
# - {reason}: The reason the player was kicked
435437
kick: "{displayname} was kicked with reason: {reason}"
438+
# This is the message that is used to relay minecraft private messages in Discord.
439+
# The following placeholders can be used here:
440+
# - {sender-username}: The username of the player sending the message
441+
# - {sender-displayname}: The display name of the player sending the message (This would be their nickname)
442+
# - {receiver-username}: The username of the player receiving the message
443+
# - {receiver-displayname}: The display name of the player receiving the message (This would be their nickname)
444+
# - {message}: The content of the message being sent
445+
pms-to-discord: "[SocialSpy] {sender-username} -> {receiver-username}: {message}"

0 commit comments

Comments
 (0)