mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-12 16:50:10 +00:00
remove rest of the parsing in csharp bot
This commit is contained in:
parent
15ffd16c01
commit
479e0a59b5
35 changed files with 242 additions and 409 deletions
|
|
@ -2,110 +2,10 @@ using System.Text.RegularExpressions;
|
|||
|
||||
using Myriad.Types;
|
||||
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Bot;
|
||||
|
||||
public static class ContextArgumentsExt
|
||||
{
|
||||
public static string PopArgument(this Context ctx) => throw new PKError("todo: PopArgument");
|
||||
|
||||
public static string PeekArgument(this Context ctx) => throw new PKError("todo: PeekArgument");
|
||||
|
||||
public static string RemainderOrNull(this Context ctx, bool skipFlags = true) => throw new PKError("todo: RemainderOrNull");
|
||||
|
||||
public static bool HasNext(this Context ctx, bool skipFlags = true) => throw new PKError("todo: HasNext");
|
||||
|
||||
public static string FullCommand(this Context ctx) => throw new PKError("todo: FullCommand");
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the next parameter is equal to one of the given keywords and pops it from the stack. 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))
|
||||
{
|
||||
used = ctx.PopArgument();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the next parameter (starting from `ptr`) is equal to one of the given keywords, and leaves it on the stack. Case-insensitive.
|
||||
/// </summary>
|
||||
public static bool PeekMatch(this Context ctx, ref int ptr, string[] potentialMatches)
|
||||
{
|
||||
throw new PKError("todo: PeekMatch");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matches the next *n* parameters against each parameter consecutively.
|
||||
/// <br />
|
||||
/// Note that this is handled differently than single-parameter Match:
|
||||
/// each method parameter is an array of potential matches for the *n*th command string parameter.
|
||||
/// </summary>
|
||||
public static bool MatchMultiple(this Context ctx, params string[][] potentialParametersMatches)
|
||||
{
|
||||
throw new PKError("todo: MatchMultiple");
|
||||
}
|
||||
|
||||
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
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static bool MatchClear(this Context ctx)
|
||||
=> ctx.Match("clear", "reset", "default") || ctx.MatchFlag("c", "clear");
|
||||
|
||||
public static ReplyFormat MatchFormat(this Context ctx)
|
||||
{
|
||||
if (ctx.Match("r", "raw") || ctx.MatchFlag("r", "raw")) return ReplyFormat.Raw;
|
||||
if (ctx.Match("pt", "plaintext") || ctx.MatchFlag("pt", "plaintext")) return ReplyFormat.Plaintext;
|
||||
return ReplyFormat.Standard;
|
||||
}
|
||||
|
||||
public static ReplyFormat PeekMatchFormat(this Context ctx)
|
||||
{
|
||||
throw new PKError("todo: PeekMatchFormat");
|
||||
}
|
||||
|
||||
public static bool MatchToggle(this Context ctx, bool? defaultValue = null)
|
||||
{
|
||||
var value = ctx.MatchToggleOrNull(defaultValue);
|
||||
if (value == null) throw new PKError("You must pass either \"on\" or \"off\" to this command.");
|
||||
return value.Value;
|
||||
}
|
||||
|
||||
public static bool? MatchToggleOrNull(this Context ctx, bool? defaultValue = null)
|
||||
{
|
||||
if (defaultValue != null && ctx.MatchClear())
|
||||
return defaultValue.Value;
|
||||
|
||||
var yesToggles = new[] { "yes", "on", "enable", "enabled", "true" };
|
||||
var noToggles = new[] { "no", "off", "disable", "disabled", "false" };
|
||||
|
||||
if (ctx.Match(yesToggles) || ctx.MatchFlag(yesToggles))
|
||||
return true;
|
||||
else if (ctx.Match(noToggles) || ctx.MatchFlag(noToggles))
|
||||
return false;
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static (ulong? messageId, ulong? channelId) GetRepliedTo(this Context ctx)
|
||||
{
|
||||
if (ctx.Message.Type == Message.MessageType.Reply && ctx.Message.MessageReference?.MessageId != null)
|
||||
|
|
|
|||
|
|
@ -48,10 +48,6 @@ public static class ContextAvatarExt
|
|||
// ToString URL-decodes, which breaks URLs to spaces; AbsoluteUri doesn't
|
||||
return new ParsedImage { Url = uri.AbsoluteUri, Source = AvatarSource.Url };
|
||||
}
|
||||
public static async Task<ParsedImage?> MatchImage(this Context ctx)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParsedImage
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
using System.Text.RegularExpressions;
|
||||
|
||||
using Myriad.Extensions;
|
||||
using Myriad.Types;
|
||||
|
||||
using PluralKit.Bot.Utils;
|
||||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Bot;
|
||||
|
|
@ -120,23 +117,4 @@ public static class ContextEntityArgumentsExt
|
|||
return $"{entity} with ID or name \"{input}\" not found.";
|
||||
return $"{entity} with name \"{input}\" not found. Note that a {entity.ToLower()} ID is 5 or 6 characters long.";
|
||||
}
|
||||
|
||||
public static async Task<Channel> MatchChannel(this Context ctx)
|
||||
{
|
||||
if (!MentionUtils.TryParseChannel(ctx.PeekArgument(), out var id))
|
||||
return null;
|
||||
|
||||
// todo: match channels in other guilds
|
||||
var channel = await ctx.Cache.TryGetChannel(ctx.Guild!.Id, id);
|
||||
if (channel == null)
|
||||
channel = await ctx.Rest.GetChannelOrNull(id);
|
||||
if (channel == null)
|
||||
return null;
|
||||
|
||||
if (!DiscordUtils.IsValidGuildChannel(channel))
|
||||
return null;
|
||||
|
||||
ctx.PopArgument();
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
using PluralKit.Core;
|
||||
|
||||
namespace PluralKit.Bot;
|
||||
|
||||
public static class ContextPrivacyExt
|
||||
{
|
||||
public static PrivacyLevel PopPrivacyLevel(this Context ctx)
|
||||
{
|
||||
if (ctx.Match("public", "pub", "show", "shown", "visible", "unhide", "unhidden"))
|
||||
return PrivacyLevel.Public;
|
||||
|
||||
if (ctx.Match("private", "priv", "hide", "hidden"))
|
||||
return PrivacyLevel.Private;
|
||||
|
||||
if (!ctx.HasNext())
|
||||
throw new PKSyntaxError("You must pass a privacy level (`public` or `private`)");
|
||||
|
||||
throw new PKSyntaxError(
|
||||
$"Invalid privacy level {ctx.PopArgument().AsCode()} (must be `public` or `private`).");
|
||||
}
|
||||
|
||||
public static SystemPrivacySubject PopSystemPrivacySubject(this Context ctx)
|
||||
{
|
||||
if (!SystemPrivacyUtils.TryParseSystemPrivacy(ctx.PeekArgument(), out var subject))
|
||||
throw new PKSyntaxError(
|
||||
$"Invalid privacy subject {ctx.PopArgument().AsCode()} (must be `description`, `members`, `front`, `fronthistory`, `groups`, or `all`).");
|
||||
|
||||
ctx.PopArgument();
|
||||
return subject;
|
||||
}
|
||||
|
||||
public static MemberPrivacySubject PopMemberPrivacySubject(this Context ctx)
|
||||
{
|
||||
if (!MemberPrivacyUtils.TryParseMemberPrivacy(ctx.PeekArgument(), out var subject))
|
||||
throw new PKSyntaxError(
|
||||
$"Invalid privacy subject {ctx.PopArgument().AsCode()} (must be `name`, `description`, `avatar`, `birthday`, `pronouns`, `proxy`, `metadata`, `visibility`, or `all`).");
|
||||
|
||||
ctx.PopArgument();
|
||||
return subject;
|
||||
}
|
||||
|
||||
public static GroupPrivacySubject PopGroupPrivacySubject(this Context ctx)
|
||||
{
|
||||
if (!GroupPrivacyUtils.TryParseGroupPrivacy(ctx.PeekArgument(), out var subject))
|
||||
throw new PKSyntaxError(
|
||||
$"Invalid privacy subject {ctx.PopArgument().AsCode()} (must be `name`, `description`, `icon`, `metadata`, `visibility`, or `all`).");
|
||||
|
||||
ctx.PopArgument();
|
||||
return subject;
|
||||
}
|
||||
}
|
||||
|
|
@ -72,7 +72,7 @@ public class Parameters
|
|||
|
||||
private async Task<Parameter?> ResolveFfiParam(Context ctx, uniffi.commands.Parameter ffi_param)
|
||||
{
|
||||
var byId = HasFlag("id", "by-id");
|
||||
var byId = HasFlag("id", "by-id"); // this is added as a hidden flag to all command definitions
|
||||
switch (ffi_param)
|
||||
{
|
||||
case uniffi.commands.Parameter.MemberRef memberRef:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue