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 <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-07-24 01:06:00 +05:00 committed by GitHub
parent 27b8f15e3b
commit e31a9f73fa
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,12 @@
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Remora.Discord.Extensions.Formatting; using Remora.Discord.Extensions.Formatting;
using Remora.Rest.Core; using Remora.Rest.Core;
using Remora.Results; using Remora.Results;
namespace Boyfriend.Data.Options; namespace Boyfriend.Data.Options;
public class SnowflakeOption : Option<Snowflake> { public partial class SnowflakeOption : Option<Snowflake> {
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { } public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
public override string Display(JsonNode settings) { public override string Display(JsonNode settings) {
@ -18,10 +19,13 @@ public class SnowflakeOption : Option<Snowflake> {
} }
public override Result Set(JsonNode settings, string from) { 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)); return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = parsed; settings[Name] = parsed;
return Result.FromSuccess(); return Result.FromSuccess();
} }
[GeneratedRegex("[^0-9]")]
private static partial Regex NonNumbers();
} }