use Color for container AccentColor & fix action row validation (#3120)

This commit is contained in:
Mihail Gribkov
2025-05-09 15:01:11 +03:00
committed by GitHub
parent b72938028f
commit c888c84752
4 changed files with 13 additions and 16 deletions

View File

@@ -31,15 +31,7 @@ public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponent
get => _components;
set
{
if (value == null)
throw new ArgumentNullException(nameof(value), $"{nameof(Components)} cannot be null.");
_components = value.Count switch
{
0 => throw new ArgumentOutOfRangeException(nameof(value), "There must be at least 1 component in a row."),
> MaxChildCount => throw new ArgumentOutOfRangeException(nameof(value), $"Action row can only contain {MaxChildCount} child components!"),
_ => value
};
_components = value ?? throw new ArgumentNullException(nameof(value), $"{nameof(Components)} cannot be null.");
}
}
@@ -48,7 +40,7 @@ public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponent
/// </summary>
public ActionRowBuilder(params IMessageComponentBuilder[] components)
{
Components = components?.ToList();
Components = components?.ToList() ?? [];
}
/// <summary>
@@ -205,6 +197,9 @@ public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponent
/// <returns>A <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/></returns>
public ActionRowComponent Build()
{
Preconditions.AtLeast(Components.Count, 1, nameof(Components), "There must be at least 1 component in a row.");
Preconditions.AtMost(Components.Count, MaxChildCount, nameof(Components), $"Action row can only contain {MaxChildCount} child components!");
return new ActionRowComponent(_components.Select(x => x.Build()).ToList());
}
IMessageComponent IMessageComponentBuilder.Build() => Build();
@@ -231,10 +226,12 @@ public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponent
}
}
/// <inheritdoc />
IComponentContainer IComponentContainer.AddComponent(IMessageComponentBuilder component) => AddComponent(component);
/// <inheritdoc />
IComponentContainer IComponentContainer.AddComponents(params IMessageComponentBuilder[] components) => AddComponents(components);
/// <inheritdoc />
IComponentContainer IComponentContainer.WithComponents(IEnumerable<IMessageComponentBuilder> components) => WithComponents(components);
}

View File

@@ -25,7 +25,7 @@ public class ContainerBuilder : IMessageComponentBuilder, IStaticComponentContai
/// <summary>
/// Gets or sets the accent color of this container.
/// </summary>
public uint? AccentColor { get; set; }
public Color? AccentColor { get; set; }
/// <summary>
/// Gets or sets whether this container is a spoiler.
@@ -59,7 +59,7 @@ public class ContainerBuilder : IMessageComponentBuilder, IStaticComponentContai
/// </returns>
public ContainerBuilder WithAccentColor(Color? color)
{
AccentColor = color?.RawValue;
AccentColor = color;
return this;
}

View File

@@ -21,14 +21,14 @@ public class ContainerComponent : IMessageComponent
/// <summary>
/// Gets the accent color of this container.
/// </summary>
public uint? AccentColor { get; }
public Color? AccentColor { get; }
/// <summary>
/// Gets whether this container is a spoiler.
/// </summary>
public bool? IsSpoiler { get; }
internal ContainerComponent(IReadOnlyCollection<IMessageComponent> components, uint? accentColor, bool? isSpoiler, int? id = null)
internal ContainerComponent(IReadOnlyCollection<IMessageComponent> components, Color? accentColor, bool? isSpoiler, int? id = null)
{
Components = components;
AccentColor = accentColor;

View File

@@ -27,7 +27,7 @@ internal class ContainerComponent : IMessageComponent
{
Type = component.Type;
Id = component.Id ?? Optional<int>.Unspecified;
AccentColor = component.AccentColor ?? Optional<uint?>.Unspecified;
AccentColor = component.AccentColor?.RawValue ?? Optional<uint?>.Unspecified;
IsSpoiler = component.IsSpoiler ?? Optional<bool>.Unspecified;
Components = component.Components.Select(x => x.ToModel()).ToArray();
}