refactor(bot): separate MatchClear from ConfirmClear

This commit is contained in:
spiral 2022-12-01 07:16:36 +00:00
parent f06fdb38ef
commit f9abcc68c4
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
9 changed files with 28 additions and 36 deletions

View File

@ -88,15 +88,7 @@ public static class ContextArgumentsExt
return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch)); return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch));
} }
public static async Task<bool> MatchClear(this Context ctx, string toClear = null) public static bool MatchClear(this Context ctx)
{
var matched = ctx.MatchClearInner();
if (matched && toClear != null)
return await ctx.ConfirmClear(toClear);
return matched;
}
private static bool MatchClearInner(this Context ctx)
=> ctx.Match("clear", "reset", "default") || ctx.MatchFlag("c", "clear"); => ctx.Match("clear", "reset", "default") || ctx.MatchFlag("c", "clear");
public static bool MatchRaw(this Context ctx) => 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) public static bool? MatchToggleOrNull(this Context ctx, bool? defaultValue = null)
{ {
if (defaultValue != null && ctx.MatchClearInner()) if (defaultValue != null && ctx.MatchClear())
return defaultValue.Value; return defaultValue.Value;
var yesToggles = new[] { "yes", "on", "enable", "enabled", "true" }; var yesToggles = new[] { "yes", "on", "enable", "enabled", "true" };

View File

@ -128,7 +128,7 @@ public class Api
return; 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 }); await ctx.Repository.UpdateSystem(ctx.System.Id, new SystemPatch { WebhookUrl = null, WebhookToken = null });

View File

