Skip to content

Commit 5ce378e

Browse files
committed
label builder
1 parent 56d56d9 commit 5ce378e

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ButtonBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Discord;
99
/// </summary>
1010
public class ButtonBuilder : IInteractableComponentBuilder
1111
{
12+
/// <inheritdoc />
1213
public ComponentType Type => ComponentType.Button;
1314

1415
/// <summary>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Immutable;
3+
using System.Linq;
4+
5+
namespace Discord;
6+
7+
public class LabelBuilder : IMessageComponentBuilder
8+
{
9+
/// <inheritdoc cref="IComponentContainer.SupportedComponentTypes"/>
10+
public ImmutableArray<ComponentType> SupportedComponentTypes { get; } =
11+
[
12+
ComponentType.SelectMenu,
13+
ComponentType.TextInput,
14+
];
15+
16+
/// <summary>
17+
/// The maximum length of the label.
18+
/// </summary>
19+
public const int MaxLabelLength = 100;
20+
21+
/// <summary>
22+
/// The maximum length of the description.
23+
/// </summary>
24+
public const int MaxDescriptionLength = 69420; // TODO: set to the real limit
25+
26+
/// <inheritdoc />
27+
public ComponentType Type => ComponentType.Label;
28+
29+
/// <inheritdoc />
30+
public int? Id { get; set; }
31+
32+
/// <summary>
33+
///
34+
/// </summary>
35+
public string Label { get; set; }
36+
37+
/// <summary>
38+
///
39+
/// </summary>
40+
public string Description { get; set; }
41+
42+
public IMessageComponentBuilder Component { get; set; }
43+
44+
/// <summary>
45+
/// Initializes a new <see cref="LabelBuilder"/>.
46+
/// </summary>
47+
public LabelBuilder() { }
48+
49+
/// <summary>
50+
/// Initializes a new <see cref="LabelBuilder"/> with the specified content.
51+
/// </summary>
52+
public LabelBuilder(string label, IMessageComponentBuilder component, string description = null, int? id = null)
53+
{
54+
Id = id;
55+
Label = label;
56+
Component = component;
57+
Description = description;
58+
}
59+
60+
/// <summary>
61+
/// Initializes a new <see cref="LabelBuilder"/> from existing component.
62+
/// </summary>
63+
public LabelBuilder(LabelComponent label)
64+
{
65+
Label = label.Label;
66+
Description = label.Description;
67+
Id = label.Id;
68+
Component = label.Component.ToBuilder();
69+
}
70+
71+
public LabelComponent Build()
72+
{
73+
Preconditions.NotNullOrWhitespace(Label, nameof(Label));
74+
Preconditions.AtMost(Label.Length, MaxLabelLength, nameof(Label));
75+
76+
Preconditions.AtMost(Description?.Length ?? 0, MaxDescriptionLength, nameof(Description));
77+
78+
Preconditions.NotNull(Component, nameof(Component));
79+
80+
if (SupportedComponentTypes.All(x => Component.Type != x))
81+
throw new InvalidOperationException($"Component can only be {nameof(SelectMenuBuilder)} or {nameof(TextInputBuilder)}.");
82+
83+
return new LabelComponent(Id, Label, Description, Component.Build());
84+
}
85+
86+
/// <inheritdoc />
87+
IMessageComponent IMessageComponentBuilder.Build() => Build();
88+
}

src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/MediaGalleryBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MediaGalleryBuilder : IMessageComponentBuilder
1818
/// <inheritdoc/>
1919
public int? Id { get; set; }
2020

21-
private List<MediaGalleryItemProperties> _items = new();
21+
private List<MediaGalleryItemProperties> _items = [];
2222

2323
/// <summary>
2424
/// Initializes a new instance of the <see cref="MediaGalleryBuilder"/>.

0 commit comments

Comments
 (0)