diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index 23aa9153..5b19f193 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -596,6 +596,8 @@ public partial class CommandTree return ctx.Execute(null, m => m.HidListPadding(ctx)); if (ctx.MatchMultiple(new[] { "member", "group" }, new[] { "limit" }) || ctx.Match("limit")) return ctx.Execute(null, m => m.LimitUpdate(ctx)); + if (ctx.MatchMultiple(new[] { "proxy" }, new[] { "switch" }) || ctx.Match("proxyswitch", "ps")) + return ctx.Execute(null, m => m.ProxySwitch(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 3e081f38..bfbc5b4f 100644 --- a/PluralKit.Bot/Commands/Config.cs +++ b/PluralKit.Bot/Commands/Config.cs @@ -123,6 +123,13 @@ public class Config "off" )); + items.Add(new( + "Proxy Switch", + "Whether using a proxy tag logs a switch", + EnabledDisabled(ctx.Config.ProxySwitch), + "disabled" + )); + await ctx.Paginate( items.ToAsyncEnumerable(), items.Count, @@ -537,6 +544,20 @@ public class Config else throw new PKError(badInputError); } + public async Task ProxySwitch(Context ctx) + { + if (!ctx.HasNext()) + { + var msg = $"Logging a switch every time a proxy tag is used is currently **{EnabledDisabled(ctx.Config.ProxySwitch)}**."; + await ctx.Reply(msg); + return; + } + + var newVal = ctx.MatchToggle(false); + await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { ProxySwitch = newVal }); + await ctx.Reply($"Logging a switch every time a proxy tag is used is now {EnabledDisabled(newVal)}."); + } + public Task LimitUpdate(Context ctx) { throw new PKError("You cannot update your own member or group limits. If you need a limit update, please join the " + diff --git a/PluralKit.Bot/Proxy/ProxyService.cs b/PluralKit.Bot/Proxy/ProxyService.cs index 83299d09..96a1ea93 100644 --- a/PluralKit.Bot/Proxy/ProxyService.cs +++ b/PluralKit.Bot/Proxy/ProxyService.cs @@ -506,6 +506,10 @@ public class ProxyService Task DispatchWebhook() => _dispatch.Dispatch(ctx.SystemId.Value, sentMessage); + Task MaybeLogSwitch() => (ctx.ProxySwitch && !Array.Exists(ctx.LastSwitchMembers, element => element == match.Member.Id)) + ? _db.Execute(conn => _repo.AddSwitch(conn, (SystemId)ctx.SystemId, new[] { match.Member.Id })) + : Task.CompletedTask; + async Task DeleteProxyTriggerMessage() { if (!deletePrevious) @@ -539,7 +543,8 @@ public class ProxyService UpdateMemberForSentMessage(), LogMessageToChannel(), SaveLatchAutoproxy(), - DispatchWebhook() + DispatchWebhook(), + MaybeLogSwitch() ); } diff --git a/PluralKit.Core/Database/Functions/MessageContext.cs b/PluralKit.Core/Database/Functions/MessageContext.cs index 7ac4245e..df2c0a91 100644 --- a/PluralKit.Core/Database/Functions/MessageContext.cs +++ b/PluralKit.Core/Database/Functions/MessageContext.cs @@ -31,5 +31,6 @@ public class MessageContext public int? LatchTimeout { get; } public bool CaseSensitiveProxyTags { get; } public bool ProxyErrorMessageEnabled { get; } + public bool ProxySwitch { get; } public bool DenyBotUsage { get; } } \ No newline at end of file diff --git a/PluralKit.Core/Database/Functions/functions.sql b/PluralKit.Core/Database/Functions/functions.sql index 1517a7bf..d417510f 100644 --- a/PluralKit.Core/Database/Functions/functions.sql +++ b/PluralKit.Core/Database/Functions/functions.sql @@ -9,6 +9,7 @@ latch_timeout integer, case_sensitive_proxy_tags bool, proxy_error_message_enabled bool, + proxy_switch bool, tag_enabled bool, proxy_enabled bool, @@ -40,6 +41,7 @@ as $$ system_config.latch_timeout as latch_timeout, system_config.case_sensitive_proxy_tags as case_sensitive_proxy_tags, system_config.proxy_error_message_enabled as proxy_error_message_enabled, + system_config.proxy_switch as proxy_switch, -- system_guild table coalesce(system_guild.tag_enabled, true) as tag_enabled, diff --git a/PluralKit.Core/Database/Migrations/45.sql b/PluralKit.Core/Database/Migrations/45.sql new file mode 100644 index 00000000..50e16dd6 --- /dev/null +++ b/PluralKit.Core/Database/Migrations/45.sql @@ -0,0 +1,6 @@ +-- database version 45 +-- add new config setting "proxy_switch" + +alter table system_config add column proxy_switch bool default false; + +update info set schema_version = 45; \ No newline at end of file diff --git a/PluralKit.Core/Database/Utils/DatabaseMigrator.cs b/PluralKit.Core/Database/Utils/DatabaseMigrator.cs index f42f057e..3ffb8544 100644 --- a/PluralKit.Core/Database/Utils/DatabaseMigrator.cs +++ b/PluralKit.Core/Database/Utils/DatabaseMigrator.cs @@ -9,7 +9,7 @@ namespace PluralKit.Core; internal class DatabaseMigrator { private const string RootPath = "PluralKit.Core.Database"; // "resource path" root for SQL files - private const int TargetSchemaVersion = 44; + private const int TargetSchemaVersion = 45; private readonly ILogger _logger; public DatabaseMigrator(ILogger logger) diff --git a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs index 26e5b13d..da70cbbb 100644 --- a/PluralKit.Core/Models/Patch/SystemConfigPatch.cs +++ b/PluralKit.Core/Models/Patch/SystemConfigPatch.cs @@ -22,7 +22,7 @@ public class SystemConfigPatch: PatchObject public Partial HidDisplaySplit { get; set; } public Partial HidDisplayCaps { get; set; } public Partial HidListPadding { get; set; } - + public Partial ProxySwitch { get; set; } public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper .With("ui_tz", UiTz) @@ -39,6 +39,7 @@ public class SystemConfigPatch: PatchObject .With("hid_display_split", HidDisplaySplit) .With("hid_display_caps", HidDisplayCaps) .With("hid_list_padding", HidListPadding) + .With("proxy_switch", ProxySwitch) ); public new void AssertIsValid() @@ -103,6 +104,9 @@ public class SystemConfigPatch: PatchObject if (HidListPadding.IsPresent) o.Add("hid_list_padding", HidListPadding.Value.ToUserString()); + if (ProxySwitch.IsPresent) + o.Add("proxy_switch", ProxySwitch.Value); + return o; } @@ -140,6 +144,9 @@ public class SystemConfigPatch: PatchObject if (o.ContainsKey("hid_display_caps")) patch.HidDisplayCaps = o.Value("hid_display_caps"); + if (o.ContainsKey("proxy_switch")) + patch.ProxySwitch = o.Value("proxy_switch"); + return patch; } } \ No newline at end of file diff --git a/PluralKit.Core/Models/SystemConfig.cs b/PluralKit.Core/Models/SystemConfig.cs index dfc6ba9b..10515ccd 100644 --- a/PluralKit.Core/Models/SystemConfig.cs +++ b/PluralKit.Core/Models/SystemConfig.cs @@ -24,6 +24,7 @@ public class SystemConfig public bool HidDisplaySplit { get; } public bool HidDisplayCaps { get; } public HidPadFormat HidListPadding { get; } + public bool ProxySwitch { get; } public enum HidPadFormat { @@ -52,6 +53,7 @@ public static class SystemConfigExt o.Add("hid_display_split", cfg.HidDisplaySplit); o.Add("hid_display_caps", cfg.HidDisplayCaps); o.Add("hid_list_padding", cfg.HidListPadding.ToUserString()); + o.Add("proxy_switch", cfg.ProxySwitch); o.Add("description_templates", JArray.FromObject(cfg.DescriptionTemplates));