This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
ComponentBuilder.cs
907 lines (806 loc) · 32.5 KB
/
ComponentBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
using System;
using System.Collections.Generic;
using System.Linq;
namespace Discord
{
/// <summary>
/// Represents a builder for creating a <see cref="MessageComponent"/>.
/// </summary>
public class ComponentBuilder
{
/// <summary>
/// The max length of a <see cref="ButtonComponent.Label"/>.
/// </summary>
public const int MaxLabelLength = 80;
/// <summary>
/// The max length of a <see cref="ButtonComponent.CustomId"/>.
/// </summary>
public const int MaxCustomIdLength = 100;
/// <summary>
/// The max amount of rows a message can have.
/// </summary>
public const int MaxActionRowCount = 5;
/// <summary>
/// Gets or sets the Action Rows for this Component Builder.
/// </summary>
public List<ActionRowBuilder> ActionRows
{
get => _actionRows;
set
{
if (value == null)
throw new ArgumentNullException(paramName: nameof(ActionRows), message: "Cannot set an component builder's components collection to null.");
if (value.Count > MaxActionRowCount)
throw new ArgumentException(message: $"Action row count must be less than or equal to {MaxActionRowCount}.", paramName: nameof(ActionRows));
_actionRows = value;
}
}
private List<ActionRowBuilder> _actionRows { get; set; }
/// <summary>
/// Adds a <see cref="SelectMenuBuilder"/> to the first row, if the first row cannot
/// accept the component then it will add it to a row that can
/// </summary>
/// <param name="label">The label of the menu.</param>
/// <param name="customId">The custom id of the menu.</param>
/// <param name="options">The options of the menu.</param>
/// <param name="placeholder">The placeholder of the menu.</param>
/// <param name="minValues">The min values of the placeholder.</param>
/// <param name="maxValues">The max values of the placeholder.</param>
/// <param name="disabled">Whether or not the menu is disabled.</param>
/// <param name="row">The row to add the menu to.</param>
/// <returns></returns>
public ComponentBuilder WithSelectMenu(string label, string customId, List<SelectMenuOptionBuilder> options,
string placeholder = null, int minValues = 1, int maxValues = 1, bool disabled = false, int row = 0)
{
return WithSelectMenu(new SelectMenuBuilder()
.WithCustomId(customId)
.WithOptions(options)
.WithPlaceholder(placeholder)
.WithMaxValues(maxValues)
.WithMinValues(minValues)
.WithDisabled(disabled),
row);
}
/// <summary>
/// Adds a <see cref="SelectMenuBuilder"/> to the first row, if the first row cannot
/// accept the component then it will add it to a row that can
/// </summary>
/// <param name="menu">The menu to add</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithSelectMenu(SelectMenuBuilder menu)
=> WithSelectMenu(menu, 0);
/// <summary>
/// Adds a <see cref="SelectMenuBuilder"/> to the current builder at the specific row.
/// </summary>
/// <param name="menu">The menu to add.</param>
/// <param name="row">The row to attempt to add this component on.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithSelectMenu(SelectMenuBuilder menu, int row)
{
Preconditions.LessThan(row, 5, nameof(row));
var builtMenu = menu.Build();
if (_actionRows == null)
{
_actionRows = new List<ActionRowBuilder>();
_actionRows.Add(new ActionRowBuilder().WithComponent(builtMenu));
}
else
{
if (_actionRows.Count == row)
_actionRows.Add(new ActionRowBuilder().WithComponent(builtMenu));
else
{
ActionRowBuilder actionRow = null;
if (_actionRows.Count > row)
actionRow = _actionRows.ElementAt(row);
else
{
actionRow = new ActionRowBuilder();
_actionRows.Add(actionRow);
}
if (actionRow.CanTakeComponent(builtMenu))
actionRow.WithComponent(builtMenu);
else if (row < 5)
WithSelectMenu(menu, row + 1);
else
throw new ArgumentOutOfRangeException($"There is no more room to add a {nameof(builtMenu)}");
}
}
return this;
}
/// <summary>
/// Adds a button to the specified row.
/// </summary>
/// <param name="label">The label text for the newly added button.</param>
/// <param name="style">The style of this newly added button.</param>
/// <param name="emote">A <see cref="IEmote"/> to be used with this button.</param>
/// <param name="customId">The custom id of the newly added button.</param>
/// <param name="url">A URL to be used only if the <see cref="ButtonStyle"/> is a Link.</param>
/// <param name="disabled">Whether or not the newly created button is disabled.</param>
/// <param name="row">The row the button should be placed on.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(
string label,
string customId,
ButtonStyle style = ButtonStyle.Primary,
IEmote emote = null,
string url = null,
bool disabled = false,
int row = 0)
{
var button = new ButtonBuilder()
.WithLabel(label)
.WithStyle(style)
.WithEmote(emote)
.WithCustomId(customId)
.WithUrl(url)
.WithDisabled(disabled);
return this.WithButton(button, row);
}
/// <summary>
/// Adds a button to the first row.
/// </summary>
/// <param name="button">The button to add to the first row.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(ButtonBuilder button)
=> this.WithButton(button, 0);
/// <summary>
/// Adds a button to the specified row.
/// </summary>
/// <param name="button">The button to add.</param>
/// <param name="row">The row to add the button.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(ButtonBuilder button, int row = 0)
{
Preconditions.LessThan(row, 5, nameof(row));
var builtButton = button.Build();
if (_actionRows == null)
{
_actionRows = new List<ActionRowBuilder>();
_actionRows.Add(new ActionRowBuilder().WithComponent(builtButton));
}
else
{
if (_actionRows.Count == row)
_actionRows.Add(new ActionRowBuilder().WithComponent(builtButton));
else
{
ActionRowBuilder actionRow = null;
if(_actionRows.Count > row)
actionRow = _actionRows.ElementAt(row);
else
{
actionRow = new ActionRowBuilder();
_actionRows.Add(actionRow);
}
if (actionRow.CanTakeComponent(builtButton))
actionRow.WithComponent(builtButton);
else if (row < 5)
WithButton(button, row + 1);
else
throw new ArgumentOutOfRangeException($"There is no more room to add a {nameof(button)}");
}
}
return this;
}
/// <summary>
/// Builds this builder into a <see cref="MessageComponent"/> used to send your components.
/// </summary>
/// <returns>A <see cref="MessageComponent"/> that can be sent with <see cref="IMessageChannel.SendMessageAsync(string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent)"/></returns>
public MessageComponent Build()
{
if (this._actionRows != null)
return new MessageComponent(this._actionRows.Select(x => x.Build()).ToList());
else
return MessageComponent.Empty;
}
}
/// <summary>
/// Represents a class used to build Action rows.
/// </summary>
public class ActionRowBuilder
{
/// <summary>
/// The max amount of child components this row can hold.
/// </summary>
public const int MaxChildCount = 5;
/// <summary>
/// Gets or sets the components inside this row.
/// </summary>
public List<IMessageComponent> Components
{
get => _components;
set
{
if (value != null)
if (value.Count > MaxChildCount)
throw new ArgumentException(message: $"Action row can only contain {MaxChildCount} child components!", paramName: nameof(Components));
_components = value;
}
}
private List<IMessageComponent> _components { get; set; } = new List<IMessageComponent>();
/// <summary>
/// Adds a list of components to the current row.
/// </summary>
/// <param name="components">The list of components to add.</param>
/// <returns>The current builder.</returns>
public ActionRowBuilder WithComponents(List<IMessageComponent> components)
{
this.Components = components;
return this;
}
/// <summary>
/// Adds a component at the end of the current row.
/// </summary>
/// <param name="component">The component to add.</param>
/// <returns>The current builder.</returns>
public ActionRowBuilder WithComponent(IMessageComponent component)
{
if (this.Components == null)
this.Components = new List<IMessageComponent>();
this.Components.Add(component);
return this;
}
/// <summary>
/// Builds the current builder to a <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/>
/// </summary>
/// <returns>A <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/></returns>
/// <exception cref="ArgumentNullException"><see cref="Components"/> cannot be null.</exception>
/// <exception cref="ArgumentException">There must be at least 1 component in a row.</exception>
public ActionRowComponent Build()
{
if (this.Components == null)
throw new ArgumentNullException($"{nameof(Components)} cannot be null!");
if (this.Components.Count == 0)
throw new ArgumentException("There must be at least 1 component in a row");
return new ActionRowComponent(this._components);
}
internal bool CanTakeComponent(IMessageComponent component)
{
switch (component.Type)
{
case ComponentType.ActionRow:
return false;
case ComponentType.Button:
if (this.Components.Any(x => x.Type == ComponentType.SelectMenu))
return false;
else
return this.Components.Count < 5;
case ComponentType.SelectMenu:
return this.Components.Count == 0;
default:
return false;
}
}
}
/// <summary>
/// Represents a class used to build <see cref="ButtonComponent"/>'s.
/// </summary>
public class ButtonBuilder
{
/// <summary>
/// Gets or sets the label of the current button.
/// </summary>
public string Label
{
get => _label;
set
{
if (value != null)
if (value.Length > ComponentBuilder.MaxLabelLength)
throw new ArgumentException(message: $"Button label must be {ComponentBuilder.MaxLabelLength} characters or less!", paramName: nameof(Label));
_label = value;
}
}
/// <summary>
/// Gets or sets the custom id of the current button.
/// </summary>
public string CustomId
{
get => _customId;
set
{
if (value != null)
if (value.Length > ComponentBuilder.MaxCustomIdLength)
throw new ArgumentException(message: $"Custom Id must be {ComponentBuilder.MaxCustomIdLength} characters or less!", paramName: nameof(CustomId));
_customId = value;
}
}
/// <summary>
/// Gets or sets the <see cref="ButtonStyle"/> of the current button.
/// </summary>
public ButtonStyle Style { get; set; }
/// <summary>
/// Gets or sets the <see cref="IEmote"/> of the current button.
/// </summary>
public IEmote Emote { get; set; }
/// <summary>
/// Gets or sets the url of the current button.
/// </summary>
public string Url { get; set; }
/// <summary>
/// Gets or sets whether the current button is disabled.
/// </summary>
public bool Disabled { get; set; }
private string _label;
private string _customId;
/// <summary>
/// Creates a button with the <see cref="ButtonStyle.Link"/> style.
/// </summary>
/// <param name="label">The label to use on the newly created link button.</param>
/// <param name="url">The url for this link button to go to.</param>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateLinkButton(string label, string url)
{
var builder = new ButtonBuilder()
.WithStyle(ButtonStyle.Link)
.WithUrl(url)
.WithLabel(label);
return builder;
}
/// <summary>
/// Creates a button with the <see cref="ButtonStyle.Danger"/> style.
/// </summary>
/// <param name="label">The label for this danger button.</param>
/// <param name="customId">The custom id for this danger button.</param>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateDangerButton(string label, string customId)
{
var builder = new ButtonBuilder()
.WithStyle(ButtonStyle.Danger)
.WithCustomId(customId)
.WithLabel(label);
return builder;
}
/// <summary>
/// Creates a button with the <see cref="ButtonStyle.Primary"/> style.
/// </summary>
/// <param name="label">The label for this primary button.</param>
/// <param name="customId">The custom id for this primary button.</param>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreatePrimaryButton(string label, string customId)
{
var builder = new ButtonBuilder()
.WithStyle(ButtonStyle.Primary)
.WithCustomId(customId)
.WithLabel(label);
return builder;
}
/// <summary>
/// Creates a button with the <see cref="ButtonStyle.Secondary"/> style.
/// </summary>
/// <param name="label">The label for this secondary button.</param>
/// <param name="customId">The custom id for this secondary button.</param>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateSecondaryButton(string label, string customId)
{
var builder = new ButtonBuilder()
.WithStyle(ButtonStyle.Secondary)
.WithCustomId(customId)
.WithLabel(label);
return builder;
}
/// <summary>
/// Creates a button with the <see cref="ButtonStyle.Success"/> style.
/// </summary>
/// <param name="label">The label for this success button.</param>
/// <param name="customId">The custom id for this success button.</param>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateSuccessButton(string label, string customId)
{
var builder = new ButtonBuilder()
.WithStyle(ButtonStyle.Success)
.WithCustomId(customId)
.WithLabel(label);
return builder;
}
/// <summary>
/// Sets the current buttons label to the specified text.
/// </summary>
/// <param name="label">The text for the label</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithLabel(string label)
{
this.Label = label;
return this;
}
/// <summary>
/// Sets the current buttons style.
/// </summary>
/// <param name="style">The style for this builders button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithStyle(ButtonStyle style)
{
this.Style = style;
return this;
}
/// <summary>
/// Sets the current buttons emote.
/// </summary>
/// <param name="emote">The emote to use for the current button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithEmote(IEmote emote)
{
this.Emote = emote;
return this;
}
/// <summary>
/// Sets the current buttons url.
/// </summary>
/// <param name="url">The url to use for the current button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithUrl(string url)
{
this.Url = url;
return this;
}
/// <summary>
/// Sets the custom id of the current button.
/// </summary>
/// <param name="id">The id to use for the current button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithCustomId(string id)
{
this.CustomId = id;
return this;
}
/// <summary>
/// Sets whether the current button is disabled.
/// </summary>
/// <param name="disabled">Whether the current button is disabled or not.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithDisabled(bool disabled)
{
this.Disabled = disabled;
return this;
}
/// <summary>
/// Builds this builder into a <see cref="ButtonComponent"/> to be used in a <see cref="ComponentBuilder"/>.
/// </summary>
/// <returns>A <see cref="ButtonComponent"/> to be used in a <see cref="ComponentBuilder"/>.</returns>
/// <exception cref="InvalidOperationException">A button cannot contain a URL and a CustomId.</exception>
/// <exception cref="ArgumentException">A button must have an Emote or a label.</exception>
public ButtonComponent Build()
{
if (string.IsNullOrEmpty(this.Label) && this.Emote == null)
throw new ArgumentException("A button must have an Emote or a label!");
if (!string.IsNullOrEmpty(this.Url) && !string.IsNullOrEmpty(this.CustomId))
throw new InvalidOperationException("A button cannot contain a URL and a CustomId");
if (this.Style == ButtonStyle.Link && !string.IsNullOrEmpty(this.CustomId))
this.CustomId = null;
else if (this.Style != ButtonStyle.Link && !string.IsNullOrEmpty(this.Url)) // Thanks 𝑴𝒓𝑪𝒂𝒌𝒆𝑺𝒍𝒂𝒚𝒆𝒓 :D
this.Url = null;
return new ButtonComponent(this.Style, this.Label, this.Emote, this.CustomId, this.Url, this.Disabled);
}
}
/// <summary>
/// Represents a class used to build <see cref="SelectMenu"/>'s.
/// </summary>
public class SelectMenuBuilder
{
/// <summary>
/// The max length of a <see cref="SelectMenu.Placeholder"/>.
/// </summary>
public const int MaxPlaceholderLength = 100;
/// <summary>
/// The maximum number of values for the <see cref="SelectMenu.MinValues"/> and <see cref="SelectMenu.MaxValues"/> properties.
/// </summary>
public const int MaxValuesCount = 25;
/// <summary>
/// The maximum number of options a <see cref="SelectMenu"/> can have.
/// </summary>
public const int MaxOptionCount = 25;
/// <summary>
/// Gets or sets the custom id of the current select menu.
/// </summary>
public string CustomId
{
get => _customId;
set
{
if (value != null)
if (value.Length > ComponentBuilder.MaxCustomIdLength)
throw new ArgumentException(message: $"Custom Id must be {ComponentBuilder.MaxCustomIdLength} characters or less!", paramName: nameof(CustomId));
_customId = value;
}
}
/// <summary>
/// Gets or sets the placeholder text of the current select menu.
/// </summary>
public string Placeholder
{
get => _placeholder;
set
{
if (value?.Length > MaxPlaceholderLength)
throw new ArgumentException(message: $"Placeholder must be {MaxPlaceholderLength} characters or less!", paramName: nameof(Placeholder));
_placeholder = value;
}
}
/// <summary>
/// Gets or sets the minimum values of the current select menu.
/// </summary>
public int MinValues
{
get => _minValues;
set
{
Preconditions.LessThan(value, MaxValuesCount, nameof(MinValues));
_minValues = value;
}
}
/// <summary>
/// Gets or sets the maximum values of the current select menu.
/// </summary>
public int MaxValues
{
get => _maxValues;
set
{
Preconditions.LessThan(value, MaxValuesCount, nameof(MaxValues));
_maxValues = value;
}
}
/// <summary>
/// Gets or sets a collection of <see cref="SelectMenuOptionBuilder"/> for this current select menu.
/// </summary>
public List<SelectMenuOptionBuilder> Options
{
get => _options;
set
{
if (value != null)
Preconditions.LessThan(value.Count, MaxOptionCount, nameof(Options));
_options = value;
}
}
/// <summary>
/// Gets or sets whether the current menu is disabled.
/// </summary>
public bool Disabled { get; set; }
private List<SelectMenuOptionBuilder> _options;
private int _minValues = 1;
private int _maxValues = 1;
private string _placeholder;
private string _customId;
/// <summary>
/// Creates a new instance of a <see cref="SelectMenuBuilder"/>.
/// </summary>
public SelectMenuBuilder() { }
/// <summary>
/// Creates a new instance of a <see cref="SelectMenuBuilder"/>.
/// </summary>
/// <param name="customId">The custom id of this select menu.</param>
/// <param name="options">The options for this select menu.</param>
public SelectMenuBuilder(string customId, List<SelectMenuOptionBuilder> options)
{
this.CustomId = customId;
this.Options = options;
}
/// <summary>
/// Sets the field CustomId.
/// </summary>
/// <param name="customId">The value to set the field CustomId to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuBuilder WithCustomId(string customId)
{
this.CustomId = customId;
return this;
}
/// <summary>
/// Sets the field placeholder.
/// </summary>
/// <param name="placeholder">The value to set the field placeholder to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuBuilder WithPlaceholder(string placeholder)
{
this.Placeholder = placeholder;
return this;
}
/// <summary>
/// Sets the field minValues.
/// </summary>
/// <param name="minValues">The value to set the field minValues to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuBuilder WithMinValues(int minValues)
{
this.MinValues = minValues;
return this;
}
/// <summary>
/// Sets the field maxValues.
/// </summary>
/// <param name="maxValues">The value to set the field maxValues to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuBuilder WithMaxValues(int maxValues)
{
this.MaxValues = maxValues;
return this;
}
/// <summary>
/// Sets the field options.
/// </summary>
/// <param name="options">The value to set the field options to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuBuilder WithOptions(List<SelectMenuOptionBuilder> options)
{
this.Options = options;
return this;
}
/// <summary>
/// Sets whether the current menu is disabled.
/// </summary>
/// <param name="disabled">Whether the current menu is disabled or not.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuBuilder WithDisabled(bool disabled)
{
this.Disabled = disabled;
return this;
}
/// <summary>
/// Builds a <see cref="SelectMenu"/>
/// </summary>
/// <returns>The newly built <see cref="SelectMenu"/></returns>
public SelectMenu Build()
{
var options = this.Options?.Select(x => x.Build()).ToList();
return new SelectMenu(this.CustomId, options, this.Placeholder, this.MinValues, this.MaxValues, this.Disabled);
}
}
/// <summary>
/// Represents a class used to build <see cref="SelectMenuOption"/>'s.
/// </summary>
public class SelectMenuOptionBuilder
{
/// <summary>
/// The maximum length of a <see cref="SelectMenuOption.Description"/>.
/// </summary>
public const int MaxDescriptionLength = 50;
/// <summary>
/// Gets or sets the label of the current select menu.
/// </summary>
public string Label
{
get => _label;
set
{
if (value != null)
if (value.Length > ComponentBuilder.MaxLabelLength)
throw new ArgumentException(message: $"Button label must be {ComponentBuilder.MaxLabelLength} characters or less!", paramName: nameof(Label));
_label = value;
}
}
/// <summary>
/// Gets or sets the custom id of the current select menu.
/// </summary>
public string Value
{
get => _value;
set
{
if (value != null)
if (value.Length > ComponentBuilder.MaxCustomIdLength)
throw new ArgumentException(message: $"Value must be {ComponentBuilder.MaxCustomIdLength} characters or less!", paramName: nameof(Value));
_value = value;
}
}
/// <summary>
/// Gets or sets this menu options description.
/// </summary>
public string Description
{
get => _description;
set
{
if (value != null)
Preconditions.LessThan(value.Length, MaxDescriptionLength, nameof(Description));
_description = value;
}
}
/// <summary>
/// Gets or sets the emote of this option.
/// </summary>
public IEmote Emote { get; set; }
/// <summary>
/// Gets or sets the whether or not this option will render selected by default.
/// </summary>
public bool? Default { get; set; }
private string _label;
private string _value;
private string _description;
/// <summary>
/// Creates a new instance of a <see cref="SelectMenuOptionBuilder"/>.
/// </summary>
public SelectMenuOptionBuilder() { }
/// <summary>
/// Creates a new instance of a <see cref="SelectMenuOptionBuilder"/>.
/// </summary>
/// <param name="label">The label for this option.</param>
/// <param name="value">The value of this option.</param>
public SelectMenuOptionBuilder(string label, string value)
{
this.Label = label;
this.Value = value;
}
/// <summary>
/// Sets the field label.
/// </summary>
/// <param name="label">The value to set the field label to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuOptionBuilder WithLabel(string label)
{
this.Label = label;
return this;
}
/// <summary>
/// Sets the field value.
/// </summary>
/// <param name="value">The value to set the field value to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuOptionBuilder WithValue(string value)
{
this.Value = value;
return this;
}
/// <summary>
/// Sets the field description.
/// </summary>
/// <param name="description">The value to set the field description to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuOptionBuilder WithDescription(string description)
{
this.Description = description;
return this;
}
/// <summary>
/// Sets the field emote.
/// </summary>
/// <param name="emote">The value to set the field emote to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuOptionBuilder WithEmote(IEmote emote)
{
this.Emote = emote;
return this;
}
/// <summary>
/// Sets the field default.
/// </summary>
/// <param name="defaultValue">The value to set the field default to.</param>
/// <returns>
/// The current builder.
/// </returns>
public SelectMenuOptionBuilder WithDefault(bool defaultValue)
{
this.Default = defaultValue;
return this;
}
/// <summary>
/// Builds a <see cref="SelectMenuOption"/>.
/// </summary>
/// <returns>The newly built <see cref="SelectMenuOption"/>.</returns>
public SelectMenuOption Build()
{
return new SelectMenuOption(this.Label, this.Value, this.Description, this.Emote, this.Default);
}
}
}