[Feature] Per file attachement size limits (#3090)

* `SendFilesAsync` check file size per attachment instead of combined

* add `AttachmentSizeLimit` to interaction entites
This commit is contained in:
Mihail Gribkov
2025-04-05 16:44:33 +03:00
committed by GitHub
parent fb90701149
commit 649f52b491
5 changed files with 22 additions and 9 deletions

View File

@@ -111,6 +111,11 @@ namespace Discord
/// </summary>
GuildPermissions Permissions { get; }
/// <summary>
/// Gets the attachment size limit in bytes.
/// </summary>
ulong AttachmentSizeLimit { get; }
/// <summary>
/// Responds to an Interaction with type <see cref="InteractionResponseType.ChannelMessageWithSource"/>.
/// </summary>

View File

@@ -60,4 +60,7 @@ internal class Interaction
[JsonProperty("app_permissions")]
public GuildPermission ApplicationPermissions { get; set; }
[JsonProperty("attachment_size_limit")]
public ulong AttachmentSizeLimit { get; set; }
}

View File

@@ -406,16 +406,11 @@ namespace Discord.Rest
foreach (var attachment in attachments)
{
Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
}
if (channel is ITextChannel guildTextChannel)
{
ulong contentSize = (ulong)attachments.Where(x => x.Stream.CanSeek).Sum(x => x.Stream.Length);
if (contentSize > guildTextChannel.Guild.MaxUploadLimit)
{
throw new ArgumentOutOfRangeException(nameof(attachments), $"Collective file size exceeds the max file size of {guildTextChannel.Guild.MaxUploadLimit} bytes in that guild!");
}
if (channel is ITextChannel guildTextChannel &&
attachment.Stream.CanSeek &&
(ulong)attachment.Stream.Length > guildTextChannel.Guild.MaxUploadLimit)
throw new ArgumentOutOfRangeException(nameof(attachments), $"File size exceeds the max file size of {guildTextChannel.Guild.MaxUploadLimit} bytes in that guild!");
}
// check that user flag and user Id list are exclusive, same with role flag and role Id list

View File

@@ -102,6 +102,9 @@ namespace Discord.Rest
/// <inheritdoc/>
public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; private set; }
/// <inheritdoc/>
public ulong AttachmentSizeLimit { get; private set; }
internal RestInteraction(BaseDiscordClient discord, ulong id)
: base(discord, id)
{
@@ -248,6 +251,8 @@ namespace Discord.Rest
: null;
Permissions = new GuildPermissions((ulong)model.ApplicationPermissions);
AttachmentSizeLimit = model.AttachmentSizeLimit;
}
internal string SerializePayload(object payload)

View File

@@ -90,6 +90,9 @@ namespace Discord.WebSocket
/// <inheritdoc cref="IDiscordInteraction.Entitlements" />
public IReadOnlyCollection<RestEntitlement> Entitlements { get; private set; }
/// <inheritdoc/>
public ulong AttachmentSizeLimit { get; private set; }
/// <inheritdoc/>
public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; private set; }
@@ -194,6 +197,8 @@ namespace Discord.WebSocket
_ => null
};
}
AttachmentSizeLimit = model.AttachmentSizeLimit;
}
/// <summary>