Extract member list argument parsing to utility method

This commit is contained in:
Ske 2020-07-07 19:51:19 +02:00
parent 36ed356d2b
commit 299f6b2edf
2 changed files with 27 additions and 15 deletions

View File

@ -1,5 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using PluralKit.Core;
namespace PluralKit.Bot namespace PluralKit.Bot
{ {
@ -55,5 +59,27 @@ namespace PluralKit.Bot
var flags = ctx.Parameters.Flags(); var flags = ctx.Parameters.Flags();
return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch)); return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch));
} }
public static async Task<List<PKMember>> ParseMemberList(this Context ctx, SystemId? restrictToSystem)
{
var members = new List<PKMember>();
// 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;
}
} }
} }

View File

@ -23,22 +23,8 @@ namespace PluralKit.Bot
public async Task SwitchDo(Context ctx) public async Task SwitchDo(Context ctx)
{ {
ctx.CheckSystem(); ctx.CheckSystem();
var members = new List<PKMember>();
// Loop through all the given arguments var members = await ctx.ParseMemberList(ctx.System.Id);
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
await DoSwitchCommand(ctx, members); await DoSwitchCommand(ctx, members);
} }
public async Task SwitchOut(Context ctx) public async Task SwitchOut(Context ctx)