Skip to content

Commit 348928a

Browse files
authored
update component limits + add ComponentCount() extension (#3107)
1 parent 05a0acc commit 348928a

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ComponentBuilderV2.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Discord;
77
public class ComponentBuilderV2 : IStaticComponentContainer
88
{
99
/// <summary>
10-
/// Gets the maximum number of components that can be added to this container.
10+
/// Gets the maximum number of components that can be added to a message.
1111
/// </summary>
12-
public const int MaxComponents = 10;
12+
public const int MaxComponents = 40;
1313

1414
private List<IMessageComponentBuilder> _components = new();
1515

@@ -53,8 +53,8 @@ public ComponentBuilderV2 WithComponents(IEnumerable<IMessageComponentBuilder> c
5353
/// <inheritdoc cref="IMessageComponentBuilder.Build" />
5454
public MessageComponent Build()
5555
{
56-
if (_components.Count is 0 or >MaxComponents)
57-
throw new InvalidOperationException($"The number of components must be between 1 and {MaxComponents}.");
56+
Preconditions.AtLeast(Components?.Count ?? 0, 1, nameof(Components.Count), "At least 1 component must be added to this container.");
57+
Preconditions.AtMost(this.ComponentCount(), MaxComponents, nameof(Components.Count), $"A message must contain {MaxComponents} components or less.");
5858

5959
if (_components.Any(x =>
6060
x is not ActionRowBuilder

src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ComponentContainerExtensions.cs

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ namespace Discord;
55

66
public static class ComponentContainerExtensions
77
{
8+
/// <summary>
9+
/// Gets the total number of components in this and all child <see cref="IComponentContainer"/>s combined.
10+
/// </summary>
11+
public static int ComponentCount(this IComponentContainer container)
12+
=> (container.Components?.Count ?? 0)
13+
+ container.Components?
14+
.OfType<IComponentContainer>()
15+
.Sum(x => x.ComponentCount()) ?? 0;
16+
817
/// <summary>
918
/// Adds a <see cref="TextDisplayBuilder"/> to the container.
1019
/// </summary>

src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ContainerBuilder.cs

-8
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace Discord;
77

88
public class ContainerBuilder : IMessageComponentBuilder, IStaticComponentContainer
99
{
10-
/// <summary>
11-
/// The maximum number of components allowed in a container.
12-
/// </summary>
13-
public const int MaxComponents = 10;
14-
1510
/// <inheritdoc />
1611
public ComponentType Type => ComponentType.Container;
1712

@@ -86,9 +81,6 @@ public ContainerBuilder WithComponents(IEnumerable<IMessageComponentBuilder> com
8681
/// <inheritdoc cref="IMessageComponentBuilder.Build"/>
8782
public ContainerComponent Build()
8883
{
89-
if (_components.Count is 0 or > MaxComponents)
90-
throw new InvalidOperationException($"A container must have between 1 and {MaxComponents} components.");
91-
9284
if (_components.Any(x => x
9385
is not ActionRowBuilder
9486
and not TextDisplayBuilder

0 commit comments

Comments
 (0)