From 299f6b2edf49da5ddcf62704ab5379c83940f779 Mon Sep 17 00:00:00 2001 From: Ske Date: Tue, 7 Jul 2020 19:51:19 +0200 Subject: [PATCH] Extract member list argument parsing to utility method --- .../CommandSystem/ContextArgumentsExt.cs | 26 +++++++++++++++++++ PluralKit.Bot/Commands/Switch.cs | 16 +----------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs b/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs index 0a8ac3bb..87325251 100644 --- a/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs +++ b/PluralKit.Bot/CommandSystem/ContextArgumentsExt.cs @@ -1,5 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; + +using PluralKit.Core; namespace PluralKit.Bot { @@ -55,5 +59,27 @@ namespace PluralKit.Bot var flags = ctx.Parameters.Flags(); return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch)); } + + public static async Task> ParseMemberList(this Context ctx, SystemId? restrictToSystem) + { + var members = new List(); + + // Loop through all the given arguments + while (ctx.HasNext()) + { + // and attempt to match a member + var member = await ctx.MatchMember(); + if (member == null) + // if we can't, big error. Every member name must be valid. + throw new PKError(ctx.CreateMemberNotFoundError(ctx.PopArgument())); + + if (restrictToSystem != null && member.System != restrictToSystem) + throw Errors.NotOwnMemberError; // TODO: name *which* member? + + members.Add(member); // Then add to the final output list + } + + return members; + } } } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/Switch.cs b/PluralKit.Bot/Commands/Switch.cs index 1118ed8a..ae3b10e3 100644 --- a/PluralKit.Bot/Commands/Switch.cs +++ b/PluralKit.Bot/Commands/Switch.cs @@ -23,22 +23,8 @@ namespace PluralKit.Bot public async Task SwitchDo(Context ctx) { ctx.CheckSystem(); - var members = new List(); - // Loop through all the given arguments - while (ctx.HasNext()) - { - // and attempt to match a member - var member = await ctx.MatchMember(); - if (member == null) - // if we can't, big error. Every member name must be valid. - throw new PKError(ctx.CreateMemberNotFoundError(ctx.PopArgument())); - - ctx.CheckOwnMember(member); // Ensure they're in our own system - members.Add(member); // Then add to the final output list - } - - // Finally, do the actual switch + var members = await ctx.ParseMemberList(ctx.System.Id); await DoSwitchCommand(ctx, members); } public async Task SwitchOut(Context ctx)