2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using Remora.Results;
|
2024-05-16 18:34:26 +03:00
|
|
|
using TeamOctolings.Octobot.Parsers;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot.Data.Options;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
public sealed class TimeSpanOption : GuildOption<TimeSpan>
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override TimeSpan Get(JsonNode settings)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
var property = settings[Name];
|
2024-01-28 21:36:29 +03:00
|
|
|
return property != null ? TimeSpanParser.TryParse(property.GetValue<string>()).Entity : DefaultValue;
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override Result Set(JsonNode settings, string from)
|
|
|
|
{
|
2024-01-28 21:36:29 +03:00
|
|
|
if (!TimeSpanParser.TryParse(from).IsDefined(out var span))
|
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] = span.ToString();
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
}
|