[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> /// </summary>
GuildPermissions Permissions { get; } GuildPermissions Permissions { get; }
/// <summary>
/// Gets the attachment size limit in bytes.
/// </summary>
ulong AttachmentSizeLimit { get; }
/// <summary> /// <summary>
/// Responds to an Interaction with type <see cref="InteractionResponseType.ChannelMessageWithSource"/>. /// Responds to an Interaction with type <see cref="InteractionResponseType.ChannelMessageWithSource"/>.
/// </summary> /// </summary>

View File

@@ -60,4 +60,7 @@ internal class Interaction
[JsonProperty("app_permissions")] [JsonProperty("app_permissions")]
public GuildPermission ApplicationPermissions { get; set; } 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) foreach (var attachment in attachments)
{ {
Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null"); Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
}
if (channel is ITextChannel guildTextChannel) if (channel is ITextChannel guildTextChannel &&
{ attachment.Stream.CanSeek &&
ulong contentSize = (ulong)attachments.Where(x => x.Stream.CanSeek).Sum(x => x.Stream.Length); (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!");
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!");
}
} }
// check that user flag and user Id list are exclusive, same with role flag and role Id list // 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/> /// <inheritdoc/>
public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; private set; } public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; private set; }
/// <inheritdoc/>
public ulong AttachmentSizeLimit { get; private set; }
internal RestInteraction(BaseDiscordClient discord, ulong id) internal RestInteraction(BaseDiscordClient discord, ulong id)
: base(discord, id) : base(discord, id)
{ {
@@ -248,6 +251,8 @@ namespace Discord.Rest
: null; : null;
Permissions = new GuildPermissions((ulong)model.ApplicationPermissions); Permissions = new GuildPermissions((ulong)model.ApplicationPermissions);
AttachmentSizeLimit = model.AttachmentSizeLimit;
} }
internal string SerializePayload(object payload) internal string SerializePayload(object payload)

View File

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