From 1ce9227b7d8214c270273c7318f949fe96de0be1 Mon Sep 17 00:00:00 2001 From: Iris System Date: Sun, 28 Apr 2024 21:05:46 +1200 Subject: [PATCH] feat(bot): add option for hid capitalization --- PluralKit.Bot/CommandMeta/CommandTree.cs | 2 ++ PluralKit.Bot/Commands/Config.cs | 21 +++++++++++++++++++ PluralKit.Bot/Utils/ModelUtils.cs | 7 ++++++- PluralKit.Core/Database/Migrations/42.sql | 1 + PluralKit.Core/Database/Utils/HidUtils.cs | 5 ++++- .../Models/Patch/SystemConfigPatch.cs | 8 +++++++ PluralKit.Core/Models/SystemConfig.cs | 2 ++ 7 files changed, 44 insertions(+), 2 deletions(-) diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index e82eb189..8296b2b2 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -540,6 +540,8 @@ public partial class CommandTree return ctx.Execute(null, m => m.ProxyErrorMessageEnabled(ctx)); if (ctx.MatchMultiple(new[] { "split" }, new[] { "id", "ids" }) || ctx.Match("sid")) return ctx.Execute(null, m => m.HidDisplaySplit(ctx)); + if (ctx.MatchMultiple(new[] { "caps", "capitalize", "capitalise" }, new[] { "id", "ids" }) || ctx.Match("capid")) + return ctx.Execute(null, m => m.HidDisplayCaps(ctx)); // todo: maybe add the list of configuration keys here? return ctx.Reply($"{Emojis.Error} Could not find a setting with that name. Please see `pk;commands config` for the list of possible config settings."); diff --git a/PluralKit.Bot/Commands/Config.cs b/PluralKit.Bot/Commands/Config.cs index f6de21ca..08b49693 100644 --- a/PluralKit.Bot/Commands/Config.cs +++ b/PluralKit.Bot/Commands/Config.cs @@ -109,6 +109,13 @@ public class Config "disabled" )); + items.Add(new( + "Capitalize IDs", + "Whether to display IDs as capital letters, to ease readability", + EnabledDisabled(ctx.Config.HidDisplayCaps), + "disabled" + )); + await ctx.Paginate( items.ToAsyncEnumerable(), items.Count, @@ -464,4 +471,18 @@ public class Config await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { HidDisplaySplit = newVal }); await ctx.Reply($"Splitting of 6-character IDs with a hyphen is now {EnabledDisabled(newVal)}."); } + + public async Task HidDisplayCaps(Context ctx) + { + if (!ctx.HasNext()) + { + var msg = $"Displaying IDs as capital letters is currently **{EnabledDisabled(ctx.Config.HidDisplayCaps)}**."; + await ctx.Reply(msg); + return; + } + + var newVal = ctx.MatchToggle(true); + await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { HidDisplayCaps = newVal }); + await ctx.Reply($"Displaying IDs as capital letters is now {EnabledDisabled(newVal)}."); + } } \ No newline at end of file diff --git a/PluralKit.Bot/Utils/ModelUtils.cs b/PluralKit.Bot/Utils/ModelUtils.cs index c27432b9..2576c77a 100644 --- a/PluralKit.Bot/Utils/ModelUtils.cs +++ b/PluralKit.Bot/Utils/ModelUtils.cs @@ -31,7 +31,12 @@ public static class ModelUtils public static string DisplayHid(this PKSystem system, SystemConfig? cfg = null) => HidTransform(system.Hid, cfg); public static string DisplayHid(this PKGroup group, SystemConfig? cfg = null) => HidTransform(group.Hid, cfg); public static string DisplayHid(this PKMember member, SystemConfig? cfg = null) => HidTransform(member.Hid, cfg); - private static string HidTransform(string hid, SystemConfig? cfg = null) => HidUtils.HidTransform(hid, cfg != null && cfg.HidDisplaySplit); + private static string HidTransform(string hid, SystemConfig? cfg = null) => + HidUtils.HidTransform( + hid, + cfg != null && cfg.HidDisplaySplit, + cfg != null && cfg.HidDisplayCaps + ); private static string EntityReference(string hid, string name) { diff --git a/PluralKit.Core/Database/Migrations/42.sql b/PluralKit.Core/Database/Migrations/42.sql index 18a93e9f..9a2aaf81 100644 --- a/PluralKit.Core/Database/Migrations/42.sql +++ b/PluralKit.Core/Database/Migrations/42.sql @@ -6,5 +6,6 @@ alter table members alter column hid type char(6) using rpad(hid, 6, ' '); alter table groups alter column hid type char(6) using rpad(hid, 6, ' '); alter table system_config add column hid_display_split bool default false; +alter table system_config add column hid_display_caps bool default false; update info set schema_version = 42; \ No newline at end of file diff --git a/PluralKit.Core/Database/Utils/HidUtils.cs b/PluralKit.Core/Database/Utils/HidUtils.cs index 9d7f30a2..15945226 100644 --- a/PluralKit.Core/Database/Utils/HidUtils.cs +++ b/PluralKit.Core/Database/Utils/HidUtils.cs @@ -22,7 +22,7 @@ public static class HidUtils return hid != null; } - public static string HidTransform(string input, bool split = false) + public static string HidTransform(string input, bool split, bool caps) { if (split && input.Length > 5) { @@ -30,6 +30,9 @@ public static class HidUtils input = string.Concat(input.AsSpan(0, len), "-", input.AsSpan(len)); } + if (caps) + input = input.ToUpper(); + return input; } } \ No newline at end of file diff --git a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs index 5f5d5b97..482b5a51 100644 --- a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs +++ b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs @@ -20,6 +20,7 @@ public class SystemConfigPatch: PatchObject public Partial CaseSensitiveProxyTags { get; set; } public Partial ProxyErrorMessageEnabled { get; set; } public Partial HidDisplaySplit { get; set; } + public Partial HidDisplayCaps { get; set; } public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper @@ -35,6 +36,7 @@ public class SystemConfigPatch: PatchObject .With("case_sensitive_proxy_tags", CaseSensitiveProxyTags) .With("proxy_error_message_enabled", ProxyErrorMessageEnabled) .With("hid_display_split", HidDisplaySplit) + .With("hid_display_caps", HidDisplayCaps) ); public new void AssertIsValid() @@ -93,6 +95,9 @@ public class SystemConfigPatch: PatchObject if (HidDisplaySplit.IsPresent) o.Add("hid_display_split", HidDisplaySplit.Value); + if (HidDisplayCaps.IsPresent) + o.Add("hid_display_caps", HidDisplayCaps.Value); + return o; } @@ -127,6 +132,9 @@ public class SystemConfigPatch: PatchObject if (o.ContainsKey("hid_display_split")) patch.HidDisplaySplit = o.Value("hid_display_split"); + if (o.ContainsKey("hid_display_caps")) + patch.HidDisplayCaps = o.Value("hid_display_caps"); + return patch; } } \ No newline at end of file diff --git a/PluralKit.Core/Models/SystemConfig.cs b/PluralKit.Core/Models/SystemConfig.cs index c4b8e292..ed7f3dbd 100644 --- a/PluralKit.Core/Models/SystemConfig.cs +++ b/PluralKit.Core/Models/SystemConfig.cs @@ -22,6 +22,7 @@ public class SystemConfig public bool CaseSensitiveProxyTags { get; } public bool ProxyErrorMessageEnabled { get; } public bool HidDisplaySplit { get; } + public bool HidDisplayCaps { get; } } public static class SystemConfigExt @@ -41,6 +42,7 @@ public static class SystemConfigExt o.Add("case_sensitive_proxy_tags", cfg.CaseSensitiveProxyTags); o.Add("proxy_error_message_enabled", cfg.ProxyErrorMessageEnabled); o.Add("hid_display_split", cfg.HidDisplaySplit); + o.Add("hid_display_caps", cfg.HidDisplayCaps); o.Add("description_templates", JArray.FromObject(cfg.DescriptionTemplates));