feat: tweak embed dequeueing logic

We no longer blindly dequeue 5 embeds, we check their length too.
The webhook executor will now send up to 10 embeds OR
embeds totaling less than 6000 characters, whichever is less.
Embeds longer than 6000 characters are discarded to prevent errors.
We also check for an empty request body in SendLogAsync and bail to prevent 400s.
This commit is contained in:
sam 2024-10-12 23:47:18 +02:00
parent f524afb05b
commit d221441c10
2 changed files with 62 additions and 10 deletions

View file

@ -1,15 +1,11 @@
using System.Drawing;
using Catalogger.Backend.Cache.InMemoryCache;
using Humanizer;
using OneOf;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Objects;
using Remora.Discord.Commands.Contexts;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Commands.Services;
using Remora.Discord.Extensions.Embeds;
using Remora.Rest.Core;
using Remora.Results;
@ -165,5 +161,17 @@ public static class DiscordExtensions
: $"*(unknown user {actionData.ModeratorId}) <@{actionData.ModeratorId}>*";
}
public static int TextLength(this IEmbed embed)
{
var length = OptionalStringLength(embed.Description) + OptionalStringLength(embed.Title);
var fieldLength = (embed.Fields.OrDefault() ?? [])
.Select(f => f.Name.Length + f.Value.Length)
.Sum();
return length + fieldLength;
}
private static int OptionalStringLength(Optional<string> s) => s.OrDefault()?.Length ?? 0;
public class DiscordRestException(string message) : Exception(message);
}