From 9427314728b60fb419492eaac512b9a80831a98b Mon Sep 17 00:00:00 2001 From: Karlo Piskor Date: Fri, 13 Oct 2023 15:02:41 +0200 Subject: [PATCH 1/4] Prevent multiline values in short Textinputs --- .../Interactions/MessageComponents/ComponentBuilder.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs index 33760ca5cf..9bdd04e129 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs @@ -1406,6 +1406,9 @@ public int? MaxLength /// /// .Length is greater than or . /// + /// + /// is and contains a new line character. + /// public string Value { get => _value; @@ -1415,6 +1418,9 @@ public string Value throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be longer than {MaxLength ?? LargestMaxLength}."); if (value?.Length < (MinLength ?? 0)) throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be shorter than {MinLength}"); + if (Style == TextInputStyle.Short && value?.Contains('\n') == true) + throw new ArgumentOutOfRangeException(nameof(value), $"Value must not contain new line characters when style is {TextInputStyle.Short}."); + _value = value; } } From 10bd2983b61ea6bf9644ae846aa8f28e6f42c90e Mon Sep 17 00:00:00 2001 From: Karlo Piskor Date: Fri, 13 Oct 2023 15:20:04 +0200 Subject: [PATCH 2/4] Change exception --- .../Interactions/MessageComponents/ComponentBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs index 9bdd04e129..bbd7dbe64e 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs @@ -1406,7 +1406,7 @@ public int? MaxLength /// /// .Length is greater than or . /// - /// + /// /// is and contains a new line character. /// public string Value @@ -1419,7 +1419,7 @@ public string Value if (value?.Length < (MinLength ?? 0)) throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be shorter than {MinLength}"); if (Style == TextInputStyle.Short && value?.Contains('\n') == true) - throw new ArgumentOutOfRangeException(nameof(value), $"Value must not contain new line characters when style is {TextInputStyle.Short}."); + throw new ArgumentException(nameof(value), $"Value must not contain new line characters when style is {TextInputStyle.Short}."); _value = value; } From 072121aa8f007272d9f55c67eac8427a9a053200 Mon Sep 17 00:00:00 2001 From: Karlo Piskor Date: Tue, 17 Oct 2023 17:51:58 +0200 Subject: [PATCH 3/4] Move check to build method --- .../Interactions/MessageComponents/ComponentBuilder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs index bbd7dbe64e..6f9d7fa56a 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs @@ -1418,8 +1418,6 @@ public string Value throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be longer than {MaxLength ?? LargestMaxLength}."); if (value?.Length < (MinLength ?? 0)) throw new ArgumentOutOfRangeException(nameof(value), $"Value must not be shorter than {MinLength}"); - if (Style == TextInputStyle.Short && value?.Contains('\n') == true) - throw new ArgumentException(nameof(value), $"Value must not contain new line characters when style is {TextInputStyle.Short}."); _value = value; } @@ -1556,6 +1554,9 @@ public TextInputComponent Build() throw new ArgumentException("TextInputComponents must have a custom id.", nameof(CustomId)); if (string.IsNullOrWhiteSpace(Label)) throw new ArgumentException("TextInputComponents must have a label.", nameof(Label)); + if (Style == TextInputStyle.Short && Value?.Contains('\n') == true) + throw new ArgumentException(nameof(Value), $"Value must not contain new line characters when style is {TextInputStyle.Short}."); + return new TextInputComponent(CustomId, Label, Placeholder, MinLength, MaxLength, Style, Required, Value); } } From 273c794728940d14b78ab9de60e9a476eeac29d1 Mon Sep 17 00:00:00 2001 From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> Date: Tue, 17 Oct 2023 20:39:15 +0300 Subject: [PATCH 4/4] Update src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs --- .../Entities/Interactions/MessageComponents/ComponentBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs index 6f9d7fa56a..549913dc91 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/ComponentBuilder.cs @@ -1555,7 +1555,7 @@ public TextInputComponent Build() if (string.IsNullOrWhiteSpace(Label)) throw new ArgumentException("TextInputComponents must have a label.", nameof(Label)); if (Style == TextInputStyle.Short && Value?.Contains('\n') == true) - throw new ArgumentException(nameof(Value), $"Value must not contain new line characters when style is {TextInputStyle.Short}."); + throw new ArgumentException($"Value must not contain new line characters when style is {TextInputStyle.Short}.", nameof(Value)); return new TextInputComponent(CustomId, Label, Placeholder, MinLength, MaxLength, Style, Required, Value); }