From 13ccc7c9972c2b55983de9d75cf35b29db5fd30b Mon Sep 17 00:00:00 2001 From: Misha133 <61027276+Misha-133@users.noreply.github.com> Date: Wed, 18 May 2022 10:50:55 +0300 Subject: [PATCH] feature: Add `.With` methods to ActionRowBuilder (#2296) * Added `.With` methods to `ActionRowBuilder` - Added `.WithButton` & `.WithSelectMenu` methods to `ActionRowBuilder` - fixed a typo * removed `` from methods which don't directly throw an exception * Update src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs * Update src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> --- .../MessageComponents/ComponentBuilder.cs | 100 +++++++++++++++++- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs index 9c529f469..37342b039 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs @@ -195,7 +195,7 @@ namespace Discord /// /// The button to add. /// The row to add the button. - /// There is no more row to add a menu. + /// There is no more row to add a button. /// must be less than . /// The current builder. public ComponentBuilder WithButton(ButtonBuilder button, int row = 0) @@ -348,6 +348,100 @@ namespace Discord return this; } + /// + /// Adds a to the . + /// + /// The custom id of the menu. + /// The options of the menu. + /// The placeholder of the menu. + /// The min values of the placeholder. + /// The max values of the placeholder. + /// Whether or not the menu is disabled. + /// The current builder. + public ActionRowBuilder WithSelectMenu(string customId, List options, + string placeholder = null, int minValues = 1, int maxValues = 1, bool disabled = false) + { + return WithSelectMenu(new SelectMenuBuilder() + .WithCustomId(customId) + .WithOptions(options) + .WithPlaceholder(placeholder) + .WithMaxValues(maxValues) + .WithMinValues(minValues) + .WithDisabled(disabled)); + } + + /// + /// Adds a to the . + /// + /// The menu to add. + /// A Select Menu cannot exist in a pre-occupied ActionRow. + /// The current builder. + public ActionRowBuilder WithSelectMenu(SelectMenuBuilder menu) + { + if (menu.Options.Distinct().Count() != menu.Options.Count) + throw new InvalidOperationException("Please make sure that there is no duplicates values."); + + var builtMenu = menu.Build(); + + if (Components.Count != 0) + throw new InvalidOperationException($"A Select Menu cannot exist in a pre-occupied ActionRow."); + + AddComponent(builtMenu); + + return this; + } + + /// + /// Adds a with specified parameters to the . + /// + /// The label text for the newly added button. + /// The style of this newly added button. + /// A to be used with this button. + /// The custom id of the newly added button. + /// A URL to be used only if the is a Link. + /// Whether or not the newly created button is disabled. + /// The current builder. + public ActionRowBuilder WithButton( + string label = null, + string customId = null, + ButtonStyle style = ButtonStyle.Primary, + IEmote emote = null, + string url = null, + bool disabled = false) + { + var button = new ButtonBuilder() + .WithLabel(label) + .WithStyle(style) + .WithEmote(emote) + .WithCustomId(customId) + .WithUrl(url) + .WithDisabled(disabled); + + return WithButton(button); + } + + /// + /// Adds a to the . + /// + /// The button to add. + /// Components count reached . + /// A button cannot be added to a row with a SelectMenu. + /// The current builder. + public ActionRowBuilder WithButton(ButtonBuilder button) + { + var builtButton = button.Build(); + + if(Components.Count >= 5) + throw new InvalidOperationException($"Components count reached {MaxChildCount}"); + + if (Components.Any(x => x.Type == ComponentType.SelectMenu)) + throw new InvalidOperationException($"A button cannot be added to a row with a SelectMenu"); + + AddComponent(builtButton); + + return this; + } + /// /// Builds the current builder to a that can be used within a /// @@ -1227,7 +1321,7 @@ namespace Discord /// The text input's minimum length. /// The text input's maximum length. /// The text input's required value. - public TextInputBuilder (string label, string customId, TextInputStyle style = TextInputStyle.Short, string placeholder = null, + public TextInputBuilder(string label, string customId, TextInputStyle style = TextInputStyle.Short, string placeholder = null, int? minLength = null, int? maxLength = null, bool? required = null, string value = null) { Label = label; @@ -1291,7 +1385,7 @@ namespace Discord Placeholder = placeholder; return this; } - + /// /// Sets the value of the current builder. ///