From 58e2c18511409d9a3efe7671ab2bd71deef5c271 Mon Sep 17 00:00:00 2001 From: Jonathan Hiles Date: Fri, 10 May 2024 20:30:44 +1000 Subject: [PATCH] feat(chat): add `entries[].discord.purgeLinks` option to prevent links from being sent to Discord (#135) Refs: #134 (cherry picked from commit 046e148418704a2f7cb815b7538da554c8669850) --- .../minecraft/ServerMessageCallback.java | 18 ++++++++++++++++-- .../minecord/impl/chat/config/ChatConfig.java | 4 ++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/minecraft/ServerMessageCallback.java b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/minecraft/ServerMessageCallback.java index 2c98697..e5bcd5e 100644 --- a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/minecraft/ServerMessageCallback.java +++ b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/callback/minecraft/ServerMessageCallback.java @@ -6,6 +6,7 @@ import eu.pb4.placeholders.api.PlaceholderContext; import eu.pb4.placeholders.api.PlaceholderHandler; import org.jetbrains.annotations.Nullable; +import static net.dv8tion.jda.api.EmbedBuilder.URL_PATTERN; import net.minecraft.network.message.MessageType; import net.minecraft.network.message.SignedMessage; @@ -20,6 +21,7 @@ import me.axieum.mcmod.minecord.api.chat.event.minecraft.TellRawMessageCallback; import me.axieum.mcmod.minecord.api.util.PlaceholdersExt; import me.axieum.mcmod.minecord.api.util.StringUtils; +import me.axieum.mcmod.minecord.impl.chat.config.ChatConfig; import me.axieum.mcmod.minecord.impl.chat.util.DiscordDispatcher; import static me.axieum.mcmod.minecord.api.util.PlaceholdersExt.string; @@ -50,7 +52,7 @@ public void onChatMessage( DiscordDispatcher.dispatch( (embed, entry) -> embed.setContent( - PlaceholdersExt.parseString(entry.discord.chatNode, ctx, placeholders) + replaceLinks(PlaceholdersExt.parseString(entry.discord.chatNode, ctx, placeholders), entry) ), entry -> entry.discord.chat != null && entry.hasWorld(player.getWorld()) ); @@ -102,7 +104,7 @@ public void onEmoteCommandMessage(SignedMessage message, ServerCommandSource sou DiscordDispatcher.dispatch( (embed, entry) -> embed.setContent( - PlaceholdersExt.parseString(entry.discord.emoteNode, ctx, placeholders) + replaceLinks(PlaceholdersExt.parseString(entry.discord.emoteNode, ctx, placeholders), entry) ), entry -> entry.discord.emote != null && (player == null || entry.hasWorld(source.getWorld())) ); @@ -172,4 +174,16 @@ public void onTellRawCommandMessage(Text message, ServerCommandSource source) ); }); } + + /** + * Wrapper method for replacing URLs found in messages if configured to disallow links. + * + * @param text formatted message + * @param entry chat config entry + * @return sanitised text + */ + private static String replaceLinks(String text, ChatConfig.ChatEntrySchema entry) + { + return entry.discord.purgeLinks ? text : URL_PATTERN.matcher(text).replaceAll(" … "); + } } diff --git a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java index d98b876..aee4783 100644 --- a/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java +++ b/minecord-chat/src/main/java/me/axieum/mcmod/minecord/impl/chat/config/ChatConfig.java @@ -70,6 +70,10 @@ public static class DiscordSchema /** Pre-parsed 'chat' text node. */ public transient TextNode chatNode; + /** True if players are allowed to send links, possibly leading to image previews. */ + @Comment("True if players are allowed to send links, possibly leading to image previews") + public boolean purgeLinks = true; + /** * A player sent an in-game message via the {@code /me} command. *