Browse Source

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>
tags/3.6.0
Cenk Ergen GitHub 3 years ago
parent
commit
4ce1801bdf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 3 deletions
  1. +24
    -0
      src/Discord.Net.Core/Interactions/IRouteMatchContainer.cs
  2. +16
    -0
      src/Discord.Net.Core/Interactions/IRouteSegmentMatch.cs
  3. +16
    -0
      src/Discord.Net.Core/Interactions/RouteSegmentMatch.cs
  4. +13
    -1
      src/Discord.Net.Interactions/InteractionContext.cs
  5. +19
    -0
      src/Discord.Net.Interactions/InteractionService.cs
  6. +13
    -1
      src/Discord.Net.Rest/Interactions/RestInteractionContext.cs
  7. +13
    -1
      src/Discord.Net.WebSocket/Interactions/SocketInteractionContext.cs

+ 24
- 0
src/Discord.Net.Core/Interactions/IRouteMatchContainer.cs View File

@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace Discord
{
/// <summary>
/// Represents a container for temporarily storing CustomId wild card matches of a component.
/// </summary>
public interface IRouteMatchContainer
{
/// <summary>
/// Gets the collection of captured route segments in this container.
/// </summary>
/// <returns>
/// A collection of captured route segments.
///</returns>
IEnumerable<IRouteSegmentMatch> SegmentMatches { get; }

/// <summary>
/// Sets the <see cref="SegmentMatches"/> property of this container.
/// </summary>
/// <param name="segmentMatches">The collection of captured route segments.</param>
void SetSegmentMatches(IEnumerable<IRouteSegmentMatch> segmentMatches);
}
}

+ 16
- 0
src/Discord.Net.Core/Interactions/IRouteSegmentMatch.cs View File

@@ -0,0 +1,16 @@
namespace Discord
{
/// <summary>
/// Represents an object for storing a CustomId wild card match.
/// </summary>
public interface IRouteSegmentMatch
{
/// <summary>
/// Gets the captured value of this wild card match.
/// </summary>
/// <returns>
/// The value of this wild card.
/// </returns>
string Value { get; }
}
}

+ 16
- 0
src/Discord.Net.Core/Interactions/RouteSegmentMatch.cs View File

@@ -0,0 +1,16 @@
namespace Discord
{
/// <summary>
/// Represents an object for storing a CustomId wild card match.
/// </summary>
internal record RouteSegmentMatch : IRouteSegmentMatch
{
/// <inheritdoc/>
public string Value { get; }

public RouteSegmentMatch(string value)
{
Value = value;
}
}
}

+ 13
- 1
src/Discord.Net.Interactions/InteractionContext.cs View File

@@ -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;
}
}

+ 19
- 0
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -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);



+ 13
- 1
src/Discord.Net.Rest/Interactions/RestInteractionContext.cs View File

@@ -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
/// <summary>
/// Represents a Rest based context of an <see cref="IDiscordInteraction"/>.
/// </summary>
public class RestInteractionContext<TInteraction> : IRestInteractionContext
public class RestInteractionContext<TInteraction> : IRestInteractionContext, IRouteMatchContainer
where TInteraction : RestInteraction
{
/// <summary>
@@ -45,6 +47,9 @@ namespace Discord.Rest
/// </remarks>
public Func<string, Task> InteractionResponseCallback { get; set; }

/// <inheritdoc cref="IRouteMatchContainer.SegmentMatches"/>
public IReadOnlyCollection<IRouteSegmentMatch> SegmentMatches { get; private set; }

/// <summary>
/// Initializes a new <see cref="RestInteractionContext{TInteraction}"/>.
/// </summary>
@@ -71,6 +76,13 @@ namespace Discord.Rest
InteractionResponseCallback = interactionResponseCallback;
}

/// <inheritdoc/>
public void SetSegmentMatches(IEnumerable<IRouteSegmentMatch> segmentMatches) => SegmentMatches = segmentMatches.ToImmutableArray();

//IRouteMatchContainer
/// <inheritdoc/>
IEnumerable<IRouteSegmentMatch> IRouteMatchContainer.SegmentMatches => SegmentMatches;

// IInterationContext
/// <inheritdoc/>
IDiscordClient IInteractionContext.Client => Client;


+ 13
- 1
src/Discord.Net.WebSocket/Interactions/SocketInteractionContext.cs View File

@@ -1,11 +1,13 @@
using Discord.WebSocket;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace Discord.Interactions
{
/// <summary>
/// Represents a Web-Socket based context of an <see cref="IDiscordInteraction"/>.
/// </summary>
public class SocketInteractionContext<TInteraction> : IInteractionContext
public class SocketInteractionContext<TInteraction> : IInteractionContext, IRouteMatchContainer
where TInteraction : SocketInteraction
{
/// <summary>
@@ -36,6 +38,9 @@ namespace Discord.Interactions
/// </summary>
public TInteraction Interaction { get; }

/// <inheritdoc cref="IRouteMatchContainer.SegmentMatches"/>
public IReadOnlyCollection<IRouteSegmentMatch> SegmentMatches { get; private set; }

/// <summary>
/// Initializes a new <see cref="SocketInteractionContext{TInteraction}"/>.
/// </summary>
@@ -50,6 +55,13 @@ namespace Discord.Interactions
Interaction = interaction;
}

/// <inheritdoc/>
public void SetSegmentMatches(IEnumerable<IRouteSegmentMatch> segmentMatches) => SegmentMatches = segmentMatches.ToImmutableArray();

//IRouteMatchContainer
/// <inheritdoc/>
IEnumerable<IRouteSegmentMatch> IRouteMatchContainer.SegmentMatches => SegmentMatches;

// IInteractionContext
/// <inheritdoc/>
IDiscordClient IInteractionContext.Client => Client;


Loading…
Cancel
Save