2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
2023-07-23 23:06:00 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2023-07-18 15:25:02 +03:00
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
using Remora.Rest.Core;
|
|
|
|
using Remora.Results;
|
|
|
|
|
|
|
|
namespace Boyfriend.Data.Options;
|
|
|
|
|
2023-07-23 23:06:00 +03:00
|
|
|
public partial class SnowflakeOption : Option<Snowflake> {
|
2023-07-18 15:25:02 +03:00
|
|
|
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
|
|
|
|
|
|
|
|
public override string Display(JsonNode settings) {
|
|
|
|
return Name.EndsWith("Channel") ? Mention.Channel(Get(settings)) : Mention.Role(Get(settings));
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Snowflake Get(JsonNode settings) {
|
|
|
|
var property = settings[Name];
|
|
|
|
return property != null ? property.GetValue<ulong>().ToSnowflake() : DefaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Result Set(JsonNode settings, string from) {
|
2023-07-23 23:06:00 +03:00
|
|
|
if (!ulong.TryParse(NonNumbers().Replace(from, ""), out var parsed))
|
2023-07-18 15:25:02 +03:00
|
|
|
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
|
|
|
|
|
|
|
|
settings[Name] = parsed;
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
2023-07-23 23:06:00 +03:00
|
|
|
|
|
|
|
[GeneratedRegex("[^0-9]")]
|
|
|
|
private static partial Regex NonNumbers();
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|