using System.Text.Json.Nodes; using Remora.Discord.Extensions.Formatting; using Remora.Results; namespace TeamOctolings.Octobot.Data.Options; /// /// Represents a per-guild option. /// /// The type of the option. public class GuildOption : IGuildOption where T : notnull { protected readonly T DefaultValue; public GuildOption(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.Success; } public Result Reset(JsonNode settings) { settings[Name] = null; return Result.Success; } /// /// 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; } }