feat: don't try to match other systems' members in switch / group add commands
This commit is contained in:
parent
dec228d5bd
commit
a2bf70b395
@ -104,14 +104,12 @@ namespace PluralKit.Bot
|
||||
while (ctx.HasNext())
|
||||
{
|
||||
// and attempt to match a member
|
||||
var member = await ctx.MatchMember();
|
||||
var member = await ctx.MatchMember(restrictToSystem);
|
||||
|
||||
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
|
||||
}
|
||||
if (members.Count == 0) throw new PKSyntaxError($"You must input at least one member.");
|
||||
@ -127,7 +125,7 @@ namespace PluralKit.Bot
|
||||
while (ctx.HasNext())
|
||||
{
|
||||
// and attempt to match a group
|
||||
var group = await ctx.MatchGroup();
|
||||
var group = await ctx.MatchGroup(restrictToSystem);
|
||||
if (group == null)
|
||||
// if we can't, big error. Every group name must be valid.
|
||||
throw new PKError(ctx.CreateGroupNotFoundError(ctx.PopArgument()));
|
||||
|
@ -60,7 +60,7 @@ namespace PluralKit.Bot
|
||||
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();
|
||||
|
||||
@ -76,7 +76,7 @@ namespace PluralKit.Bot
|
||||
return memberByName;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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
|
||||
/// resolved by the next word in the argument stack, does *not* touch the stack, and returns null.
|
||||
/// </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
|
||||
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 (member != null) ctx.PopArgument();
|
||||
@ -103,14 +103,14 @@ namespace PluralKit.Bot
|
||||
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();
|
||||
|
||||
await using var conn = await ctx.Database.Obtain();
|
||||
if (ctx.System != null && await ctx.Repository.GetGroupByName(conn, ctx.System.Id, input) is { } byName)
|
||||
return byName;
|
||||
if (await ctx.Repository.GetGroupByHid(conn, input) is { } byHid)
|
||||
if (await ctx.Repository.GetGroupByHid(conn, input, restrictToSystem) is { } byHid)
|
||||
return byHid;
|
||||
if (await ctx.Repository.GetGroupByDisplayName(conn, ctx.System.Id, input) is { } byDisplayName)
|
||||
return byDisplayName;
|
||||
@ -118,9 +118,9 @@ namespace PluralKit.Bot
|
||||
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();
|
||||
return group;
|
||||
}
|
||||
|
@ -17,8 +17,11 @@ namespace PluralKit.Core
|
||||
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 });
|
||||
|
||||
public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new { hid = hid.ToLowerInvariant() });
|
||||
public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid, SystemId? system = null)
|
||||
=> 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)
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -11,8 +12,11 @@ namespace PluralKit.Core
|
||||
public Task<PKMember?> GetMember(IPKConnection conn, MemberId id) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new { id });
|
||||
|
||||
public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid) =>
|
||||
conn.QuerySingleOrDefaultAsync<PKMember?>("select * from members where hid = @Hid", new { Hid = hid.ToLower() });
|
||||
public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid, SystemId? system = null)
|
||||
=> 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) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
|
||||
|
Loading…
Reference in New Issue
Block a user