From e31a9f73fa4b3add2b5cac9073904208ceaa65f1 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Mon, 24 Jul 2023 01:06:00 +0500 Subject: [PATCH] Remove non-numbers from SnowflakeOption inputs (#63) This PR allows passing mentions as an input to SnowflakeOption by removing non-number characters from the input string. Signed-off-by: Octol1ttle --- src/Data/Options/SnowflakeOption.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Data/Options/SnowflakeOption.cs b/src/Data/Options/SnowflakeOption.cs index f65065c..05921ff 100644 --- a/src/Data/Options/SnowflakeOption.cs +++ b/src/Data/Options/SnowflakeOption.cs @@ -1,11 +1,12 @@ using System.Text.Json.Nodes; +using System.Text.RegularExpressions; using Remora.Discord.Extensions.Formatting; using Remora.Rest.Core; using Remora.Results; namespace Boyfriend.Data.Options; -public class SnowflakeOption : Option { +public partial class SnowflakeOption : Option { public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { } public override string Display(JsonNode settings) { @@ -18,10 +19,13 @@ public class SnowflakeOption : Option { } public override Result Set(JsonNode settings, string from) { - if (!ulong.TryParse(from, out var parsed)) + if (!ulong.TryParse(NonNumbers().Replace(from, ""), out var parsed)) return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue)); settings[Name] = parsed; return Result.FromSuccess(); } + + [GeneratedRegex("[^0-9]")] + private static partial Regex NonNumbers(); }