Skip to content

Commit

Permalink
feat(chat): add entries[].discord.purgeLinks option to prevent link…
Browse files Browse the repository at this point in the history
…s from being sent to Discord (#135)

Refs: #134
(cherry picked from commit 046e148)
  • Loading branch information
axieum committed May 10, 2024
1 parent dae9e53 commit 58e2c18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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())
);
Expand Down Expand Up @@ -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()))
);
Expand Down Expand Up @@ -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(" … ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 58e2c18

Please sign in to comment.