feat: don't try to match other systems' members in switch / group add commands

This commit is contained in:
spiral 2021-09-13 03:14:59 -04:00
parent dec228d5bd
commit a2bf70b395
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
4 changed files with 22 additions and 17 deletions

View File

@ -104,14 +104,12 @@ namespace PluralKit.Bot
while (ctx.HasNext()) while (ctx.HasNext())
{ {
// and attempt to match a member // and attempt to match a member
var member = await ctx.MatchMember(); var member = await ctx.MatchMember(restrictToSystem);
if (member == null) if (member == null)
// if we can't, big error. Every member name must be valid. // if we can't, big error. Every member name must be valid.
throw new PKError(ctx.CreateMemberNotFoundError(ctx.PopArgument())); 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 members.Add(member); // Then add to the final output list
} }
if (members.Count == 0) throw new PKSyntaxError($"You must input at least one member."); if (members.Count == 0) throw new PKSyntaxError($"You must input at least one member.");
@ -127,7 +125,7 @@ namespace PluralKit.Bot
while (ctx.HasNext()) while (ctx.HasNext())
{ {
// and attempt to match a group // and attempt to match a group
var group = await ctx.MatchGroup(); var group = await ctx.MatchGroup(restrictToSystem);
if (group == null) if (group == null)
// if we can't, big error. Every group name must be valid. // if we can't, big error. Every group name must be valid.
throw new PKError(ctx.CreateGroupNotFoundError(ctx.PopArgument())); throw new PKError(ctx.CreateGroupNotFoundError(ctx.PopArgument()));

View File

@ -60,7 +60,7 @@ namespace PluralKit.Bot
return system; return system;
} }
public static async Task<PKMember> PeekMember(this Context ctx) public static async Task<PKMember> PeekMember(this Context ctx, SystemId? restrictToSystem = null)
{ {
var input = ctx.PeekArgument(); var input = ctx.PeekArgument();
@ -76,7 +76,7 @@ namespace PluralKit.Bot
return memberByName; return memberByName;
// Then, try member HID parsing: // Then, try member HID parsing:
if (await ctx.Repository.GetMemberByHid(conn, input) is PKMember memberByHid) if (await ctx.Repository.GetMemberByHid(conn, input, restrictToSystem) is PKMember memberByHid)
return memberByHid; return memberByHid;
// And if that again fails, we try finding a member with a display name matching the argument from the system // And if that again fails, we try finding a member with a display name matching the argument from the system
@ -91,10 +91,10 @@ namespace PluralKit.Bot
/// Attempts to pop a member descriptor from the stack, returning it if present. If a member could not be /// Attempts to pop a member descriptor from the stack, returning it if present. If a member could not be
/// resolved by the next word in the argument stack, does *not* touch the stack, and returns null. /// resolved by the next word in the argument stack, does *not* touch the stack, and returns null.
/// </summary> /// </summary>
public static async Task<PKMember> MatchMember(this Context ctx) public static async Task<PKMember> MatchMember(this Context ctx, SystemId? restrictToSystem = null)
{ {
// First, peek a member // First, peek a member
var member = await ctx.PeekMember(); var member = await ctx.PeekMember(restrictToSystem);
// If the peek was successful, we've used up the next argument, so we pop that just to get rid of it. // If the peek was successful, we've used up the next argument, so we pop that just to get rid of it.
if (member != null) ctx.PopArgument(); if (member != null) ctx.PopArgument();
@ -103,14 +103,14 @@ namespace PluralKit.Bot
return member; return member;
} }
public static async Task<PKGroup> PeekGroup(this Context ctx) public static async Task<PKGroup> PeekGroup(this Context ctx, SystemId? restrictToSystem = null)
{ {
var input = ctx.PeekArgument(); var input = ctx.PeekArgument();
await using var conn = await ctx.Database.Obtain(); await using var conn = await ctx.Database.Obtain();
if (ctx.System != null && await ctx.Repository.GetGroupByName(conn, ctx.System.Id, input) is { } byName) if (ctx.System != null && await ctx.Repository.GetGroupByName(conn, ctx.System.Id, input) is { } byName)
return byName; return byName;
if (await ctx.Repository.GetGroupByHid(conn, input) is { } byHid) if (await ctx.Repository.GetGroupByHid(conn, input, restrictToSystem) is { } byHid)
return byHid; return byHid;
if (await ctx.Repository.GetGroupByDisplayName(conn, ctx.System.Id, input) is { } byDisplayName) if (await ctx.Repository.GetGroupByDisplayName(conn, ctx.System.Id, input) is { } byDisplayName)
return byDisplayName; return byDisplayName;
@ -118,9 +118,9 @@ namespace PluralKit.Bot
return null; return null;
} }
public static async Task<PKGroup> MatchGroup(this Context ctx) public static async Task<PKGroup> MatchGroup(this Context ctx, SystemId? restrictToSystem = null)
{ {
var group = await ctx.PeekGroup(); var group = await ctx.PeekGroup(restrictToSystem);
if (group != null) ctx.PopArgument(); if (group != null) ctx.PopArgument();
return group; return group;
} }

View File

@ -17,8 +17,11 @@ namespace PluralKit.Core
public Task<PKGroup?> GetGroupByDisplayName(IPKConnection conn, SystemId system, string display_name) => public Task<PKGroup?> GetGroupByDisplayName(IPKConnection conn, SystemId system, string display_name) =>
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new { System = system, Name = display_name }); conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new { System = system, Name = display_name });
public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid) => public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid, SystemId? system = null)
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new { hid = hid.ToLowerInvariant() }); => conn.QueryFirstOrDefaultAsync<PKGroup?>(
"select * from groups where hid = @hid" + (system != null ? " and system = @System" : ""),
new { hid = hid.ToLowerInvariant(), System = system }
);
public Task<int> GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null) public Task<int> GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null)
{ {

View File

@ -1,4 +1,5 @@
#nullable enable #nullable enable
using System;
using System.Data; using System.Data;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -11,8 +12,11 @@ namespace PluralKit.Core
public Task<PKMember?> GetMember(IPKConnection conn, MemberId id) => public Task<PKMember?> GetMember(IPKConnection conn, MemberId id) =>
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new { id }); conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new { id });
public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid) => public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid, SystemId? system = null)
conn.QuerySingleOrDefaultAsync<PKMember?>("select * from members where hid = @Hid", new { Hid = hid.ToLower() }); => conn.QuerySingleOrDefaultAsync<PKMember?>(
"select * from members where hid = @Hid" + (system != null ? " and system = @System" : ""),
new { Hid = hid.ToLower(), System = system }
);
public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) => public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) =>
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system }); conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });