feature: Passing CustomId matches into contexts (#2136)
* add logic for passing the wild card captures into the context * move concrete impl of IRouteSegmentMatch to internal * Apply suggestions from code review Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> * fix build errors * Apply suggestions from code review Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Discord.Interactions
|
||||
{
|
||||
/// <inheritdoc cref="IInteractionContext"/>
|
||||
public class InteractionContext : IInteractionContext
|
||||
public class InteractionContext : IInteractionContext, IRouteMatchContainer
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public IDiscordClient Client { get; }
|
||||
@@ -13,6 +16,8 @@ namespace Discord.Interactions
|
||||
public IUser User { get; }
|
||||
/// <inheritdoc/>
|
||||
public IDiscordInteraction Interaction { get; }
|
||||
/// <inheritdoc cref="IRouteMatchContainer.SegmentMatches"/>
|
||||
public IReadOnlyCollection<IRouteSegmentMatch> SegmentMatches { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new <see cref="SocketInteractionContext{TInteraction}"/>.
|
||||
@@ -30,5 +35,12 @@ namespace Discord.Interactions
|
||||
User = interaction.User;
|
||||
Interaction = interaction;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void SetSegmentMatches(IEnumerable<IRouteSegmentMatch> segmentMatches) => SegmentMatches = segmentMatches.ToImmutableArray();
|
||||
|
||||
//IRouteMatchContainer
|
||||
/// <inheritdoc/>
|
||||
IEnumerable<IRouteSegmentMatch> IRouteMatchContainer.SegmentMatches => SegmentMatches;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -775,6 +775,9 @@ namespace Discord.Interactions
|
||||
await _componentCommandExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
SetMatchesIfApplicable(context, result);
|
||||
|
||||
return await result.Command.ExecuteAsync(context, services, result.RegexCaptureGroups).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -819,9 +822,25 @@ namespace Discord.Interactions
|
||||
await _componentCommandExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
SetMatchesIfApplicable(context, result);
|
||||
|
||||
return await result.Command.ExecuteAsync(context, services, result.RegexCaptureGroups).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static void SetMatchesIfApplicable<T>(IInteractionContext context, SearchResult<T> searchResult)
|
||||
where T : class, ICommandInfo
|
||||
{
|
||||
if (!searchResult.Command.SupportsWildCards || context is not IRouteMatchContainer matchContainer)
|
||||
return;
|
||||
|
||||
var matches = new RouteSegmentMatch[searchResult.RegexCaptureGroups.Length];
|
||||
for (var i = 0; i < searchResult.RegexCaptureGroups.Length; i++)
|
||||
matches[i] = new RouteSegmentMatch(searchResult.RegexCaptureGroups[i]);
|
||||
|
||||
matchContainer.SetSegmentMatches(matches);
|
||||
}
|
||||
|
||||
internal TypeConverter GetTypeConverter(Type type, IServiceProvider services = null)
|
||||
=> _typeConverterMap.Get(type, services);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user