From 649f52b491008812500b70d5e2141b5b9891e907 Mon Sep 17 00:00:00 2001
From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>
Date: Sat, 5 Apr 2025 16:44:33 +0300
Subject: [PATCH] [Feature] Per file attachement size limits (#3090)
* `SendFilesAsync` check file size per attachment instead of combined
* add `AttachmentSizeLimit` to interaction entites
---
.../Entities/Interactions/IDiscordInteraction.cs | 5 +++++
src/Discord.Net.Rest/API/Common/Interaction.cs | 3 +++
.../Entities/Channels/ChannelHelper.cs | 13 ++++---------
.../Entities/Interactions/RestInteraction.cs | 5 +++++
.../Entities/Interaction/SocketInteraction.cs | 5 +++++
5 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs b/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs
index 5b04de59..39b3ea03 100644
--- a/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs
+++ b/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs
@@ -111,6 +111,11 @@ namespace Discord
///
GuildPermissions Permissions { get; }
+ ///
+ /// Gets the attachment size limit in bytes.
+ ///
+ ulong AttachmentSizeLimit { get; }
+
///
/// Responds to an Interaction with type .
///
diff --git a/src/Discord.Net.Rest/API/Common/Interaction.cs b/src/Discord.Net.Rest/API/Common/Interaction.cs
index 8ca83991..8a3769a0 100644
--- a/src/Discord.Net.Rest/API/Common/Interaction.cs
+++ b/src/Discord.Net.Rest/API/Common/Interaction.cs
@@ -60,4 +60,7 @@ internal class Interaction
[JsonProperty("app_permissions")]
public GuildPermission ApplicationPermissions { get; set; }
+
+ [JsonProperty("attachment_size_limit")]
+ public ulong AttachmentSizeLimit { get; set; }
}
diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
index 3fd65d02..7a76732d 100644
--- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
@@ -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
diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs
index 551eb9ab..f2f2404b 100644
--- a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs
+++ b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs
@@ -102,6 +102,9 @@ namespace Discord.Rest
///
public IReadOnlyDictionary IntegrationOwners { get; private set; }
+ ///
+ 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)
diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs
index 24aecb47..685d57ff 100644
--- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs
+++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs
@@ -90,6 +90,9 @@ namespace Discord.WebSocket
///
public IReadOnlyCollection Entitlements { get; private set; }
+ ///
+ public ulong AttachmentSizeLimit { get; private set; }
+
///
public IReadOnlyDictionary IntegrationOwners { get; private set; }
@@ -194,6 +197,8 @@ namespace Discord.WebSocket
_ => null
};
}
+
+ AttachmentSizeLimit = model.AttachmentSizeLimit;
}
///