From 7a69a805a356eeb09ca737702471a088e7a3a283 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 15 Oct 2023 23:58:20 +0300 Subject: [PATCH] Ignore empty messages in message forwarding --- .../api/discord/entity/message/SendableDiscordMessage.java | 6 ++++++ .../entity/message/impl/SendableDiscordMessageImpl.java | 5 +++++ .../discord/DiscordChatMessageModule.java | 7 ++++++- .../messageforwarding/game/AbstractGameMessageModule.java | 5 ++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/discordsrv/api/discord/entity/message/SendableDiscordMessage.java b/api/src/main/java/com/discordsrv/api/discord/entity/message/SendableDiscordMessage.java index f44af7a8..84417261 100644 --- a/api/src/main/java/com/discordsrv/api/discord/entity/message/SendableDiscordMessage.java +++ b/api/src/main/java/com/discordsrv/api/discord/entity/message/SendableDiscordMessage.java @@ -141,6 +141,12 @@ public interface SendableDiscordMessage { */ SendableDiscordMessage withReplyingToMessageId(Long replyingToMessageId); + /** + * Checks if this message has any sendable content. + * @return {@code true} if there is no sendable content + */ + boolean isEmpty(); + @SuppressWarnings("UnusedReturnValue") // API interface Builder { diff --git a/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java b/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java index 59169831..90392ea3 100644 --- a/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java +++ b/api/src/main/java/com/discordsrv/api/discord/entity/message/impl/SendableDiscordMessageImpl.java @@ -95,6 +95,11 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage { ); } + @Override + public boolean isEmpty() { + return (content == null || content.isEmpty()) && embeds.isEmpty() && attachments.isEmpty() && actionRows.isEmpty(); + } + @Override public @Nullable String getContent() { return content; diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java index 46cfc7b5..237cf46a 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordChatMessageModule.java @@ -190,8 +190,13 @@ public class DiscordChatMessageModule extends AbstractModule { message.replaceAll(ASCII_CONTROL_FILTER, ""); chatConfig.contentRegexFilters.forEach(message::replaceAll); + String finalMessage = message.toString(); + if (StringUtils.isEmpty(finalMessage)) { + return; + } + Component messageComponent = DiscordSRVMinecraftRenderer.getWithContext(guild, chatConfig, () -> - discordSRV.componentFactory().minecraftSerializer().serialize(message.toString())); + discordSRV.componentFactory().minecraftSerializer().serialize(finalMessage)); GameTextBuilder componentBuilder = discordSRV.componentFactory() .textBuilder(format) diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java index 38b4550d..93bdf869 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/game/AbstractGameMessageModule.java @@ -107,7 +107,7 @@ public abstract class AbstractGameMessageModule { SendableDiscordMessage.Builder format = moduleConfig.format(); - if (format == null) { + if (format == null || format.isEmpty()) { return CompletableFuture.completedFuture(null); } @@ -178,6 +178,9 @@ public abstract class AbstractGameMessageModule, DiscordGuildMessageChannel> futures = new LinkedHashMap<>(); for (DiscordGuildMessageChannel channel : channels) {