diff --git a/src/Discord.Net.Core/Interactions/IRouteMatchContainer.cs b/src/Discord.Net.Core/Interactions/IRouteMatchContainer.cs new file mode 100644 index 000000000..f9a3a3183 --- /dev/null +++ b/src/Discord.Net.Core/Interactions/IRouteMatchContainer.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace Discord +{ + /// + /// Represents a container for temporarily storing CustomId wild card matches of a component. + /// + public interface IRouteMatchContainer + { + /// + /// Gets the collection of captured route segments in this container. + /// + /// + /// A collection of captured route segments. + /// + IEnumerable SegmentMatches { get; } + + /// + /// Sets the property of this container. + /// + /// The collection of captured route segments. + void SetSegmentMatches(IEnumerable segmentMatches); + } +} diff --git a/src/Discord.Net.Core/Interactions/IRouteSegmentMatch.cs b/src/Discord.Net.Core/Interactions/IRouteSegmentMatch.cs new file mode 100644 index 000000000..675bd6754 --- /dev/null +++ b/src/Discord.Net.Core/Interactions/IRouteSegmentMatch.cs @@ -0,0 +1,16 @@ +namespace Discord +{ + /// + /// Represents an object for storing a CustomId wild card match. + /// + public interface IRouteSegmentMatch + { + /// + /// Gets the captured value of this wild card match. + /// + /// + /// The value of this wild card. + /// + string Value { get; } + } +} diff --git a/src/Discord.Net.Core/Interactions/RouteSegmentMatch.cs b/src/Discord.Net.Core/Interactions/RouteSegmentMatch.cs new file mode 100644 index 000000000..f1d80cfea --- /dev/null +++ b/src/Discord.Net.Core/Interactions/RouteSegmentMatch.cs @@ -0,0 +1,16 @@ +namespace Discord +{ + /// + /// Represents an object for storing a CustomId wild card match. + /// + internal record RouteSegmentMatch : IRouteSegmentMatch + { + /// + public string Value { get; } + + public RouteSegmentMatch(string value) + { + Value = value; + } + } +} diff --git a/src/Discord.Net.Interactions/InteractionContext.cs b/src/Discord.Net.Interactions/InteractionContext.cs index 99a8d8736..024ab5ef8 100644 --- a/src/Discord.Net.Interactions/InteractionContext.cs +++ b/src/Discord.Net.Interactions/InteractionContext.cs @@ -1,7 +1,10 @@ +using System.Collections.Generic; +using System.Collections.Immutable; + namespace Discord.Interactions { /// - public class InteractionContext : IInteractionContext + public class InteractionContext : IInteractionContext, IRouteMatchContainer { /// public IDiscordClient Client { get; } @@ -13,6 +16,8 @@ namespace Discord.Interactions public IUser User { get; } /// public IDiscordInteraction Interaction { get; } + /// + public IReadOnlyCollection SegmentMatches { get; private set; } /// /// Initializes a new . @@ -30,5 +35,12 @@ namespace Discord.Interactions User = interaction.User; Interaction = interaction; } + + /// + public void SetSegmentMatches(IEnumerable segmentMatches) => SegmentMatches = segmentMatches.ToImmutableArray(); + + //IRouteMatchContainer + /// + IEnumerable IRouteMatchContainer.SegmentMatches => SegmentMatches; } } diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index 01fb8cc9d..8eb5799d6 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -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(IInteractionContext context, SearchResult 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); diff --git a/src/Discord.Net.Rest/Interactions/RestInteractionContext.cs b/src/Discord.Net.Rest/Interactions/RestInteractionContext.cs index 196c6133b..d407f5103 100644 --- a/src/Discord.Net.Rest/Interactions/RestInteractionContext.cs +++ b/src/Discord.Net.Rest/Interactions/RestInteractionContext.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Collections.Immutable; using System.Threading.Tasks; namespace Discord.Rest @@ -6,7 +8,7 @@ namespace Discord.Rest /// /// Represents a Rest based context of an . /// - public class RestInteractionContext : IRestInteractionContext + public class RestInteractionContext : IRestInteractionContext, IRouteMatchContainer where TInteraction : RestInteraction { /// @@ -45,6 +47,9 @@ namespace Discord.Rest /// public Func InteractionResponseCallback { get; set; } + /// + public IReadOnlyCollection SegmentMatches { get; private set; } + /// /// Initializes a new . /// @@ -71,6 +76,13 @@ namespace Discord.Rest InteractionResponseCallback = interactionResponseCallback; } + /// + public void SetSegmentMatches(IEnumerable segmentMatches) => SegmentMatches = segmentMatches.ToImmutableArray(); + + //IRouteMatchContainer + /// + IEnumerable IRouteMatchContainer.SegmentMatches => SegmentMatches; + // IInterationContext /// IDiscordClient IInteractionContext.Client => Client; diff --git a/src/Discord.Net.WebSocket/Interactions/SocketInteractionContext.cs b/src/Discord.Net.WebSocket/Interactions/SocketInteractionContext.cs index 4cd9ef264..a2a101839 100644 --- a/src/Discord.Net.WebSocket/Interactions/SocketInteractionContext.cs +++ b/src/Discord.Net.WebSocket/Interactions/SocketInteractionContext.cs @@ -1,11 +1,13 @@ using Discord.WebSocket; +using System.Collections.Generic; +using System.Collections.Immutable; namespace Discord.Interactions { /// /// Represents a Web-Socket based context of an . /// - public class SocketInteractionContext : IInteractionContext + public class SocketInteractionContext : IInteractionContext, IRouteMatchContainer where TInteraction : SocketInteraction { /// @@ -36,6 +38,9 @@ namespace Discord.Interactions /// public TInteraction Interaction { get; } + /// + public IReadOnlyCollection SegmentMatches { get; private set; } + /// /// Initializes a new . /// @@ -50,6 +55,13 @@ namespace Discord.Interactions Interaction = interaction; } + /// + public void SetSegmentMatches(IEnumerable segmentMatches) => SegmentMatches = segmentMatches.ToImmutableArray(); + + //IRouteMatchContainer + /// + IEnumerable IRouteMatchContainer.SegmentMatches => SegmentMatches; + // IInteractionContext /// IDiscordClient IInteractionContext.Client => Client;