From b89aeda198891d4f4a4e17672574eb2905e62eb7 Mon Sep 17 00:00:00 2001 From: Milutinke Date: Sat, 22 Oct 2022 14:21:35 +0200 Subject: [PATCH] Fix a crash with Discord Bridge when getting an empty message --- MinecraftClient/ChatBots/DiscordBridge.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/MinecraftClient/ChatBots/DiscordBridge.cs b/MinecraftClient/ChatBots/DiscordBridge.cs index d5afe5e6..1d83cf5d 100644 --- a/MinecraftClient/ChatBots/DiscordBridge.cs +++ b/MinecraftClient/ChatBots/DiscordBridge.cs @@ -149,6 +149,10 @@ namespace MinecraftClient.ChatBots text = GetVerbatim(text).Trim(); + // Stop the crash when an empty text is recived somehow + if (string.IsNullOrEmpty(text)) + return; + string message = ""; string username = ""; bool teleportRequest = false; @@ -185,7 +189,7 @@ namespace MinecraftClient.ChatBots public void SendMessage(string message) { - if (!CanSendMessages()) + if (!CanSendMessages() || string.IsNullOrEmpty(message)) return; _client!.SendMessageAsync(_channel, message).Wait(); @@ -215,7 +219,10 @@ namespace MinecraftClient.ChatBots using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { filePath = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..]; - var messageBuilder = new DiscordMessageBuilder().WithContent(text); + var messageBuilder = new DiscordMessageBuilder(); + + if (text != null) + messageBuilder.WithContent(text); messageBuilder.WithFiles(new Dictionary() { { $"attachment://{filePath}", fs } }); @@ -305,6 +312,9 @@ namespace MinecraftClient.ChatBots string message = e.Message.Content.Trim(); + if (string.IsNullOrEmpty(message) || string.IsNullOrWhiteSpace(message)) + return; + if (bridgeDirection == BridgeDirection.Discord) { if (!message.StartsWith(".dscbridge"))