Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix large messages #13

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions DiscordBot.App/Controllers/FeedsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading