From fed271f888a08cac8ba555bc992a4b76db9c690b Mon Sep 17 00:00:00 2001 From: PauliusIvaskevicius <13413990+PauliusIvaskevicius@users.noreply.github.com> Date: Sat, 11 Nov 2023 19:02:04 +0200 Subject: [PATCH] Fix large messages --- DiscordBot.App/Controllers/FeedsController.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/DiscordBot.App/Controllers/FeedsController.cs b/DiscordBot.App/Controllers/FeedsController.cs index 055dc86..bbd8193 100644 --- a/DiscordBot.App/Controllers/FeedsController.cs +++ b/DiscordBot.App/Controllers/FeedsController.cs @@ -11,6 +11,7 @@ namespace DiscordBot.Controllers; public class FeedsController : ControllerBase { private readonly DiscordService _discordService; + private const int _maxMessageLength = 2000; public FeedsController(DiscordService discordService) { _discordService = discordService; @@ -39,6 +40,27 @@ public async Task PostSend(string channel, string message) _ => throw new Exception("Invalid channel name") }; + if (message.Length > _maxMessageLength) + { + var messageParts = message.Split(" "); + + var currentMessage = string.Empty; + + foreach (var messagePart in messageParts) + { + if (currentMessage.Length + messagePart.Length > _maxMessageLength) + { + await _discordService.Client.SendMessageAsync(discordChannel, currentMessage); + currentMessage = string.Empty; + } + + currentMessage += messagePart + " "; + } + + return; + } + + await _discordService.Client.SendMessageAsync(discordChannel, message); } }