From f9abcc68c4f4e0b4b190cf2dfefcf752cc84ef47 Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 1 Dec 2022 07:16:36 +0000 Subject: [PATCH] refactor(bot): separate MatchClear from ConfirmClear --- .../CommandSystem/Context/ContextArgumentsExt.cs | 12 ++---------- PluralKit.Bot/Commands/Api.cs | 2 +- PluralKit.Bot/Commands/Config.cs | 4 ++-- PluralKit.Bot/Commands/Groups.cs | 10 +++++----- PluralKit.Bot/Commands/MemberAvatar.cs | 2 +- PluralKit.Bot/Commands/MemberEdit.cs | 14 +++++++------- PluralKit.Bot/Commands/MemberProxy.cs | 2 +- PluralKit.Bot/Commands/ServerConfig.cs | 2 +- PluralKit.Bot/Commands/SystemEdit.cs | 16 ++++++++-------- 9 files changed, 28 insertions(+), 36 deletions(-) diff --git a/PluralKit.Bot/CommandSystem/Context/ContextArgumentsExt.cs b/PluralKit.Bot/CommandSystem/Context/ContextArgumentsExt.cs index d877dfb7..9a93440b 100644 --- a/PluralKit.Bot/CommandSystem/Context/ContextArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/Context/ContextArgumentsExt.cs @@ -88,15 +88,7 @@ public static class ContextArgumentsExt return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch)); } - public static async Task MatchClear(this Context ctx, string toClear = null) - { - var matched = ctx.MatchClearInner(); - if (matched && toClear != null) - return await ctx.ConfirmClear(toClear); - return matched; - } - - private static bool MatchClearInner(this Context ctx) + public static bool MatchClear(this Context ctx) => ctx.Match("clear", "reset", "default") || ctx.MatchFlag("c", "clear"); public static bool MatchRaw(this Context ctx) => @@ -111,7 +103,7 @@ public static class ContextArgumentsExt public static bool? MatchToggleOrNull(this Context ctx, bool? defaultValue = null) { - if (defaultValue != null && ctx.MatchClearInner()) + if (defaultValue != null && ctx.MatchClear()) return defaultValue.Value; var yesToggles = new[] { "yes", "on", "enable", "enabled", "true" }; diff --git a/PluralKit.Bot/Commands/Api.cs b/PluralKit.Bot/Commands/Api.cs index 2396a347..f4ac4caa 100644 --- a/PluralKit.Bot/Commands/Api.cs +++ b/PluralKit.Bot/Commands/Api.cs @@ -128,7 +128,7 @@ public class Api return; } - if (await ctx.MatchClear("your system's webhook URL")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's webhook URL")) { await ctx.Repository.UpdateSystem(ctx.System.Id, new SystemPatch { WebhookUrl = null, WebhookToken = null }); diff --git a/PluralKit.Bot/Commands/Config.cs b/PluralKit.Bot/Commands/Config.cs index ecf960e5..c69f5843 100644 --- a/PluralKit.Bot/Commands/Config.cs +++ b/PluralKit.Bot/Commands/Config.cs @@ -173,7 +173,7 @@ public class Config Duration? newTimeout; Duration overflow = Duration.Zero; if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeout = Duration.Zero; - else if (await ctx.MatchClear()) newTimeout = null; + else if (ctx.MatchClear()) newTimeout = null; else { var timeoutStr = ctx.RemainderOrNull(); @@ -204,7 +204,7 @@ public class Config { if (ctx.System == null) throw Errors.NoSystemError; - if (await ctx.MatchClear()) + if (ctx.MatchClear()) { await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { UiTz = "UTC" }); diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 94b7eab0..4041f17c 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -170,7 +170,7 @@ public class Groups ctx.CheckOwnGroup(target); - if (await ctx.MatchClear("this group's display name")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this group's display name")) { var patch = new GroupPatch { DisplayName = Partial.Null() }; await ctx.Repository.UpdateGroup(target.Id, patch); @@ -228,7 +228,7 @@ public class Groups ctx.CheckOwnGroup(target); - if (await ctx.MatchClear("this group's description")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this group's description")) { var patch = new GroupPatch { Description = Partial.Null() }; await ctx.Repository.UpdateGroup(target.Id, patch); @@ -304,7 +304,7 @@ public class Groups } } - if (await ctx.MatchClear("this group's icon")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this group's icon")) await ClearIcon(); else if (await ctx.MatchImage() is { } img) await SetIcon(img); @@ -368,7 +368,7 @@ public class Groups } } - if (await ctx.MatchClear("this group's banner image")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this group's banner image")) await ClearBannerImage(); else if (await ctx.MatchImage() is { } img) await SetBannerImage(img); @@ -380,7 +380,7 @@ public class Groups { var isOwnSystem = ctx.System?.Id == target.System; var matchedRaw = ctx.MatchRaw(); - var matchedClear = await ctx.MatchClear(); + var matchedClear = ctx.MatchClear(); if (!isOwnSystem || !(ctx.HasNext() || matchedClear)) { diff --git a/PluralKit.Bot/Commands/MemberAvatar.cs b/PluralKit.Bot/Commands/MemberAvatar.cs index 612ef816..cce0dd9e 100644 --- a/PluralKit.Bot/Commands/MemberAvatar.cs +++ b/PluralKit.Bot/Commands/MemberAvatar.cs @@ -91,7 +91,7 @@ public class MemberAvatar MemberGuildSettings? guildData) { // First, see if we need to *clear* - if (await ctx.MatchClear(location == AvatarLocation.Server + if (ctx.MatchClear() && await ctx.ConfirmClear(location == AvatarLocation.Server ? "this member's server avatar" : "this member's avatar")) { diff --git a/PluralKit.Bot/Commands/MemberEdit.cs b/PluralKit.Bot/Commands/MemberEdit.cs index ebc16ae8..717d29bb 100644 --- a/PluralKit.Bot/Commands/MemberEdit.cs +++ b/PluralKit.Bot/Commands/MemberEdit.cs @@ -97,7 +97,7 @@ public class MemberEdit ctx.CheckOwnMember(target); - if (await ctx.MatchClear("this member's description")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this member's description")) { var patch = new MemberPatch { Description = Partial.Null() }; await ctx.Repository.UpdateMember(target.Id, patch); @@ -149,7 +149,7 @@ public class MemberEdit ctx.CheckOwnMember(target); - if (await ctx.MatchClear("this member's pronouns")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this member's pronouns")) { var patch = new MemberPatch { Pronouns = Partial.Null() }; await ctx.Repository.UpdateMember(target.Id, patch); @@ -217,7 +217,7 @@ public class MemberEdit } } - if (await ctx.MatchClear("this member's banner image")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this member's banner image")) await ClearBannerImage(); else if (await ctx.MatchImage() is { } img) await SetBannerImage(img); @@ -229,7 +229,7 @@ public class MemberEdit { var isOwnSystem = ctx.System?.Id == target.System; var matchedRaw = ctx.MatchRaw(); - var matchedClear = await ctx.MatchClear(); + var matchedClear = ctx.MatchClear(); if (!isOwnSystem || !(ctx.HasNext() || matchedClear)) { @@ -277,7 +277,7 @@ public class MemberEdit public async Task Birthday(Context ctx, PKMember target) { - if (await ctx.MatchClear("this member's birthday")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this member's birthday")) { ctx.CheckOwnMember(target); @@ -408,7 +408,7 @@ public class MemberEdit ctx.CheckOwnMember(target); - if (await ctx.MatchClear("this member's display name")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this member's display name")) { var patch = new MemberPatch { DisplayName = Partial.Null() }; await ctx.Repository.UpdateMember(target.Id, patch); @@ -468,7 +468,7 @@ public class MemberEdit ctx.CheckOwnMember(target); - if (await ctx.MatchClear("this member's server name")) + if (ctx.MatchClear() && await ctx.ConfirmClear("this member's server name")) { await ctx.Repository.UpdateMemberGuild(target.Id, ctx.Guild.Id, new MemberGuildPatch { DisplayName = null }); diff --git a/PluralKit.Bot/Commands/MemberProxy.cs b/PluralKit.Bot/Commands/MemberProxy.cs index 953d53d8..02ee4555 100644 --- a/PluralKit.Bot/Commands/MemberProxy.cs +++ b/PluralKit.Bot/Commands/MemberProxy.cs @@ -34,7 +34,7 @@ public class MemberProxy } // "Sub"command: clear flag - if (await ctx.MatchClear()) + if (ctx.MatchClear()) { // If we already have multiple tags, this would clear everything, so prompt that if (target.ProxyTags.Count > 1) diff --git a/PluralKit.Bot/Commands/ServerConfig.cs b/PluralKit.Bot/Commands/ServerConfig.cs index 48ce8ee9..6d11ea01 100644 --- a/PluralKit.Bot/Commands/ServerConfig.cs +++ b/PluralKit.Bot/Commands/ServerConfig.cs @@ -23,7 +23,7 @@ public class ServerConfig await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server"); var settings = await ctx.Repository.GetGuild(ctx.Guild.Id); - if (await ctx.MatchClear("the server log channel")) + if (ctx.MatchClear() && await ctx.ConfirmClear("the server log channel")) { await ctx.Repository.UpdateGuild(ctx.Guild.Id, new GuildPatch { LogChannel = null }); await ctx.Reply($"{Emojis.Success} Proxy logging channel cleared."); diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 14baed6f..31136d66 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -57,7 +57,7 @@ public class SystemEdit ctx.CheckSystem().CheckOwnSystem(target); - if (await ctx.MatchClear("your system's name")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's name")) { await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Name = null }); @@ -113,7 +113,7 @@ public class SystemEdit ctx.CheckSystem().CheckOwnSystem(target); - if (await ctx.MatchClear("your system's description")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's description")) { await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Description = null }); @@ -135,7 +135,7 @@ public class SystemEdit { var isOwnSystem = ctx.System?.Id == target.Id; var matchedRaw = ctx.MatchRaw(); - var matchedClear = await ctx.MatchClear(); + var matchedClear = ctx.MatchClear(); if (!isOwnSystem || !(ctx.HasNext() || matchedClear)) { @@ -210,7 +210,7 @@ public class SystemEdit ctx.CheckSystem().CheckOwnSystem(target); - if (await ctx.MatchClear("your system's tag")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's tag")) { await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Tag = null }); @@ -337,7 +337,7 @@ public class SystemEdit return str; } - if (await ctx.MatchClear("your system's server tag")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's server tag")) await Clear(); else if (ctx.Match("disable") || ctx.MatchFlag("disable")) await EnableDisable(false); @@ -384,7 +384,7 @@ public class SystemEdit ctx.CheckSystem().CheckOwnSystem(target); - if (await ctx.MatchClear("your system's pronouns")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's pronouns")) { await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Pronouns = null }); @@ -463,7 +463,7 @@ public class SystemEdit return; } - if (await ctx.MatchClear("your system's icon")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's icon")) await ClearIcon(); else if (await ctx.MatchImage() is { } img) await SetIcon(img); @@ -501,7 +501,7 @@ public class SystemEdit ctx.CheckSystem().CheckOwnSystem(target); - if (await ctx.MatchClear("your system's banner image")) + if (ctx.MatchClear() && await ctx.ConfirmClear("your system's banner image")) { await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { BannerImage = null }); await ctx.Reply($"{Emojis.Success} System banner image cleared.");