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;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Data.Options;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public sealed partial class SnowflakeOption : Option<Snowflake>
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override string Display(JsonNode settings)
|
|
|
|
{
|
|
|
|
return Name.EndsWith("Channel", StringComparison.Ordinal)
|
|
|
|
? Mention.Channel(Get(settings))
|
|
|
|
: Mention.Role(Get(settings));
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override Snowflake Get(JsonNode settings)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
var property = settings[Name];
|
|
|
|
return property != null ? property.GetValue<ulong>().ToSnowflake() : DefaultValue;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
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-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|