feat: change member/group lookup order, add -id flag
This commit is contained in:
parent
19628ec400
commit
8df259a40d
@ -108,7 +108,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
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.CreateNotFoundError("Member", ctx.PopArgument()));
|
||||||
|
|
||||||
members.Add(member); // Then add to the final output list
|
members.Add(member); // Then add to the final output list
|
||||||
}
|
}
|
||||||
@ -128,7 +128,7 @@ namespace PluralKit.Bot
|
|||||||
var group = await ctx.MatchGroup(restrictToSystem);
|
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.CreateNotFoundError("Group", ctx.PopArgument()));
|
||||||
|
|
||||||
// todo: remove this, the database query enforces the restriction
|
// todo: remove this, the database query enforces the restriction
|
||||||
if (restrictToSystem != null && group.System != restrictToSystem)
|
if (restrictToSystem != null && group.System != restrictToSystem)
|
||||||
|
@ -68,18 +68,23 @@ namespace PluralKit.Bot
|
|||||||
// - A textual name of a member *in your own system*
|
// - A textual name of a member *in your own system*
|
||||||
// - a textual display name of a member *in your own system*
|
// - a textual display name of a member *in your own system*
|
||||||
|
|
||||||
// First, if we have a system, try finding by member name in system
|
// Skip name / display name matching if the user does not have a system
|
||||||
if (ctx.System != null && await ctx.Repository.GetMemberByName(ctx.System.Id, input) is PKMember memberByName)
|
// or if they specifically request by-HID matching
|
||||||
return memberByName;
|
if (ctx.System != null && !ctx.MatchFlag("id", "by-id"))
|
||||||
|
{
|
||||||
|
// First, try finding by member name in system
|
||||||
|
if (await ctx.Repository.GetMemberByName(ctx.System.Id, input) is PKMember memberByName)
|
||||||
|
return memberByName;
|
||||||
|
|
||||||
// Then, try member HID parsing:
|
// And if that fails, we try finding a member with a display name matching the argument from the system
|
||||||
|
if (ctx.System != null && await ctx.Repository.GetMemberByDisplayName(ctx.System.Id, input) is PKMember memberByDisplayName)
|
||||||
|
return memberByDisplayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally (or if by-HID lookup is specified), try member HID parsing:
|
||||||
if (await ctx.Repository.GetMemberByHid(input, restrictToSystem) is PKMember memberByHid)
|
if (await ctx.Repository.GetMemberByHid(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
|
|
||||||
if (ctx.System != null && await ctx.Repository.GetMemberByDisplayName(ctx.System.Id, input) is PKMember memberByDisplayName)
|
|
||||||
return memberByDisplayName;
|
|
||||||
|
|
||||||
// We didn't find anything, so we return null.
|
// We didn't find anything, so we return null.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -104,12 +109,18 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
var input = ctx.PeekArgument();
|
var input = ctx.PeekArgument();
|
||||||
|
|
||||||
if (ctx.System != null && await ctx.Repository.GetGroupByName(ctx.System.Id, input) is { } byName)
|
// see PeekMember for an explanation of the logic used here
|
||||||
return byName;
|
|
||||||
|
if (ctx.System != null && !ctx.MatchFlag("id", "by-id"))
|
||||||
|
{
|
||||||
|
if (await ctx.Repository.GetGroupByName(ctx.System.Id, input) is { } byName)
|
||||||
|
return byName;
|
||||||
|
if (await ctx.Repository.GetGroupByDisplayName(ctx.System.Id, input) is { } byDisplayName)
|
||||||
|
return byDisplayName;
|
||||||
|
}
|
||||||
|
|
||||||
if (await ctx.Repository.GetGroupByHid(input, restrictToSystem) is { } byHid)
|
if (await ctx.Repository.GetGroupByHid(input, restrictToSystem) is { } byHid)
|
||||||
return byHid;
|
return byHid;
|
||||||
if (await ctx.Repository.GetGroupByDisplayName(ctx.System.Id, input) is { } byDisplayName)
|
|
||||||
return byDisplayName;
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -121,34 +132,24 @@ namespace PluralKit.Bot
|
|||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string CreateMemberNotFoundError(this Context ctx, string input)
|
public static string CreateNotFoundError(this Context ctx, string entity, string input)
|
||||||
{
|
{
|
||||||
// TODO: does this belong here?
|
var isIDOnlyQuery = ctx.System == null || ctx.MatchFlag("id", "by-id");
|
||||||
if (input.Length == 5)
|
|
||||||
|
if (isIDOnlyQuery)
|
||||||
{
|
{
|
||||||
if (ctx.System != null)
|
if (input.Length == 5)
|
||||||
return $"Member with ID or name \"{input}\" not found.";
|
return $"{entity} with ID \"{input}\" not found.";
|
||||||
return $"Member with ID \"{input}\" not found."; // Accounts without systems can't query by name
|
else
|
||||||
|
return $"{entity} not found. Note that a {entity.ToLower()} ID is 5 characters long.";
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (ctx.System != null)
|
|
||||||
return $"Member with name \"{input}\" not found. Note that a member ID is 5 characters long.";
|
|
||||||
return $"Member not found. Note that a member ID is 5 characters long.";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string CreateGroupNotFoundError(this Context ctx, string input)
|
|
||||||
{
|
|
||||||
// TODO: does this belong here?
|
|
||||||
if (input.Length == 5)
|
|
||||||
{
|
{
|
||||||
if (ctx.System != null)
|
if (input.Length == 5)
|
||||||
return $"Group with ID or name \"{input}\" not found.";
|
return $"{entity} with ID or name \"{input}\" not found.";
|
||||||
return $"Group with ID \"{input}\" not found."; // Accounts without systems can't query by name
|
else
|
||||||
|
return $"{entity} with name \"{input}\" not found. Note that a {entity.ToLower()} ID is 5 characters long.";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.System != null)
|
|
||||||
return $"Group with name \"{input}\" not found. Note that a group ID is 5 characters long.";
|
|
||||||
return $"Group not found. Note that a group ID is 5 characters long.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<Channel> MatchChannel(this Context ctx)
|
public static Task<Channel> MatchChannel(this Context ctx)
|
||||||
|
@ -373,7 +373,7 @@ namespace PluralKit.Bot
|
|||||||
await PrintCommandExpectedError(ctx, MemberNew, MemberInfo, MemberRename, MemberDisplayName, MemberServerName, MemberDesc, MemberPronouns,
|
await PrintCommandExpectedError(ctx, MemberNew, MemberInfo, MemberRename, MemberDisplayName, MemberServerName, MemberDesc, MemberPronouns,
|
||||||
MemberColor, MemberBirthday, MemberProxy, MemberDelete, MemberAvatar);
|
MemberColor, MemberBirthday, MemberProxy, MemberDelete, MemberAvatar);
|
||||||
else
|
else
|
||||||
await ctx.Reply($"{Emojis.Error} {ctx.CreateMemberNotFoundError(ctx.PopArgument())}");
|
await ctx.Reply($"{Emojis.Error} {ctx.CreateNotFoundError("Member", ctx.PopArgument())}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleMemberCommandTargeted(Context ctx, PKMember target)
|
private async Task HandleMemberCommandTargeted(Context ctx, PKMember target)
|
||||||
@ -478,7 +478,7 @@ namespace PluralKit.Bot
|
|||||||
else if (!ctx.HasNext())
|
else if (!ctx.HasNext())
|
||||||
await PrintCommandExpectedError(ctx, GroupCommands);
|
await PrintCommandExpectedError(ctx, GroupCommands);
|
||||||
else
|
else
|
||||||
await ctx.Reply($"{Emojis.Error} {ctx.CreateGroupNotFoundError(ctx.PopArgument())}");
|
await ctx.Reply($"{Emojis.Error} {ctx.CreateNotFoundError("Group", ctx.PopArgument())}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleSwitchCommand(Context ctx)
|
private async Task HandleSwitchCommand(Context ctx)
|
||||||
|
Loading…
Reference in New Issue
Block a user