Skip to content

Commit

Permalink
Fix: Don't depend on WebSocket for Interaction service (#2912)
Browse files Browse the repository at this point in the history
* unfuck interaction service to not depend on WS

* Add XML docs

* fix summary refs
  • Loading branch information
quinchs committed Apr 16, 2024
1 parent 13ea566 commit a2f624e
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Discord
{
/// <summary>
Expand All @@ -9,5 +12,18 @@ public interface IAutocompleteInteraction : IDiscordInteraction
/// Gets the autocomplete data of this interaction.
/// </summary>
new IAutocompleteInteractionData Data { get; }

/// <summary>
/// Responds to this interaction with a set of choices.
/// </summary>
/// <param name="result">
/// The set of choices for the user to pick from.
/// <remarks>
/// A max of 25 choices are allowed. Passing <see langword="null"/> for this argument will show the executing user that
/// there is no choices for their autocompleted input.
/// </remarks>
/// </param>
/// <param name="options">The request options for this response.</param>
Task RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ protected virtual string GetLogString(IInteractionContext context)
var result = await GenerateSuggestionsAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false);

if (result.IsSuccess)
switch (autocompleteInteraction)
{
var task = autocompleteInteraction.RespondAsync(result.Suggestions);

await task;

if (task is Task<string> strTask)
{
case RestAutocompleteInteraction restAutocomplete:
var payload = restAutocomplete.Respond(result.Suggestions);

if (context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null)
await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false);
else
await InteractionService._restResponseCallback(context, payload).ConfigureAwait(false);
break;
case SocketAutocompleteInteraction socketAutocomplete:
await socketAutocomplete.RespondAsync(result.Suggestions).ConfigureAwait(false);
break;
}
var payload = strTask.Result;

if (context is IRestInteractionContext {InteractionResponseCallback: not null} restContext)
await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false);
else
await InteractionService._restResponseCallback(context, payload).ConfigureAwait(false);
}
}
await InteractionService._autocompleteHandlerExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
return result;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Discord.Net.Interactions/Discord.Net.Interactions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
<IsTrimmable>false</IsTrimmable>
<IsAotCompatible>false</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />
<ProjectReference Include="..\Discord.Net.Rest\Discord.Net.Rest.csproj" />
<ProjectReference Include="..\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class SlashCommandParameterInfo : CommandParameterInfo
public int? MaxLength { get; }

/// <summary>
/// Gets the <see cref="TypeConverter{T}"/> that will be used to convert the incoming <see cref="Discord.WebSocket.SocketSlashCommandDataOption"/> into
/// Gets the <see cref="TypeConverter{T}"/> that will be used to convert the incoming <see cref="Discord.IDiscordInteractionData"/> into
/// <see cref="CommandParameterInfo.ParameterType"/>.
/// </summary>
public TypeConverter TypeConverter { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Interactions/InteractionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class InteractionContext : IInteractionContext, IRouteMatchContainer
public IReadOnlyCollection<IRouteSegmentMatch> SegmentMatches { get; private set; }

/// <summary>
/// Initializes a new <see cref="SocketInteractionContext{TInteraction}"/>.
/// Initializes a new <see cref="InteractionContext"/>.
/// </summary>
/// <param name="client">The underlying client.</param>
/// <param name="interaction">The underlying interaction.</param>
Expand Down
26 changes: 5 additions & 21 deletions src/Discord.Net.Interactions/InteractionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,16 @@ public class InteractionService : IDisposable
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(DiscordSocketClient discord, InteractionServiceConfig config = null)
: this(() => discord.Rest, config ?? new InteractionServiceConfig()) { }

/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(DiscordShardedClient discord, InteractionServiceConfig config = null)
: this(() => discord.Rest, config ?? new InteractionServiceConfig()) { }

/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(BaseSocketClient discord, InteractionServiceConfig config = null)
: this(() => discord.Rest, config ?? new InteractionServiceConfig()) { }
public InteractionService(DiscordRestClient discord, InteractionServiceConfig config = null)
: this(() => discord, config ?? new InteractionServiceConfig()) { }

/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="discordProvider">The discord client provider.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(DiscordRestClient discord, InteractionServiceConfig config = null)
: this(() => discord, config ?? new InteractionServiceConfig()) { }
public InteractionService(IRestClientProvider discordProvider, InteractionServiceConfig config = null)
: this(() => discordProvider.RestClient, config ?? new InteractionServiceConfig()) { }

private InteractionService(Func<DiscordRestClient> getRestClient, InteractionServiceConfig config = null)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Discord.Net.Rest/DiscordRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Discord.Rest
/// <summary>
/// Provides a client to send REST-based requests to Discord.
/// </summary>
public class DiscordRestClient : BaseDiscordClient, IDiscordClient
public class DiscordRestClient : BaseDiscordClient, IDiscordClient, IRestClientProvider
{
#region DiscordRestClient
private RestApplication _applicationInfo;
Expand Down Expand Up @@ -399,5 +399,7 @@ async Task<IApplicationCommand> IDiscordClient.CreateGlobalApplicationCommand(Ap
async Task<IReadOnlyCollection<IApplicationCommand>> IDiscordClient.BulkOverwriteGlobalApplicationCommand(ApplicationCommandProperties[] properties, RequestOptions options)
=> await BulkOverwriteGlobalCommands(properties, options).ConfigureAwait(false);
#endregion

DiscordRestClient IRestClientProvider.RestClient => this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@ public override string RespondWithModal(Modal modal, RequestOptions options = nu
//IAutocompleteInteraction
/// <inheritdoc/>
IAutocompleteInteractionData IAutocompleteInteraction.Data => Data;

/// <inheritdoc/>
Task IAutocompleteInteraction.RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options)
=>Task.FromResult(Respond(result, options));
}
}
14 changes: 14 additions & 0 deletions src/Discord.Net.Rest/IRestClientProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Discord.Rest;

namespace Discord.Rest;

/// <summary>
/// An interface that represents a client provider for Rest-based clients.
/// </summary>
public interface IRestClientProvider
{
/// <summary>
/// Gets the Rest client of this provider.
/// </summary>
DiscordRestClient RestClient { get; }
}
4 changes: 3 additions & 1 deletion src/Discord.Net.WebSocket/BaseSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Discord.WebSocket
/// <summary>
/// Represents the base of a WebSocket-based Discord client.
/// </summary>
public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient
public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient, IRestClientProvider
{
#region BaseSocketClient
protected readonly DiscordSocketConfig BaseConfig;
Expand Down Expand Up @@ -352,5 +352,7 @@ async Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsyn
return await GetVoiceRegionsAsync().ConfigureAwait(false);
}
#endregion

DiscordRestClient IRestClientProvider.RestClient => Rest;
}
}

0 comments on commit a2f624e

Please sign in to comment.