2021-05-03 10:33:30 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2020-07-07 17:51:19 +00:00
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
using Myriad.Types;
|
|
|
|
|
2020-07-07 17:51:19 +00:00
|
|
|
using PluralKit.Core;
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public static class ContextArgumentsExt
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string PopArgument(this Context ctx) =>
|
|
|
|
ctx.Parameters.Pop();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string PeekArgument(this Context ctx) =>
|
|
|
|
ctx.Parameters.Peek();
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string RemainderOrNull(this Context ctx, bool skipFlags = true) =>
|
|
|
|
ctx.Parameters.Remainder(skipFlags).Length == 0 ? null : ctx.Parameters.Remainder(skipFlags);
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool HasNext(this Context ctx, bool skipFlags = true) =>
|
|
|
|
ctx.RemainderOrNull(skipFlags) != null;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static string FullCommand(this Context ctx) =>
|
|
|
|
ctx.Parameters.FullCommand;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the next parameter is equal to one of the given keywords. Case-insensitive.
|
|
|
|
/// </summary>
|
|
|
|
public static bool Match(this Context ctx, ref string used, params string[] potentialMatches)
|
|
|
|
{
|
|
|
|
var arg = ctx.PeekArgument();
|
|
|
|
foreach (var match in potentialMatches)
|
|
|
|
if (arg.Equals(match, StringComparison.InvariantCultureIgnoreCase))
|
2020-07-01 16:27:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
used = ctx.PopArgument();
|
|
|
|
return true;
|
2020-07-01 16:27:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-01 16:27:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the next parameter is equal to one of the given keywords. Case-insensitive.
|
|
|
|
/// </summary>
|
|
|
|
public static bool Match(this Context ctx, params string[] potentialMatches)
|
|
|
|
{
|
|
|
|
string used = null; // Unused and unreturned, we just yeet it
|
|
|
|
return ctx.Match(ref used, potentialMatches);
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool MatchFlag(this Context ctx, params string[] potentialMatches)
|
|
|
|
{
|
|
|
|
// Flags are *ALWAYS PARSED LOWERCASE*. This means we skip out on a "ToLower" call here.
|
|
|
|
// Can assume the caller array only contains lowercase *and* the set below only contains lowercase
|
2020-07-07 17:51:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var flags = ctx.Parameters.Flags();
|
|
|
|
return potentialMatches.Any(potentialMatch => flags.Contains(potentialMatch));
|
|
|
|
}
|
2020-07-07 21:41:51 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<bool> MatchClear(this Context ctx, string toClear = null)
|
|
|
|
{
|
|
|
|
var matched = ctx.Match("clear", "reset") || ctx.MatchFlag("c", "clear");
|
|
|
|
if (matched && toClear != null)
|
|
|
|
return await ctx.ConfirmClear(toClear);
|
|
|
|
return matched;
|
|
|
|
}
|
2021-08-27 23:20:14 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static bool MatchRaw(this Context ctx) =>
|
|
|
|
ctx.Match("r", "raw") || ctx.MatchFlag("r", "raw");
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static (ulong? messageId, ulong? channelId) MatchMessage(this Context ctx, bool parseRawMessageId)
|
|
|
|
{
|
|
|
|
if (ctx.Message.Type == Message.MessageType.Reply && ctx.Message.MessageReference?.MessageId != null)
|
|
|
|
return (ctx.Message.MessageReference.MessageId, ctx.Message.MessageReference.ChannelId);
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var word = ctx.PeekArgument();
|
|
|
|
if (word == null)
|
|
|
|
return (null, null);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (parseRawMessageId && ulong.TryParse(word, out var mid))
|
|
|
|
return (mid, null);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var match = Regex.Match(word, "https://(?:\\w+.)?discord(?:app)?.com/channels/\\d+/(\\d+)/(\\d+)");
|
|
|
|
if (!match.Success)
|
|
|
|
return (null, null);
|
2021-05-03 10:33:30 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var channelId = ulong.Parse(match.Groups[1].Value);
|
|
|
|
var messageId = ulong.Parse(match.Groups[2].Value);
|
|
|
|
ctx.PopArgument();
|
|
|
|
return (messageId, channelId);
|
|
|
|
}
|
2020-07-07 17:51:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<List<PKMember>> ParseMemberList(this Context ctx, SystemId? restrictToSystem)
|
|
|
|
{
|
|
|
|
var members = new List<PKMember>();
|
2021-09-13 07:14:59 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Loop through all the given arguments
|
|
|
|
while (ctx.HasNext())
|
|
|
|
{
|
|
|
|
// and attempt to match a member
|
|
|
|
var member = await ctx.MatchMember(restrictToSystem);
|
2020-07-07 17:51:19 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (member == null)
|
|
|
|
// if we can't, big error. Every member name must be valid.
|
|
|
|
throw new PKError(ctx.CreateNotFoundError("Member", ctx.PopArgument()));
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
members.Add(member); // Then add to the final output list
|
2020-07-07 17:51:19 +00:00
|
|
|
}
|
2020-11-23 00:57:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (members.Count == 0) throw new PKSyntaxError("You must input at least one member.");
|
2020-11-23 00:57:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return members;
|
|
|
|
}
|
2020-11-23 00:57:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static async Task<List<PKGroup>> ParseGroupList(this Context ctx, SystemId? restrictToSystem)
|
|
|
|
{
|
|
|
|
var groups = new List<PKGroup>();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// Loop through all the given arguments
|
|
|
|
while (ctx.HasNext())
|
|
|
|
{
|
|
|
|
// and attempt to match a group
|
|
|
|
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.CreateNotFoundError("Group", ctx.PopArgument()));
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// todo: remove this, the database query enforces the restriction
|
|
|
|
if (restrictToSystem != null && group.System != restrictToSystem)
|
|
|
|
throw Errors.NotOwnGroupError; // TODO: name *which* group?
|
2020-11-23 00:57:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
groups.Add(group); // Then add to the final output list
|
2020-11-23 00:57:01 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
if (groups.Count == 0) throw new PKSyntaxError("You must input at least one group.");
|
|
|
|
|
|
|
|
return groups;
|
2020-07-01 16:27:26 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
}
|