1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-05 05:26:28 +03:00

Made guild settings code 10x better

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-07-11 00:28:49 +05:00
parent c00585e639
commit bbddb3790a
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
26 changed files with 452 additions and 303 deletions

View file

@ -0,0 +1,31 @@
using System.Text.Json.Nodes;
using Boyfriend.locale;
using Remora.Results;
namespace Boyfriend.Data.Options;
public class BoolOption : Option<bool> {
public BoolOption(string name, bool defaultValue) : base(name, defaultValue) { }
public override Result Set(JsonNode settings, string from) {
if (!TryParseBool(from, out var value))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = value;
return Result.FromSuccess();
}
private static bool TryParseBool(string from, out bool value) {
value = false;
switch (from) {
case "1" or "y" or "yes" or "д" or "да":
value = true;
return true;
case "0" or "n" or "no" or "н" or "не" or "нет":
value = false;
return true;
default:
return false;
}
}
}

View file

@ -0,0 +1,10 @@
using System.Text.Json.Nodes;
using Remora.Results;
namespace Boyfriend.Data.Options;
public interface IOption {
string Name { get; init; }
object GetAsObject(JsonNode settings);
Result Set(JsonNode settings, string from);
}

View file

@ -0,0 +1,31 @@
using System.Globalization;
using System.Text.Json.Nodes;
using Boyfriend.locale;
using Remora.Results;
namespace Boyfriend.Data.Options;
/// <inheritdoc />
public class LanguageOption : Option<CultureInfo> {
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new() {
{ "en", new CultureInfo("en-US") },
{ "ru", new CultureInfo("ru-RU") },
{ "mctaylors-ru", new CultureInfo("tt-RU") }
};
public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { }
/// <inheritdoc />
public override CultureInfo Get(JsonNode settings) {
var property = settings[Name];
return property != null ? CultureInfoCache[property.GetValue<string>()] : DefaultValue;
}
/// <inheritdoc />
public override Result Set(JsonNode settings, string from) {
if (!CultureInfoCache.ContainsKey(from.ToLowerInvariant()))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported));
return base.Set(settings, from.ToLowerInvariant());
}
}

View file

@ -0,0 +1,45 @@
using System.Text.Json.Nodes;
using Remora.Results;
namespace Boyfriend.Data.Options;
/// <summary>
/// Represents an per-guild option.
/// </summary>
/// <typeparam name="T">The type of the option.</typeparam>
public class Option<T> : IOption {
internal readonly T DefaultValue;
public Option(string name, T defaultValue) {
Name = name;
DefaultValue = defaultValue;
}
public Type Type { get; set; } = typeof(T);
public string Name { get; init; }
public object GetAsObject(JsonNode settings) {
return Get(settings)!;
}
/// <summary>
/// Sets the value of the option from a <see cref="string" /> to the provided JsonNode.
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to set the value to.</param>
/// <param name="from">The string from which the new value of the option will be parsed.</param>
/// <returns>A value setting result which may or may not have succeeded.</returns>
public virtual Result Set(JsonNode settings, string from) {
settings[Name] = from;
return Result.FromSuccess();
}
/// <summary>
/// Gets the value of the option from the provided <paramref name="settings" />.
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to get the value from.</param>
/// <returns>The value of the option.</returns>
public virtual T Get(JsonNode settings) {
var property = settings[Name];
return property != null ? property.GetValue<T>() : DefaultValue;
}
}

View file

@ -0,0 +1,23 @@
using System.Text.Json.Nodes;
using Boyfriend.locale;
using Remora.Rest.Core;
using Remora.Results;
namespace Boyfriend.Data.Options;
public class SnowflakeOption : Option<Snowflake> {
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
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) {
if (!ulong.TryParse(from, out var parsed))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = parsed;
return Result.FromSuccess();
}
}

View file

@ -0,0 +1,20 @@
using System.Text.Json.Nodes;
using Boyfriend.locale;
using Remora.Commands.Parsers;
using Remora.Results;
namespace Boyfriend.Data.Options;
public class TimeSpanOption : Option<TimeSpan> {
private static readonly TimeSpanParser Parser = new();
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
public override Result Set(JsonNode settings, string from) {
if (!Parser.TryParseAsync(from).Result.IsDefined(out var span))
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
settings[Name] = span.ToString();
return Result.FromSuccess();
}
}