2021-04-06 04:25:13 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2020-12-25 12:19:35 +00:00
|
|
|
using Myriad.Extensions;
|
2020-12-22 15:55:13 +00:00
|
|
|
using Myriad.Types;
|
|
|
|
|
2020-07-01 16:27:26 +00:00
|
|
|
using PluralKit.Bot.Utils;
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public static class ContextEntityArgumentsExt
|
|
|
|
{
|
2020-12-24 13:52:44 +00:00
|
|
|
public static async Task<User> MatchUser(this Context ctx)
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
|
|
|
var text = ctx.PeekArgument();
|
|
|
|
if (text.TryParseMention(out var id))
|
2021-01-31 15:16:52 +00:00
|
|
|
return await ctx.Cache.GetOrFetchUser(ctx.Rest, id);
|
2020-12-24 13:52:44 +00:00
|
|
|
|
2020-07-01 16:27:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool MatchUserRaw(this Context ctx, out ulong id)
|
|
|
|
{
|
|
|
|
id = 0;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-07-01 16:27:26 +00:00
|
|
|
var text = ctx.PeekArgument();
|
|
|
|
if (text.TryParseMention(out var mentionId))
|
|
|
|
id = mentionId;
|
|
|
|
|
|
|
|
return id != 0;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-07-01 16:27:26 +00:00
|
|
|
public static Task<PKSystem> PeekSystem(this Context ctx) => ctx.MatchSystemInner();
|
|
|
|
|
|
|
|
public static async Task<PKSystem> MatchSystem(this Context ctx)
|
|
|
|
{
|
|
|
|
var system = await ctx.MatchSystemInner();
|
|
|
|
if (system != null) ctx.PopArgument();
|
|
|
|
return system;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task<PKSystem> MatchSystemInner(this Context ctx)
|
|
|
|
{
|
|
|
|
var input = ctx.PeekArgument();
|
|
|
|
|
|
|
|
// System references can take three forms:
|
|
|
|
// - The direct user ID of an account connected to the system
|
|
|
|
// - A @mention of an account connected to the system (<@uid>)
|
|
|
|
// - A system hid
|
|
|
|
|
|
|
|
// Direct IDs and mentions are both handled by the below method:
|
|
|
|
if (input.TryParseMention(out var id))
|
2021-09-30 01:51:38 +00:00
|
|
|
return await ctx.Repository.GetSystemByAccount(id);
|
2020-07-01 16:27:26 +00:00
|
|
|
|
|
|
|
// Finally, try HID parsing
|
2021-09-30 01:51:38 +00:00
|
|
|
var system = await ctx.Repository.GetSystemByHid(input);
|
2020-07-01 16:27:26 +00:00
|
|
|
return system;
|
|
|
|
}
|
|
|
|
|
2021-09-13 07:14:59 +00:00
|
|
|
public static async Task<PKMember> PeekMember(this Context ctx, SystemId? restrictToSystem = null)
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
|
|
|
var input = ctx.PeekArgument();
|
|
|
|
|
|
|
|
// Member references can have one of three forms, depending on
|
|
|
|
// whether you're in a system or not:
|
|
|
|
// - A member hid
|
|
|
|
// - A textual name of a member *in your own system*
|
|
|
|
// - a textual display name of a member *in your own system*
|
|
|
|
|
2021-11-17 14:30:19 +00:00
|
|
|
// Skip name / display name matching if the user does not have a system
|
|
|
|
// or if they specifically request by-HID matching
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2021-11-17 14:30:19 +00:00
|
|
|
// Finally (or if by-HID lookup is specified), try member HID parsing:
|
2021-09-30 01:51:38 +00:00
|
|
|
if (await ctx.Repository.GetMemberByHid(input, restrictToSystem) is PKMember memberByHid)
|
2020-07-01 16:27:26 +00:00
|
|
|
return memberByHid;
|
|
|
|
|
|
|
|
// We didn't find anything, so we return null.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 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>
|
2021-09-13 07:14:59 +00:00
|
|
|
public static async Task<PKMember> MatchMember(this Context ctx, SystemId? restrictToSystem = null)
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
|
|
|
// First, peek a member
|
2021-09-13 07:14:59 +00:00
|
|
|
var member = await ctx.PeekMember(restrictToSystem);
|
2020-07-01 16:27:26 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// Finally, we return the member value.
|
|
|
|
return member;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-09-13 07:14:59 +00:00
|
|
|
public static async Task<PKGroup> PeekGroup(this Context ctx, SystemId? restrictToSystem = null)
|
2020-06-29 21:51:12 +00:00
|
|
|
{
|
|
|
|
var input = ctx.PeekArgument();
|
|
|
|
|
2021-11-17 14:30:19 +00:00
|
|
|
// see PeekMember for an explanation of the logic used here
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
if (await ctx.Repository.GetGroupByHid(input, restrictToSystem) is { } byHid)
|
2020-06-29 21:51:12 +00:00
|
|
|
return byHid;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-13 07:14:59 +00:00
|
|
|
public static async Task<PKGroup> MatchGroup(this Context ctx, SystemId? restrictToSystem = null)
|
2020-06-29 21:51:12 +00:00
|
|
|
{
|
2021-09-13 07:14:59 +00:00
|
|
|
var group = await ctx.PeekGroup(restrictToSystem);
|
2020-06-29 21:51:12 +00:00
|
|
|
if (group != null) ctx.PopArgument();
|
|
|
|
return group;
|
|
|
|
}
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2021-11-17 14:30:19 +00:00
|
|
|
public static string CreateNotFoundError(this Context ctx, string entity, string input)
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
2021-11-17 14:30:19 +00:00
|
|
|
var isIDOnlyQuery = ctx.System == null || ctx.MatchFlag("id", "by-id");
|
|
|
|
|
|
|
|
if (isIDOnlyQuery)
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
2021-11-17 14:30:19 +00:00
|
|
|
if (input.Length == 5)
|
|
|
|
return $"{entity} with ID \"{input}\" not found.";
|
|
|
|
else
|
|
|
|
return $"{entity} not found. Note that a {entity.ToLower()} ID is 5 characters long.";
|
2020-07-01 16:27:26 +00:00
|
|
|
}
|
2021-11-17 14:30:19 +00:00
|
|
|
else
|
2020-07-06 17:50:39 +00:00
|
|
|
{
|
2021-11-17 14:30:19 +00:00
|
|
|
if (input.Length == 5)
|
|
|
|
return $"{entity} with ID or name \"{input}\" not found.";
|
|
|
|
else
|
|
|
|
return $"{entity} with name \"{input}\" not found. Note that a {entity.ToLower()} ID is 5 characters long.";
|
2020-07-06 17:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-18 01:41:02 +00:00
|
|
|
public static async Task<Channel> MatchChannel(this Context ctx)
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
2021-08-27 15:03:47 +00:00
|
|
|
if (!MentionUtils.TryParseChannel(ctx.PeekArgument(), out var id))
|
2021-11-18 01:41:02 +00:00
|
|
|
return null;
|
2020-12-22 15:55:13 +00:00
|
|
|
|
2021-11-18 18:11:02 +00:00
|
|
|
if (!(await ctx.Cache.TryGetChannel(id) is Channel channel))
|
2021-11-18 01:41:02 +00:00
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-07-08 13:17:35 +00:00
|
|
|
if (!DiscordUtils.IsValidGuildChannel(channel))
|
2021-11-18 01:41:02 +00:00
|
|
|
return null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-07-02 16:29:04 +00:00
|
|
|
ctx.PopArgument();
|
2021-11-18 01:41:02 +00:00
|
|
|
return channel;
|
2020-07-01 16:27:26 +00:00
|
|
|
}
|
2021-04-06 04:25:13 +00:00
|
|
|
|
2021-11-18 18:11:02 +00:00
|
|
|
public static async Task<Guild> MatchGuild(this Context ctx)
|
2021-04-06 04:25:13 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var id = ulong.Parse(ctx.PeekArgument());
|
2021-11-18 18:11:02 +00:00
|
|
|
var guild = await ctx.Cache.TryGetGuild(id);
|
2021-04-06 04:25:13 +00:00
|
|
|
if (guild != null)
|
|
|
|
ctx.PopArgument();
|
|
|
|
|
|
|
|
return guild;
|
|
|
|
}
|
|
|
|
catch (FormatException)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-07-01 16:27:26 +00:00
|
|
|
}
|
|
|
|
}
|