Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds full support for socialspy in EssentialsDiscord #5620

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
10 changes: 10 additions & 0 deletions EssentialsDiscord/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Loading