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