Skip to content

Commit 0aff637

Browse files
authored
Add select menu IsRequired property and fix usage of DefaultValues (#3199)
1 parent a476014 commit 0aff637

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public List<SelectMenuOptionBuilder> Options
121121
}
122122
}
123123

124+
/// <summary>
125+
/// Gets or sets whether the current menu is required to answer in a modal (defaults to <see langword="true"/>).
126+
/// </summary>
127+
public bool IsRequired { get ; set; } = true;
128+
124129
/// <summary>
125130
/// Gets or sets whether the current menu is disabled.
126131
/// </summary>
@@ -168,11 +173,13 @@ public SelectMenuBuilder(SelectMenuComponent selectMenu)
168173
CustomId = selectMenu.CustomId;
169174
MaxValues = selectMenu.MaxValues;
170175
MinValues = selectMenu.MinValues;
176+
IsRequired = selectMenu.IsRequired;
171177
IsDisabled = selectMenu.IsDisabled;
172178
Type = selectMenu.Type;
173179
Options = selectMenu.Options?
174180
.Select(x => new SelectMenuOptionBuilder(x.Label, x.Value, x.Description, x.Emote, x.IsDefault))
175181
.ToList();
182+
ChannelTypes = selectMenu.ChannelTypes?.ToList();
176183
DefaultValues = selectMenu.DefaultValues?.ToList();
177184
Id = selectMenu.Id;
178185
}
@@ -188,12 +195,16 @@ public SelectMenuBuilder(SelectMenuComponent selectMenu)
188195
/// <param name="isDisabled">Disabled this select menu or not.</param>
189196
/// <param name="type">The <see cref="ComponentType"/> of this select menu.</param>
190197
/// <param name="channelTypes">The types of channels this menu can select (only valid on <see cref="ComponentType.ChannelSelect"/>s)</param>
191-
public SelectMenuBuilder(string customId, List<SelectMenuOptionBuilder> options = null, string placeholder = null, int maxValues = 1, int minValues = 1,
192-
bool isDisabled = false, ComponentType type = ComponentType.SelectMenu, List<ChannelType> channelTypes = null, List<SelectMenuDefaultValue> defaultValues = null, int? id = null)
198+
/// <param name="defaultValues">The list of default values.</param>
199+
/// <param name="id">An optional id for the component.</param>
200+
/// <param name="isRequired">Whether the current menu is required to answer in a modal.</param>
201+
public SelectMenuBuilder(string customId, List<SelectMenuOptionBuilder> options = null, string placeholder = null, int maxValues = 1, int minValues = 1, bool isDisabled = false,
202+
ComponentType type = ComponentType.SelectMenu, List<ChannelType> channelTypes = null, List<SelectMenuDefaultValue> defaultValues = null, int? id = null, bool isRequired = true)
193203
{
194204
CustomId = customId;
195205
Options = options;
196206
Placeholder = placeholder;
207+
IsRequired = isRequired;
197208
IsDisabled = isDisabled;
198209
MaxValues = maxValues;
199210
MinValues = minValues;
@@ -352,6 +363,19 @@ public SelectMenuBuilder WithDefaultValues(params SelectMenuDefaultValue[] defau
352363
return this;
353364
}
354365

366+
/// <summary>
367+
/// Sets whether the current menu is required to answer in a modal.
368+
/// </summary>
369+
/// <param name="isRequired">Whether the current menu is required to answer in a modal.</param>
370+
/// <returns>
371+
/// The current builder.
372+
/// </returns>
373+
public SelectMenuBuilder WithRequired(bool isRequired)
374+
{
375+
IsRequired = isRequired;
376+
return this;
377+
}
378+
355379
/// <summary>
356380
/// Sets whether the current menu is disabled.
357381
/// </summary>
@@ -414,7 +438,7 @@ public SelectMenuComponent Build()
414438
{
415439
var options = Options?.Select(x => x.Build()).ToList();
416440

417-
return new SelectMenuComponent(CustomId, options, Placeholder, MinValues, MaxValues, IsDisabled, Type, Id, ChannelTypes, DefaultValues);
441+
return new SelectMenuComponent(CustomId, options, Placeholder, MinValues, MaxValues, IsRequired, IsDisabled, Type, Id, ChannelTypes, DefaultValues);
418442
}
419443

420444
/// <inheritdoc/>

src/Discord.Net.Core/Entities/Interactions/MessageComponents/SelectMenuComponent.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class SelectMenuComponent : IInteractableComponent
3737
/// </summary>
3838
public int MaxValues { get; }
3939

40+
/// <summary>
41+
/// Gets whether this menu is required to answer in a modal.
42+
/// </summary>
43+
public bool IsRequired { get;}
44+
4045
/// <summary>
4146
/// Gets whether this menu is disabled or not.
4247
/// </summary>
@@ -58,14 +63,15 @@ public class SelectMenuComponent : IInteractableComponent
5863
public SelectMenuBuilder ToBuilder()
5964
=> new(this);
6065

61-
internal SelectMenuComponent(string customId, List<SelectMenuOption> options, string placeholder, int minValues, int maxValues,
66+
internal SelectMenuComponent(string customId, List<SelectMenuOption> options, string placeholder, int minValues, int maxValues, bool required,
6267
bool disabled, ComponentType type, int? id, IEnumerable<ChannelType> channelTypes = null, IEnumerable<SelectMenuDefaultValue> defaultValues = null)
6368
{
6469
CustomId = customId;
6570
Options = options;
6671
Placeholder = placeholder;
6772
MinValues = minValues;
6873
MaxValues = maxValues;
74+
IsRequired = required;
6975
IsDisabled = disabled;
7076
Type = type;
7177
Id = id;

src/Discord.Net.Core/Entities/Interactions/Modals/ModalBuilder.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public ModalBuilder AddLabel(
165165
return this;
166166
}
167167

168-
/// <inheritdoc cref="ModalComponentBuilder.WithSelectMenu(string, string, List{SelectMenuOptionBuilder}, string, int, int, bool, ComponentType, ChannelType[], int?, string, int?)"/>
168+
/// <inheritdoc cref="ModalComponentBuilder.WithSelectMenu(string, string, List{SelectMenuOptionBuilder}, string, int, int, bool, bool, ComponentType, ChannelType[], SelectMenuDefaultValue[], int?, string, int?)"/>
169169
/// <returns>The current <see cref="ModalBuilder"/>.</returns>
170170
public ModalBuilder AddSelectMenu(
171171
string label,
@@ -174,9 +174,11 @@ public ModalBuilder AddSelectMenu(
174174
string placeholder = null,
175175
int minValues = 1,
176176
int maxValues = 1,
177+
bool required = true,
177178
bool disabled = false,
178179
ComponentType type = ComponentType.SelectMenu,
179180
ChannelType[] channelTypes = null,
181+
SelectMenuDefaultValue[] defaultValues = null,
180182
int? id = null,
181183
string description = null,
182184
int? labelId = null
@@ -189,9 +191,11 @@ public ModalBuilder AddSelectMenu(
189191
placeholder,
190192
minValues,
191193
maxValues,
194+
required,
192195
disabled,
193196
type,
194197
channelTypes,
198+
defaultValues,
195199
id,
196200
description,
197201
labelId
@@ -624,9 +628,11 @@ public ModalComponentBuilder WithLabel(
624628
/// <param name="placeholder">The placeholder of the <see cref="SelectMenuBuilder"/>.</param>
625629
/// <param name="minValues">The min values of the <see cref="SelectMenuBuilder"/>.</param>
626630
/// <param name="maxValues">The max values of the <see cref="SelectMenuBuilder"/>.</param>
631+
/// <param name="required">Whether the <see cref="SelectMenuBuilder"/> is required to be answered.</param>
627632
/// <param name="disabled">Whether the <see cref="SelectMenuBuilder"/> is disabled.</param>
628633
/// <param name="type">The type of the <see cref="SelectMenuBuilder"/>.</param>
629634
/// <param name="channelTypes">The channel types of the <see cref="SelectMenuBuilder"/>.</param>
635+
/// <param name="defaultValues">The default values of the <see cref="SelectMenuBuilder"/>.</param>
630636
/// <param name="id">The id of the <see cref="SelectMenuBuilder"/>.</param>
631637
/// <param name="description">The description around the <see cref="SelectMenuBuilder"/>.</param>
632638
/// <param name="labelId">
@@ -640,9 +646,11 @@ public ModalComponentBuilder WithSelectMenu(
640646
string placeholder = null,
641647
int minValues = 1,
642648
int maxValues = 1,
649+
bool required = true,
643650
bool disabled = false,
644651
ComponentType type = ComponentType.SelectMenu,
645652
ChannelType[] channelTypes = null,
653+
SelectMenuDefaultValue[] defaultValues = null,
646654
int? id = null,
647655
string description = null,
648656
int? labelId = null
@@ -655,9 +663,11 @@ public ModalComponentBuilder WithSelectMenu(
655663
.WithPlaceholder(placeholder)
656664
.WithMaxValues(maxValues)
657665
.WithMinValues(minValues)
666+
.WithRequired(required)
658667
.WithDisabled(disabled)
659668
.WithType(type)
660-
.WithChannelTypes(channelTypes),
669+
.WithChannelTypes(channelTypes)
670+
.WithDefaultValues(defaultValues),
661671
description,
662672
labelId
663673
);
@@ -695,11 +705,11 @@ public ModalComponentBuilder WithSelectMenu(
695705
/// Constructs and adds a <see cref="LabelBuilder"/> with the provided
696706
/// <see cref="FileUploadComponentBuilder"/> to the current <see cref="ModalComponentBuilder"/>.
697707
/// </summary>
698-
/// <param name="label">The label around the <see cref="SelectMenuBuilder"/>.</param>
708+
/// <param name="label">The label around the <see cref="FileUploadComponentBuilder"/>.</param>
699709
/// <param name="fileUpload">The file upload to add.</param>
700-
/// <param name="description">The description around the <see cref="SelectMenuBuilder"/>.</param>
710+
/// <param name="description">The description around the <see cref="FileUploadComponentBuilder"/>.</param>
701711
/// <param name="labelId">
702-
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="SelectMenuBuilder"/>.
712+
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="FileUploadComponentBuilder"/>.
703713
/// </param>
704714
/// <returns>The current <see cref="ModalComponentBuilder"/>.</returns>
705715
public ModalComponentBuilder WithFileUpload(
@@ -713,15 +723,15 @@ public ModalComponentBuilder WithFileUpload(
713723
/// Constructs and adds a <see cref="LabelBuilder"/> with a <see cref="FileUploadComponentBuilder"/>
714724
/// to the current <see cref="ModalComponentBuilder"/>.
715725
/// </summary>
716-
/// <param name="label">The label around the <see cref="SelectMenuBuilder"/>.</param>
726+
/// <param name="label">The label around the <see cref="FileUploadComponentBuilder"/>.</param>
717727
/// <param name="customId">The custom id of the <see cref="FileUploadComponentBuilder"/>.</param>
718728
/// <param name="minValues">The min values of the <see cref="FileUploadComponentBuilder"/>.</param>
719729
/// <param name="maxValues">The max values of the <see cref="FileUploadComponentBuilder"/>.</param>
720730
/// <param name="isRequired">Whether the <see cref="FileUploadComponentBuilder"/> is required.</param>
721731
/// <param name="id">The id of the <see cref="FileUploadComponentBuilder"/>.</param>
722-
/// <param name="description">The description around the <see cref="SelectMenuBuilder"/>.</param>
732+
/// <param name="description">The description around the <see cref="FileUploadComponentBuilder"/>.</param>
723733
/// <param name="labelId">
724-
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="SelectMenuBuilder"/>.
734+
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="FileUploadComponentBuilder"/>.
725735
/// </param>
726736
/// <returns>The current <see cref="ModalComponentBuilder"/>.</returns>
727737
public ModalComponentBuilder WithFileUpload(
@@ -767,11 +777,11 @@ public ModalComponentBuilder WithTextDisplay(string content, int? id = null)
767777
/// Constructs and adds a <see cref="LabelBuilder"/> with the provided <see cref="TextInputBuilder"/> to
768778
/// the current <see cref="ModalComponentBuilder"/>.
769779
/// </summary>
770-
/// <param name="label">The label around the <see cref="SelectMenuBuilder"/>.</param>
780+
/// <param name="label">The label around the <see cref="TextInputBuilder"/>.</param>
771781
/// <param name="textInput">The text input to add.</param>
772-
/// <param name="description">The description around the <see cref="SelectMenuBuilder"/>.</param>
782+
/// <param name="description">The description around the <see cref="TextInputBuilder"/>.</param>
773783
/// <param name="labelId">
774-
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="SelectMenuBuilder"/>.
784+
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="TextInputBuilder"/>.
775785
/// </param>
776786
/// <returns>The current <see cref="ModalComponentBuilder"/>.</returns>
777787
public ModalComponentBuilder WithTextInput(
@@ -823,7 +833,7 @@ public ModalComponentBuilder WithTextInput(TextInputBuilder text, int row)
823833
/// Constructs and adds a <see cref="LabelBuilder"/> with a <see cref="TextInputBuilder"/>
824834
/// to the current <see cref="ModalComponentBuilder"/>.
825835
/// </summary>
826-
/// <param name="label">The label around the <see cref="SelectMenuBuilder"/>.</param>
836+
/// <param name="label">The label around the <see cref="TextInputBuilder"/>.</param>
827837
/// <param name="customId">The custom id of the <see cref="TextInputBuilder"/>.</param>
828838
/// <param name="style">The style of the <see cref="TextInputBuilder"/>.</param>
829839
/// <param name="placeholder">The placeholder of the <see cref="TextInputBuilder"/>.</param>
@@ -833,9 +843,9 @@ public ModalComponentBuilder WithTextInput(TextInputBuilder text, int row)
833843
/// <param name="required">Whether the <see cref="TextInputBuilder"/> is required.</param>
834844
/// <param name="value">The value of the <see cref="TextInputBuilder"/>.</param>
835845
/// <param name="id">The id of the <see cref="TextInputBuilder"/>.</param>
836-
/// <param name="description">The description around the <see cref="SelectMenuBuilder"/>.</param>
846+
/// <param name="description">The description around the <see cref="TextInputBuilder"/>.</param>
837847
/// <param name="labelId">
838-
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="SelectMenuBuilder"/>.
848+
/// The id of the <see cref="LabelBuilder"/> wrapping the <see cref="TextInputBuilder"/>.
839849
/// </param>
840850
/// <returns>The current <see cref="ModalComponentBuilder"/>.</returns>
841851
public ModalComponentBuilder WithTextInput(

src/Discord.Net.Rest/API/Common/SelectMenuComponent.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ internal class SelectMenuComponent : IInteractableComponent
2626
[JsonProperty("max_values")]
2727
public int MaxValues { get; set; }
2828

29+
[JsonProperty("required")]
30+
public bool Required { get; set; }
31+
2932
[JsonProperty("disabled")]
3033
public bool Disabled { get; set; }
3134

@@ -51,6 +54,7 @@ public SelectMenuComponent(Discord.SelectMenuComponent component)
5154
Placeholder = component.Placeholder;
5255
MinValues = component.MinValues;
5356
MaxValues = component.MaxValues;
57+
Required = component.IsRequired;
5458
Disabled = component.IsDisabled;
5559
ChannelTypes = component.ChannelTypes.ToArray();
5660
DefaultValues = component.DefaultValues.Select(x => new SelectMenuDefaultValue {Id = x.Id, Type = x.Type}).ToArray();

src/Discord.Net.Rest/Extensions/MessageComponentExtension.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ internal static IMessageComponent ToEntity(this IMessageComponent component)
102102
parsed.Placeholder.GetValueOrDefault(),
103103
parsed.MinValues,
104104
parsed.MaxValues,
105+
parsed.Required,
105106
parsed.Disabled,
106107
parsed.Type,
107108
parsed.Id.ToNullable(),

0 commit comments

Comments
 (0)