PluralKit/PluralKit.Core/Models/SystemGuildSettings.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2021-10-13 05:02:34 +00:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
2021-10-13 05:02:34 +00:00
namespace PluralKit.Core;
[JsonConverter(typeof(StringEnumConverter))]
public enum AutoproxyMode
{
Off = 1,
Front = 2,
Latch = 3,
Member = 4
}
public class SystemGuildSettings
{
public ulong Guild { get; }
public SystemId System { get; }
public bool ProxyEnabled { get; } = true;
public AutoproxyMode AutoproxyMode { get; } = AutoproxyMode.Off;
public MemberId? AutoproxyMember { get; }
2021-08-27 15:03:47 +00:00
public string? Tag { get; }
public bool TagEnabled { get; }
}
public static class SystemGuildExt
{
public static JObject ToJson(this SystemGuildSettings settings, string? memberHid = null)
{
var o = new JObject();
o.Add("proxying_enabled", settings.ProxyEnabled);
o.Add("autoproxy_mode", settings.AutoproxyMode.ToString().ToLower());
o.Add("autoproxy_member", memberHid);
o.Add("tag", settings.Tag);
o.Add("tag_enabled", settings.TagEnabled);
2021-08-27 15:03:47 +00:00
return o;
}
2021-10-13 05:02:34 +00:00
public static (AutoproxyMode?, ValidationError?) ParseAutoproxyMode(this JToken o)
2021-10-13 05:02:34 +00:00
{
if (o.Type == JTokenType.Null)
return (AutoproxyMode.Off, null);
if (o.Type != JTokenType.String)
return (null, new ValidationError("autoproxy_mode"));
2021-10-13 05:02:34 +00:00
var value = o.Value<string>();
2021-10-13 05:02:34 +00:00
switch (value)
2021-10-13 05:02:34 +00:00
{
case "off":
return (AutoproxyMode.Off, null);
case "front":
return (AutoproxyMode.Front, null);
case "latch":
return (AutoproxyMode.Latch, null);
case "member":
return (AutoproxyMode.Member, null);
default:
return (null,
new ValidationError("autoproxy_mode", $"Value '{value}' is not a valid autoproxy mode."));
2021-10-13 05:02:34 +00:00
}
}
}