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

Re-type IMessageComponentData.cs#Values as a union array of snowflakes or strings #277

Merged
merged 4 commits into from
Jan 2, 2025
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 @@ -22,6 +22,7 @@

using System.Collections.Generic;
using JetBrains.Annotations;
using OneOf;
using Remora.Rest.Core;

namespace Remora.Discord.API.Abstractions.Objects;
Expand Down Expand Up @@ -52,10 +53,5 @@ public interface IMessageComponentData
/// <summary>
/// Gets the values selected by the user.
/// </summary>
/// <remarks>
/// The Discord API docs denote that this field should contain a list of
/// <see cref="ISelectOption"/> values. However, in their samples and as
/// identified through testing, it actually always contains a list of strings.
/// </remarks>
Optional<IReadOnlyList<string>> Values { get; }
Optional<OneOf<IReadOnlyList<Snowflake>, IReadOnlyList<string>>> Values { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using System.Collections.Generic;
using JetBrains.Annotations;
using OneOf;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Rest.Core;

Expand All @@ -34,5 +35,5 @@ public record MessageComponentData
string CustomID,
ComponentType ComponentType,
Optional<IApplicationCommandInteractionDataResolved> Resolved,
Optional<IReadOnlyList<string>> Values
Optional<OneOf<IReadOnlyList<Snowflake>, IReadOnlyList<string>>> Values
) : IMessageComponentData;
32 changes: 21 additions & 11 deletions Remora.Discord.Interactivity/Responders/InteractivityResponder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
using Remora.Commands.Services;
using Remora.Commands.Tokenization;
using Remora.Commands.Trees;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Gateway.Events;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
Expand Down Expand Up @@ -155,13 +154,7 @@ or ComponentType.MentionableSelect
var buildParameters = data.ComponentType switch
{
ComponentType.Button => new Dictionary<string, IReadOnlyList<string>>(),
ComponentType.StringSelect => Result<Dictionary<string, IReadOnlyList<string>>>.FromSuccess
(
new Dictionary<string, IReadOnlyList<string>>
{
{ "values", data.Values.Value }
}
),
ComponentType.StringSelect => BuildParametersFromStringData(data),
ComponentType.UserSelect
or ComponentType.RoleSelect
or ComponentType.MentionableSelect
Expand All @@ -185,6 +178,23 @@ or ComponentType.ChannelSelect
return await TryExecuteCommandAsync(context, commandPath, parameters, ct);
}

private static Result<Dictionary<string, IReadOnlyList<string>>> BuildParametersFromStringData
(
IMessageComponentData data
)
{
// In the case that the developer defined options are similar to Snowflakes, they'll
// be incorrectly parsed. Hence, we have to handle either case for string selects.
var values = data.Values.Value.TryPickT1(out var stringValues, out var snowflakeValues)
? stringValues
: snowflakeValues.Select(x => x.ToString()).ToList();

return new Dictionary<string, IReadOnlyList<string>>
{
{ "values", values }
};
}

private static Result<Dictionary<string, IReadOnlyList<string>>> BuildParametersFromResolvedData
(
IMessageComponentData data
Expand All @@ -193,11 +203,11 @@ IMessageComponentData data
var parameters = new Dictionary<string, IReadOnlyList<string>>();

var values = new HashSet<Snowflake>();
foreach (var value in data.Values.Value)
if (data.Values.Value.TryPickT0(out var snowflakes, out _))
{
if (DiscordSnowflake.TryParse(value, out var parsed))
foreach (var snowflake in snowflakes)
{
values.Add(parsed.Value);
values.Add(snowflake);
}
}

Expand Down
Loading