diff --git a/PluralKit.Bot/CommandMeta/CommandTree.cs b/PluralKit.Bot/CommandMeta/CommandTree.cs index e5af4704..7d93b3d3 100644 --- a/PluralKit.Bot/CommandMeta/CommandTree.cs +++ b/PluralKit.Bot/CommandMeta/CommandTree.cs @@ -142,9 +142,7 @@ public partial class CommandTree private async Task HandleSystemCommand(Context ctx) { // these commands never take a system target - if (!ctx.HasNext()) - await ctx.Execute(SystemInfo, m => m.Query(ctx, ctx.System)); - else if (ctx.Match("new", "create", "make", "add", "register", "init", "n")) + if (ctx.Match("new", "create", "make", "add", "register", "init", "n")) await ctx.Execute(SystemNew, m => m.New(ctx)); else if (ctx.Match("commands", "help")) await PrintCommandList(ctx, "systems", SystemCommands); @@ -161,7 +159,7 @@ public partial class CommandTree else if (ctx.Match("proxy")) await ctx.Execute(SystemProxy, m => m.SystemProxy(ctx)); - // finally, parse commands that *do* take a system target + // finally, parse commands that *can* take a system target else { // try matching a system ID @@ -172,11 +170,20 @@ public partial class CommandTree // we skip the `target != null` check here since the argument isn't be popped if it's not a system if (!ctx.HasNext()) { - await ctx.Execute(SystemInfo, m => m.Query(ctx, target)); + await ctx.Execute(SystemInfo, m => m.Query(ctx, target ?? ctx.System)); return; } - await HandleSystemCommandTargeted(ctx, target ?? ctx.System); + // hacky, but we need to CheckSystem(target) which throws a PKError + try + { + await HandleSystemCommandTargeted(ctx, target ?? ctx.System); + } + catch (PKError e) + { + await ctx.Reply($"{Emojis.Error} {e.Message}"); + return; + } // if we *still* haven't matched anything, the user entered an invalid command name or system reference if (ctx.Parameters._ptr == previousPtr) @@ -197,44 +204,44 @@ public partial class CommandTree private async Task HandleSystemCommandTargeted(Context ctx, PKSystem target) { if (ctx.Match("name", "rename", "changename")) - await ctx.Execute(SystemRename, m => m.Name(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemRename, m => m.Name(ctx, target)); else if (ctx.Match("tag")) - await ctx.Execute(SystemTag, m => m.Tag(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemTag, m => m.Tag(ctx, target)); else if (ctx.Match("servertag")) - await ctx.Execute(SystemServerTag, m => m.ServerTag(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemServerTag, m => m.ServerTag(ctx, target)); else if (ctx.Match("description", "desc", "bio")) - await ctx.Execute(SystemDesc, m => m.Description(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemDesc, m => m.Description(ctx, target)); else if (ctx.Match("color", "colour")) - await ctx.Execute(SystemColor, m => m.Color(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemColor, m => m.Color(ctx, target)); else if (ctx.Match("banner", "splash", "cover")) - await ctx.Execute(SystemBannerImage, m => m.BannerImage(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemBannerImage, m => m.BannerImage(ctx, target)); else if (ctx.Match("avatar", "picture", "icon", "image", "pic", "pfp")) - await ctx.Execute(SystemAvatar, m => m.Avatar(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemAvatar, m => m.Avatar(ctx, target)); else if (ctx.Match("list", "l", "members")) - await ctx.Execute(SystemList, m => m.MemberList(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemList, m => m.MemberList(ctx, target)); else if (ctx.Match("find", "search", "query", "fd", "s")) - await ctx.Execute(SystemFind, m => m.MemberList(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemFind, m => m.MemberList(ctx, target)); else if (ctx.Match("f", "front", "fronter", "fronters")) { if (ctx.Match("h", "history")) - await ctx.Execute(SystemFrontHistory, m => m.SystemFrontHistory(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemFrontHistory, m => m.SystemFrontHistory(ctx, target)); else if (ctx.Match("p", "percent", "%")) - await ctx.Execute(SystemFrontPercent, m => m.FrontPercent(ctx, system: target)); + await ctx.CheckSystem(target).Execute(SystemFrontPercent, m => m.FrontPercent(ctx, system: target)); else - await ctx.Execute(SystemFronter, m => m.SystemFronter(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemFronter, m => m.SystemFronter(ctx, target)); } else if (ctx.Match("fh", "fronthistory", "history", "switches")) - await ctx.Execute(SystemFrontHistory, m => m.SystemFrontHistory(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemFrontHistory, m => m.SystemFrontHistory(ctx, target)); else if (ctx.Match("fp", "frontpercent", "front%", "frontbreakdown")) - await ctx.Execute(SystemFrontPercent, m => m.FrontPercent(ctx, system: target)); + await ctx.CheckSystem(target).Execute(SystemFrontPercent, m => m.FrontPercent(ctx, system: target)); else if (ctx.Match("info", "view", "show")) - await ctx.Execute(SystemInfo, m => m.Query(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemInfo, m => m.Query(ctx, target)); else if (ctx.Match("groups", "gs")) - await ctx.Execute(GroupList, g => g.ListSystemGroups(ctx, target)); + await ctx.CheckSystem(target).Execute(GroupList, g => g.ListSystemGroups(ctx, target)); else if (ctx.Match("privacy")) - await ctx.Execute(SystemPrivacy, m => m.SystemPrivacy(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemPrivacy, m => m.SystemPrivacy(ctx, target)); else if (ctx.Match("delete", "remove", "destroy", "erase", "yeet")) - await ctx.Execute(SystemDelete, m => m.Delete(ctx, target)); + await ctx.CheckSystem(target).Execute(SystemDelete, m => m.Delete(ctx, target)); } private async Task HandleMemberCommand(Context ctx) diff --git a/PluralKit.Bot/CommandSystem/Context/ContextChecksExt.cs b/PluralKit.Bot/CommandSystem/Context/ContextChecksExt.cs index 1d48e057..a4f693e1 100644 --- a/PluralKit.Bot/CommandSystem/Context/ContextChecksExt.cs +++ b/PluralKit.Bot/CommandSystem/Context/ContextChecksExt.cs @@ -55,6 +55,13 @@ public static class ContextChecksExt return ctx; } + public static Context CheckSystem(this Context ctx, PKSystem system) + { + if (system == null) + throw Errors.NoSystemError; + return ctx; + } + public static Context CheckNoSystem(this Context ctx) { if (ctx.System != null)