|
| 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 | +} |
0 commit comments