update component limits + add ComponentCount() extension (#3107)
This commit is contained in:
@@ -7,9 +7,9 @@ namespace Discord;
|
||||
public class ComponentBuilderV2 : IStaticComponentContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the maximum number of components that can be added to this container.
|
||||
/// Gets the maximum number of components that can be added to a message.
|
||||
/// </summary>
|
||||
public const int MaxComponents = 10;
|
||||
public const int MaxComponents = 40;
|
||||
|
||||
private List<IMessageComponentBuilder> _components = new();
|
||||
|
||||
@@ -53,8 +53,8 @@ public class ComponentBuilderV2 : IStaticComponentContainer
|
||||
/// <inheritdoc cref="IMessageComponentBuilder.Build" />
|
||||
public MessageComponent Build()
|
||||
{
|
||||
if (_components.Count is 0 or >MaxComponents)
|
||||
throw new InvalidOperationException($"The number of components must be between 1 and {MaxComponents}.");
|
||||
Preconditions.AtLeast(Components?.Count ?? 0, 1, nameof(Components.Count), "At least 1 component must be added to this container.");
|
||||
Preconditions.AtMost(this.ComponentCount(), MaxComponents, nameof(Components.Count), $"A message must contain {MaxComponents} components or less.");
|
||||
|
||||
if (_components.Any(x =>
|
||||
x is not ActionRowBuilder
|
||||
|
||||
@@ -5,6 +5,15 @@ namespace Discord;
|
||||
|
||||
public static class ComponentContainerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the total number of components in this and all child <see cref="IComponentContainer"/>s combined.
|
||||
/// </summary>
|
||||
public static int ComponentCount(this IComponentContainer container)
|
||||
=> (container.Components?.Count ?? 0)
|
||||
+ container.Components?
|
||||
.OfType<IComponentContainer>()
|
||||
.Sum(x => x.ComponentCount()) ?? 0;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="TextDisplayBuilder"/> to the container.
|
||||
/// </summary>
|
||||
|
||||
@@ -7,11 +7,6 @@ namespace Discord;
|
||||
|
||||
public class ContainerBuilder : IMessageComponentBuilder, IStaticComponentContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum number of components allowed in a container.
|
||||
/// </summary>
|
||||
public const int MaxComponents = 10;
|
||||
|
||||
/// <inheritdoc />
|
||||
public ComponentType Type => ComponentType.Container;
|
||||
|
||||
@@ -86,9 +81,6 @@ public class ContainerBuilder : IMessageComponentBuilder, IStaticComponentContai
|
||||
/// <inheritdoc cref="IMessageComponentBuilder.Build"/>
|
||||
public ContainerComponent Build()
|
||||
{
|
||||
if (_components.Count is 0 or > MaxComponents)
|
||||
throw new InvalidOperationException($"A container must have between 1 and {MaxComponents} components.");
|
||||
|
||||
if (_components.Any(x => x
|
||||
is not ActionRowBuilder
|
||||
and not TextDisplayBuilder
|
||||
|
||||
Reference in New Issue
Block a user