implement proxied message and permcheck commands

This commit is contained in:
dusk 2025-10-03 02:21:12 +00:00
parent 2b304457cc
commit e4f38c76a9
No known key found for this signature in database
19 changed files with 233 additions and 155 deletions

View file

@ -106,25 +106,24 @@ public static class ContextArgumentsExt
else return null;
}
public static (ulong? messageId, ulong? channelId) MatchMessage(this Context ctx, bool parseRawMessageId)
public static (ulong? messageId, ulong? channelId) GetRepliedTo(this Context ctx)
{
if (ctx.Message.Type == Message.MessageType.Reply && ctx.Message.MessageReference?.MessageId != null)
return (ctx.Message.MessageReference.MessageId, ctx.Message.MessageReference.ChannelId);
return (null, null);
}
var word = ctx.PeekArgument();
if (word == null)
return (null, null);
if (parseRawMessageId && ulong.TryParse(word, out var mid))
public static (ulong? messageId, ulong? channelId) ParseMessage(this Context ctx, string maybeMessageRef, bool parseRawMessageId)
{
if (parseRawMessageId && ulong.TryParse(maybeMessageRef, out var mid))
return (mid, null);
var match = Regex.Match(word, "https://(?:\\w+.)?discord(?:app)?.com/channels/\\d+/(\\d+)/(\\d+)");
var match = Regex.Match(maybeMessageRef, "https://(?:\\w+.)?discord(?:app)?.com/channels/\\d+/(\\d+)/(\\d+)");
if (!match.Success)
return (null, null);
var channelId = ulong.Parse(match.Groups[1].Value);
var messageId = ulong.Parse(match.Groups[2].Value);
ctx.PopArgument();
return (messageId, channelId);
}
}

View file

@ -213,24 +213,4 @@ public static class ContextEntityArgumentsExt
ctx.PopArgument();
return channel;
}
public static async Task<Guild> ParseGuild(this Context ctx, string input)
{
if (!ulong.TryParse(input, out var id))
return null;
return await ctx.Rest.GetGuildOrNull(id);
}
public static async Task<Guild> MatchGuild(this Context ctx)
{
if (!ulong.TryParse(ctx.PeekArgument(), out var id))
return null;
var guild = await ctx.Rest.GetGuildOrNull(id);
if (guild != null)
ctx.PopArgument();
return guild;
}
}

View file

@ -100,6 +100,22 @@ public static class ContextParametersExt
);
}
public static async Task<Myriad.Types.Message.Reference?> ParamResolveMessage(this Context ctx, string param_name)
{
return await ctx.Parameters.ResolveParameter(
ctx, param_name,
param => (param as Parameter.MessageRef)?.message
);
}
public static async Task<Myriad.Types.Channel?> ParamResolveChannel(this Context ctx, string param_name)
{
return await ctx.Parameters.ResolveParameter(
ctx, param_name,
param => (param as Parameter.ChannelRef)?.channel
);
}
public static async Task<Myriad.Types.Guild?> ParamResolveGuild(this Context ctx, string param_name)
{
return await ctx.Parameters.ResolveParameter(

View file

@ -13,6 +13,8 @@ public abstract record Parameter()
public record GroupRef(PKGroup group): Parameter;
public record GroupRefs(List<PKGroup> groups): Parameter;
public record SystemRef(PKSystem system): Parameter;
public record MessageRef(Message.Reference message): Parameter;
public record ChannelRef(Channel channel): Parameter;
public record GuildRef(Guild guild): Parameter;
public record MemberPrivacyTarget(MemberPrivacySubject target): Parameter;
public record GroupPrivacyTarget(GroupPrivacySubject target): Parameter;
@ -118,8 +120,12 @@ public class Parameters
return new Parameter.Opaque(opaque.raw);
case uniffi.commands.Parameter.Avatar avatar:
return new Parameter.Avatar(await ctx.GetUserPfp(avatar.avatar) ?? ctx.ParseImage(avatar.avatar));
case uniffi.commands.Parameter.GuildRef guildRef:
return new Parameter.GuildRef(await ctx.ParseGuild(guildRef.guild) ?? throw new PKError($"Guild {guildRef.guild} not found"));
case uniffi.commands.Parameter.MessageRef(var guildId, var channelId, var messageId):
return new Parameter.MessageRef(new Message.Reference(guildId, channelId, messageId));
case uniffi.commands.Parameter.ChannelRef(var channelId):
return new Parameter.ChannelRef(await ctx.Rest.GetChannelOrNull(channelId) ?? throw new PKError($"Channel {channelId} not found"));
case uniffi.commands.Parameter.GuildRef(var guildId):
return new Parameter.GuildRef(await ctx.Rest.GetGuildOrNull(guildId) ?? throw new PKError($"Guild {guildId} not found"));
}
return null;
}