using System.Text.Json.Nodes;
using Remora.Discord.Extensions.Formatting;
using Remora.Results;
namespace Octobot.Data.Options;
///
/// Represents an per-guild option.
///
/// The type of the option.
public class Option : IOption
where T : notnull
{
protected readonly T DefaultValue;
public Option(string name, T defaultValue)
{
Name = name;
DefaultValue = defaultValue;
}
public string Name { get; }
public virtual string Display(JsonNode settings)
{
return Markdown.InlineCode(Get(settings).ToString() ?? throw new InvalidOperationException());
}
///
/// Sets the value of the option from a to the provided JsonNode.
///
/// The to set the value to.
/// The string from which the new value of the option will be parsed.
/// A value setting result which may or may not have succeeded.
public virtual Result Set(JsonNode settings, string from)
{
settings[Name] = from;
return Result.FromSuccess();
}
///
/// Gets the value of the option from the provided .
///
/// The to get the value from.
/// The value of the option.
public virtual T Get(JsonNode settings)
{
var property = settings[Name];
return property != null ? property.GetValue() : DefaultValue;
}
public Result Reset(JsonNode settings)
{
settings[Name] = null;
return Result.FromSuccess();
}
}