Skip to content

Use Color for container AccentColor & fix action row validation #3120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,7 @@ public List<IMessageComponentBuilder> Components
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.");
}
}

Expand All @@ -48,7 +40,7 @@ public List<IMessageComponentBuilder> Components
/// </summary>
public ActionRowBuilder(params IMessageComponentBuilder[] components)
{
Components = components?.ToList();
Components = components?.ToList() ?? [];
}

/// <summary>
Expand Down Expand Up @@ -205,6 +197,9 @@ public ActionRowBuilder WithButton(ButtonBuilder button)
/// <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();
Expand All @@ -231,10 +226,12 @@ internal bool CanTakeComponent(IMessageComponentBuilder component)
}
}


/// <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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<IMessageComponentBuilder> Components
/// <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.
Expand Down Expand Up @@ -59,7 +59,7 @@ public ContainerBuilder(ContainerComponent container)
/// </returns>
public ContainerBuilder WithAccentColor(Color? color)
{
AccentColor = color?.RawValue;
AccentColor = color;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/API/Common/ContainerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ContainerComponent(Discord.ContainerComponent component)
{
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();
}
Expand Down