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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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;
30 changes: 19 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 @@ -149,13 +148,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 when data.Values.Value.IsT1 => BuildParametersFromStringData(data),
carlst99 marked this conversation as resolved.
Show resolved Hide resolved
ComponentType.UserSelect
or ComponentType.RoleSelect
or ComponentType.MentionableSelect
Expand All @@ -179,6 +172,21 @@ or ComponentType.ChannelSelect
return await TryExecuteCommandAsync(context, commandPath, parameters, ct);
}

private static Result<Dictionary<string, IReadOnlyList<string>>> BuildParametersFromStringData
(
IMessageComponentData data
)
{
var parameters = new Dictionary<string, IReadOnlyList<string>>();

if (data.Values.Value.TryPickT1(out var values, out _))
{
parameters.Add("values", values);
}

carlst99 marked this conversation as resolved.
Show resolved Hide resolved
return parameters;
}

private static Result<Dictionary<string, IReadOnlyList<string>>> BuildParametersFromResolvedData
(
IMessageComponentData data
Expand All @@ -187,11 +195,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