This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/src/Data/Options/LanguageOption.cs
Octol1ttle daa1dd4184
General fixes (#54)
Depends on #55

These changes are really small and I don't know how to reasonably split
them into multiple PRs, but here's the changelog:
- The result of the `UtilityService#LogActionAsync` call in
`BanCommandGroup#BanUserAsync` is no longer ignored and it's errors will
now prevent feedback from being sent;
- Converted `if` statement to a `return` with ternary in
`LanguageOption`;
- Slightly decreased method complexity of
`MessageDeletedResponder#RespondAsync` by cleverly using Results;
- Changed the order of parameters for `UtilityService#LogActionAsync` to
flow better;
- Removed the exemption for `ConvertIfToReturnStatement` for
InspectCode.

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
2023-07-20 07:36:21 +00:00

34 lines
1.2 KiB
C#

using System.Globalization;
using System.Text.Json.Nodes;
using Remora.Discord.Extensions.Formatting;
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]) { }
public override string Display(JsonNode settings) {
return Markdown.InlineCode(settings[Name]?.GetValue<string>() ?? "en");
}
/// <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) {
return CultureInfoCache.ContainsKey(from.ToLowerInvariant())
? base.Set(settings, from.ToLowerInvariant())
: Result.FromError(new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported));
}
}