Add filtering for which control characters can be sent to Minecraft, add removal of line break spam to default config

This commit is contained in:
Vankka 2023-09-07 17:41:50 +03:00
parent 6c5064b081
commit d111fce37b
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
2 changed files with 9 additions and 1 deletions

View File

@ -51,7 +51,9 @@ public class DiscordToMinecraftChatConfig {
// TODO: more info on regex pairs (String#replaceAll)
@Comment("Regex filters for Discord message contents (this is the %message% part of the \"format\" option)")
@Untranslated(Untranslated.Type.VALUE)
public Map<Pattern, String> contentRegexFilters = new LinkedHashMap<>();
public Map<Pattern, String> contentRegexFilters = new LinkedHashMap<Pattern, String>() {{
put(Pattern.compile("\\n{2,}"), "\n");
}};
@Comment("Users, bots, roles and webhooks to ignore")
public DiscordIgnoresConfig ignores = new DiscordIgnoresConfig();

View File

@ -52,9 +52,14 @@ import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.regex.Pattern;
public class DiscordChatMessageModule extends AbstractModule<DiscordSRV> {
// Filter for ASCII control characters which have no use being displayed, but might be misinterpreted somewhere
// Notably this excludes, 0x09 HT (\t), 0x0A LF (\n), 0x0B VT (\v) and 0x0D CR (\r) (which may be used for text formatting)
private static final Pattern ASCII_CONTROL_FILTER = Pattern.compile("[\\u0000-\\u0008\\u000C\\u000E-\\u001F\\u007F]");
private final Map<String, MessageSend> sends = new ConcurrentHashMap<>();
public DiscordChatMessageModule(DiscordSRV discordSRV) {
@ -182,6 +187,7 @@ public class DiscordChatMessageModule extends AbstractModule<DiscordSRV> {
}
Placeholders message = new Placeholders(event.getContent());
message.replaceAll(ASCII_CONTROL_FILTER, "");
chatConfig.contentRegexFilters.forEach(message::replaceAll);
Component messageComponent = DiscordSRVMinecraftRenderer.getWithContext(guild, chatConfig, () ->