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; get => _components;
set set
{ {
if (value == null) _components = value ?? throw new ArgumentNullException(nameof(value), $"{nameof(Components)} cannot be 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
};
} }
} }
@@ -48,7 +40,7 @@ public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponent
/// </summary> /// </summary>
public ActionRowBuilder(params IMessageComponentBuilder[] components) public ActionRowBuilder(params IMessageComponentBuilder[] components)
{ {
Components = components?.ToList(); Components = components?.ToList() ?? [];
} }
/// <summary> /// <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> /// <returns>A <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/></returns>
public ActionRowComponent Build() 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()); return new ActionRowComponent(_components.Select(x => x.Build()).ToList());
} }
IMessageComponent IMessageComponentBuilder.Build() => Build(); IMessageComponent IMessageComponentBuilder.Build() => Build();
@@ -231,10 +226,12 @@ public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponent
} }
} }
/// <inheritdoc />
IComponentContainer IComponentContainer.AddComponent(IMessageComponentBuilder component) => AddComponent(component); IComponentContainer IComponentContainer.AddComponent(IMessageComponentBuilder component) => AddComponent(component);
/// <inheritdoc />
IComponentContainer IComponentContainer.AddComponents(params IMessageComponentBuilder[] components) => AddComponents(components); IComponentContainer IComponentContainer.AddComponents(params IMessageComponentBuilder[] components) => AddComponents(components);
/// <inheritdoc />
IComponentContainer IComponentContainer.WithComponents(IEnumerable<IMessageComponentBuilder> components) => WithComponents(components); IComponentContainer IComponentContainer.WithComponents(IEnumerable<IMessageComponentBuilder> components) => WithComponents(components);
} }

View File

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

View File

@@ -21,14 +21,14 @@ public class ContainerComponent : IMessageComponent
/// <summary> /// <summary>
/// Gets the accent color of this container. /// Gets the accent color of this container.
/// </summary> /// </summary>
public uint? AccentColor { get; } public Color? AccentColor { get; }
/// <summary> /// <summary>
/// Gets whether this container is a spoiler. /// Gets whether this container is a spoiler.
/// </summary> /// </summary>
public bool? IsSpoiler { get; } 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; Components = components;
AccentColor = accentColor; AccentColor = accentColor;

View File

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