From 87350f8f0b31b203de5aad228e694bbcaa95451c Mon Sep 17 00:00:00 2001 From: quin lynch Date: Wed, 25 Aug 2021 18:40:31 -0300 Subject: [PATCH] Add methods to create builders from messages --- .../Message Components/ComponentBuilder.cs | 41 +++++++++++++++++++ .../Discord.Net.WebSocket.csproj | 6 +-- src/Discord.Net/Discord.Net.nuspec | 8 ++-- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs index 085c62cef..f91bdba36 100644 --- a/src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs @@ -45,6 +45,47 @@ namespace Discord private List _actionRows { get; set; } + /// + /// Creates a new builder from a message. + /// + /// The message to create the builder from. + /// The newly created builder. + public static ComponentBuilder FromMessage(IMessage message) + => FromComponents(message.Components); + + /// + /// Creates a new builder from the provided list of components. + /// + /// The components to create the builder from. + /// The newly created builder. + public static ComponentBuilder FromComponents(IReadOnlyCollection components) + { + var builder = new ComponentBuilder(); + for(int i = 0; i != components.Count; i++) + { + var component = components.ElementAt(i); + builder.AddComponent(component, i); + } + return builder; + } + + internal void AddComponent(IMessageComponent component, int row) + { + switch (component) + { + case ButtonComponent button: + this.WithButton(button.Label, button.CustomId, button.Style, button.Emote, button.Url, button.Disabled, row); + break; + case ActionRowComponent actionRow: + foreach (var cmp in actionRow.Components) + AddComponent(cmp, row); + break; + case SelectMenu menu: + this.WithSelectMenu(menu.Placeholder, menu.CustomId, menu.Options.Select(x => new SelectMenuOptionBuilder(x.Label, x.Value, x.Description, x.Emote, x.Default)).ToList(), menu.Placeholder, menu.MinValues, menu.MaxValues, menu.Disabled, row); + break; + } + } + /// /// Adds a to the at the specific row. /// If the row cannot accept the component then it will add it to a row that can. diff --git a/src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj b/src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj index fbf104b2b..9aa71cc3d 100644 --- a/src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj +++ b/src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj @@ -8,7 +8,7 @@ net461;netstandard2.0;netstandard2.1 netstandard2.0;netstandard2.1 true - 3.0.1 + 3.0.2 https://github.com/Discord-Net-Labs/Discord.Net-Labs https://github.com/Discord-Net-Labs/Discord.Net-Labs Temporary.png @@ -16,8 +16,8 @@ ..\Discord.Net.WebSocket\Discord.Net.WebSocket.xml - 3.0.1 - 3.0.1 + 3.0.2 + 3.0.2 TRACE diff --git a/src/Discord.Net/Discord.Net.nuspec b/src/Discord.Net/Discord.Net.nuspec index e1de36188..dbee18672 100644 --- a/src/Discord.Net/Discord.Net.nuspec +++ b/src/Discord.Net/Discord.Net.nuspec @@ -2,7 +2,7 @@ Discord.Net.Labs - 3.0.1$suffix$ + 3.0.2$suffix$ Discord.Net Labs Discord.Net Contributors quinchs @@ -16,21 +16,21 @@ - + - + - +