Skip to content

Commit 7745558

Browse files
authored
Add MaxUploadLimit to guilds (#2001)
1 parent 2a416a3 commit 7745558

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

src/Discord.Net.Core/Entities/Guilds/IGuild.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ public interface IGuild : IDeletable, ISnowflakeEntity
354354
/// </returns>
355355
bool IsBoostProgressBarEnabled { get; }
356356

357+
/// <summary>
358+
/// Gets the upload limit in bytes for this guild. This number is dependent on the guild's boost status.
359+
/// </summary>
360+
ulong MaxUploadLimit { get; }
361+
357362
/// <summary>
358363
/// Modifies this guild.
359364
/// </summary>

src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ public static async Task<RestUserMessage> SendFilesAsync(IMessageChannel channel
372372
Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
373373
}
374374

375+
if (channel is ITextChannel guildTextChannel)
376+
{
377+
var contentSize = (ulong)attachments.Sum(x => x.Stream.Length);
378+
379+
if (contentSize > guildTextChannel.Guild.MaxUploadLimit)
380+
{
381+
throw new ArgumentOutOfRangeException(nameof(attachments), $"Collective file size exceeds the max file size of {guildTextChannel.Guild.MaxUploadLimit} bytes in that guild!");
382+
}
383+
}
384+
375385
// check that user flag and user Id list are exclusive, same with role flag and role Id list
376386
if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
377387
{

src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public static async Task DeleteAsync(IGuild guild, BaseDiscordClient client,
124124
{
125125
await client.ApiClient.DeleteGuildAsync(guild.Id, options).ConfigureAwait(false);
126126
}
127+
public static ulong GetUploadLimit(IGuild guild)
128+
{
129+
return guild.PremiumTier switch
130+
{
131+
PremiumTier.Tier2 => 50ul * 1000000,
132+
PremiumTier.Tier3 => 100ul * 1000000,
133+
_ => 8ul * 1000000
134+
};
135+
}
127136
#endregion
128137

129138
#region Bans

src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public int MaxBitrate
9999
};
100100
}
101101
}
102+
/// <inheritdoc/>
103+
public ulong MaxUploadLimit
104+
=> GuildHelper.GetUploadLimit(this);
102105
/// <inheritdoc />
103106
public NsfwLevel NsfwLevel { get; private set; }
104107
/// <inheritdoc />

src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public int MaxBitrate
201201
};
202202
}
203203
}
204+
/// <inheritdoc/>
205+
public ulong MaxUploadLimit
206+
=> GuildHelper.GetUploadLimit(this);
204207
/// <summary>
205208
/// Gets the widget channel (i.e. the channel set in the guild's widget settings) in this guild.
206209
/// </summary>

0 commit comments

Comments
 (0)