Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commands): Fix handling of pulling context #335

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public static IServiceCollection AddDiscordCommands
.AddParser<EmojiParser>()
.AddParser<GuildMemberParser>()
.AddParser<MessageParser>()
.AddParser<PartialMessageParser>()
.AddParser<RoleParser>()
.AddParser<UserParser>();

Expand Down
65 changes: 2 additions & 63 deletions Remora.Discord.Commands/Parsers/MessageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
namespace Remora.Discord.Commands.Parsers;

/// <summary>
/// Parses instances of <see cref="IMessage"/> and <see cref="IPartialMessage"/> from command-line inputs.
/// Parses instances of <see cref="IMessage"/> from command-line inputs.
/// </summary>
[PublicAPI]
public class MessageParser : AbstractTypeParser<IMessage>, ITypeParser<IPartialMessage>
public class MessageParser : AbstractTypeParser<IMessage>
{
private static readonly Regex _messageLinkRegex = new
(
Expand Down Expand Up @@ -100,65 +100,4 @@ public override async ValueTask<Result<IMessage>> TryParseAsync

return await _channelAPI.GetChannelMessageAsync(channelId.Value, messageId.Value, ct);
}

/// <inheritdoc/>
async ValueTask<Result<IPartialMessage>> ITypeParser<IPartialMessage>.TryParseAsync
(
IReadOnlyList<string> tokens,
CancellationToken ct
)
{
return (await (this as ITypeParser<IMessage>).TryParseAsync(tokens, ct)).Map(a => a as IPartialMessage);
}

/// <inheritdoc/>
async ValueTask<Result<IPartialMessage>> ITypeParser<IPartialMessage>.TryParseAsync
(
string token,
CancellationToken ct
)
{
_ = DiscordSnowflake.TryParse(token.Unmention(), out var messageID);
if (messageID is null)
{
var messageLinkMatch = _messageLinkRegex.Match(token);
if (!messageLinkMatch.Success)
{
return new ParsingError<IPartialMessage>(token, "Unrecognized input format.");
}

var messageIdRaw = messageLinkMatch.Groups["message_id"].Value;
if (!DiscordSnowflake.TryParse(messageIdRaw, out messageID))
{
return new ParsingError<IPartialMessage>(token, "Unrecognized input format.");
}
}

var resolvedMessage = GetResolvedMessageOrDefault(messageID.Value);
return resolvedMessage is null
? (await (this as ITypeParser<IMessage>).TryParseAsync(token, ct)).Map(a => a as IPartialMessage)
: Result<IPartialMessage>.FromSuccess(resolvedMessage);
}

private IPartialMessage? GetResolvedMessageOrDefault(Snowflake messageID)
{
if (_context is not IInteractionContext interactionContext)
{
return null;
}

if (!interactionContext.Interaction.Data.TryGet(out var data))
{
return null;
}

var resolvedData = data.Match(a => a.Resolved, _ => default, _ => default);
if (!resolvedData.TryGet(out var resolved) || !resolved.Messages.TryGet(out var messages))
{
return null;
}

_ = messages.TryGetValue(messageID, out var message);
return message;
}
}
92 changes: 92 additions & 0 deletions Remora.Discord.Commands/Parsers/PartialMessageParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// PartialMessageParser.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Remora.Commands.Parsers;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Commands.Contexts;
using Remora.Discord.Commands.Extensions;
using Remora.Results;

namespace Remora.Discord.Commands.Parsers;

/// <summary>
/// Parses instances of <see cref="IPartialMessage"/> from command-line inputs.
/// </summary>
[PublicAPI]
public class PartialMessageParser : AbstractTypeParser<IPartialMessage>
{
private readonly IInteractionContext? _context;
private readonly ITypeParser<IMessage> _messageParser;

/// <summary>
/// Initializes a new instance of the <see cref="PartialMessageParser"/> class.
/// </summary>
/// <param name="context">The interaction context, if available. </param>
/// <param name="messageParser">The message parser to delegate to, if necessary.</param>
public PartialMessageParser(IInteractionContext? context, ITypeParser<IMessage> messageParser)
{
_context = context;
_messageParser = messageParser;
}

/// <inheritdoc />
public override async ValueTask<Result<IPartialMessage>> TryParseAsync
(
string value,
CancellationToken ct = default
)
{
var resolvedValue = ResolveMessageFromContext(value);

return resolvedValue is not null ? Result<IPartialMessage>.FromSuccess(resolvedValue) : (await _messageParser.TryParseAsync(value, ct)).Map<IPartialMessage>(m => m);
}

private IPartialMessage? ResolveMessageFromContext(string value)
{
if (_context is null)
{
return null;
}

if (!DiscordSnowflake.TryParse(value.Unmention(), out var messageID))
{
return null;
}

if (!_context.Interaction.Data.TryGet(out var interactionData) || interactionData.TryPickT0(out var strongData, out _))
{
return null;
}

if (!strongData.Resolved.TryGet(out var resolved) || !resolved.Messages.TryGet(out var messages))
{
return null;
}

_ = messages.TryGetValue(messageID.Value, out var message);
return message;
}
}
Loading