@ -173,7 +173,7 @@ public class Config
Duration? newTimeout; Duration? newTimeout;
Duration overflow = Duration.Zero; Duration overflow = Duration.Zero;
if (ctx.Match("off", "stop", "cancel", "no", "disable", "remove")) newTimeout = 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 else
{ {
var timeoutStr = ctx.RemainderOrNull(); var timeoutStr = ctx.RemainderOrNull();
@ -204,7 +204,7 @@ public class Config
{ {
if (ctx.System == null) throw Errors.NoSystemError; if (ctx.System == null) throw Errors.NoSystemError;
if (await ctx.MatchClear()) if (ctx.MatchClear())
{ {
await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { UiTz = "UTC" }); await ctx.Repository.UpdateSystemConfig(ctx.System.Id, new() { UiTz = "UTC" });

View File

@ -170,7 +170,7 @@ public class Groups
ctx.CheckOwnGroup(target); 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<string>.Null() }; var patch = new GroupPatch { DisplayName = Partial<string>.Null() };
await ctx.Repository.UpdateGroup(target.Id, patch); await ctx.Repository.UpdateGroup(target.Id, patch);
@ -228,7 +228,7 @@ public class Groups
ctx.CheckOwnGroup(target); 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<string>.Null() }; var patch = new GroupPatch { Description = Partial<string>.Null() };
await ctx.Repository.UpdateGroup(target.Id, patch); 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(); await ClearIcon();
else if (await ctx.MatchImage() is { } img) else if (await ctx.MatchImage() is { } img)
await SetIcon(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(); await ClearBannerImage();
else if (await ctx.MatchImage() is { } img) else if (await ctx.MatchImage() is { } img)
await SetBannerImage(img); await SetBannerImage(img);
@ -380,7 +380,7 @@ public class Groups
{ {
var isOwnSystem = ctx.System?.Id == target.System; var isOwnSystem = ctx.System?.Id == target.System;
var matchedRaw = ctx.MatchRaw(); var matchedRaw = ctx.MatchRaw();
var matchedClear = await ctx.MatchClear(); var matchedClear = ctx.MatchClear();
if (!isOwnSystem || !(ctx.HasNext() || matchedClear)) if (!isOwnSystem || !(ctx.HasNext() || matchedClear))
{ {

View File

@ -91,7 +91,7 @@ public class MemberAvatar
MemberGuildSettings? guildData) MemberGuildSettings? guildData)
{ {
// First, see if we need to *clear* // 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 server avatar"
: "this member's avatar")) : "this member's avatar"))
{ {

View File

@ -97,7 +97,7 @@ public class MemberEdit
ctx.CheckOwnMember(target); 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<string>.Null() }; var patch = new MemberPatch { Description = Partial<string>.Null() };
await ctx.Repository.UpdateMember(target.Id, patch); await ctx.Repository.UpdateMember(target.Id, patch);
@ -149,7 +149,7 @@ public class MemberEdit
ctx.CheckOwnMember(target); 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<string>.Null() }; var patch = new MemberPatch { Pronouns = Partial<string>.Null() };
await ctx.Repository.UpdateMember(target.Id, patch); 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(); await ClearBannerImage();
else if (await ctx.MatchImage() is { } img) else if (await ctx.MatchImage() is { } img)
await SetBannerImage(img); await SetBannerImage(img);
@ -229,7 +229,7 @@ public class MemberEdit
{ {
var isOwnSystem = ctx.System?.Id == target.System; var isOwnSystem = ctx.System?.Id == target.System;
var matchedRaw = ctx.MatchRaw(); var matchedRaw = ctx.MatchRaw();
var matchedClear = await ctx.MatchClear(); var matchedClear = ctx.MatchClear();
if (!isOwnSystem || !(ctx.HasNext() || matchedClear)) if (!isOwnSystem || !(ctx.HasNext() || matchedClear))
{ {
@ -277,7 +277,7 @@ public class MemberEdit
public async Task Birthday(Context ctx, PKMember target) 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); ctx.CheckOwnMember(target);
@ -408,7 +408,7 @@ public class MemberEdit
ctx.CheckOwnMember(target); 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<string>.Null() }; var patch = new MemberPatch { DisplayName = Partial<string>.Null() };
await ctx.Repository.UpdateMember(target.Id, patch); await ctx.Repository.UpdateMember(target.Id, patch);
@ -468,7 +468,7 @@ public class MemberEdit
ctx.CheckOwnMember(target); 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 }); await ctx.Repository.UpdateMemberGuild(target.Id, ctx.Guild.Id, new MemberGuildPatch { DisplayName = null });

View File

@ -34,7 +34,7 @@ public class MemberProxy
} }
// "Sub"command: clear flag // "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 we already have multiple tags, this would clear everything, so prompt that
if (target.ProxyTags.Count > 1) if (target.ProxyTags.Count > 1)

View File

@ -23,7 +23,7 @@ public class ServerConfig
await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server"); await ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");
var settings = await ctx.Repository.GetGuild(ctx.Guild.Id); 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.Repository.UpdateGuild(ctx.Guild.Id, new GuildPatch { LogChannel = null });
await ctx.Reply($"{Emojis.Success} Proxy logging channel cleared."); await ctx.Reply($"{Emojis.Success} Proxy logging channel cleared.");

View File

@ -57,7 +57,7 @@ public class SystemEdit
ctx.CheckSystem().CheckOwnSystem(target); 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 }); await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Name = null });
@ -113,7 +113,7 @@ public class SystemEdit
ctx.CheckSystem().CheckOwnSystem(target); 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 }); 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 isOwnSystem = ctx.System?.Id == target.Id;
var matchedRaw = ctx.MatchRaw(); var matchedRaw = ctx.MatchRaw();
var matchedClear = await ctx.MatchClear(); var matchedClear = ctx.MatchClear();
if (!isOwnSystem || !(ctx.HasNext() || matchedClear)) if (!isOwnSystem || !(ctx.HasNext() || matchedClear))
{ {
@ -210,7 +210,7 @@ public class SystemEdit
ctx.CheckSystem().CheckOwnSystem(target); 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 }); await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Tag = null });
@ -337,7 +337,7 @@ public class SystemEdit
return str; return str;
} }
if (await ctx.MatchClear("your system's server tag")) if (ctx.MatchClear() && await ctx.ConfirmClear("your system's server tag"))
await Clear(); await Clear();
else if (ctx.Match("disable") || ctx.MatchFlag("disable")) else if (ctx.Match("disable") || ctx.MatchFlag("disable"))
await EnableDisable(false); await EnableDisable(false);
@ -384,7 +384,7 @@ public class SystemEdit
ctx.CheckSystem().CheckOwnSystem(target); 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 }); await ctx.Repository.UpdateSystem(target.Id, new SystemPatch { Pronouns = null });
@ -463,7 +463,7 @@ public class SystemEdit
return; return;
} }
if (await ctx.MatchClear("your system's icon")) if (ctx.MatchClear() && await ctx.ConfirmClear("your system's icon"))
await ClearIcon(); await ClearIcon();
else if (await ctx.MatchImage() is { } img) else if (await ctx.MatchImage() is { } img)
await SetIcon(img); await SetIcon(img);
@ -501,7 +501,7 @@ public class SystemEdit
ctx.CheckSystem().CheckOwnSystem(target); 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.Repository.UpdateSystem(target.Id, new SystemPatch { BannerImage = null });
await ctx.Reply($"{Emojis.Success} System banner image cleared."); await ctx.Reply($"{Emojis.Success} System banner image cleared.");