Browse Source

Add response check to socket auto complete

pull/1923/head
quin lynch 3 years ago
parent
commit
a862507630
1 changed files with 23 additions and 3 deletions
  1. +23
    -3
      src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteraction.cs

+ 23
- 3
src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteraction.cs View File

@@ -18,6 +18,9 @@ namespace Discord.WebSocket
/// </summary> /// </summary>
public new SocketAutocompleteInteractionData Data { get; } public new SocketAutocompleteInteractionData Data { get; }


internal override bool _hasResponded { get; set; }
private object _lock = new object();

internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel) internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel)
: base(client, model.Id, channel) : base(client, model.Id, channel)
{ {
@@ -50,8 +53,25 @@ namespace Discord.WebSocket
/// <returns> /// <returns>
/// A task that represents the asynchronous operation of responding to this interaction. /// A task that represents the asynchronous operation of responding to this interaction.
/// </returns> /// </returns>
public Task RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options = null)
=> InteractionHelper.SendAutocompleteResultAsync(Discord, result, Id, Token, options);
public async Task RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

lock (_lock)
{
if (_hasResponded)
{
throw new InvalidOperationException("Cannot respond twice to the same interaction");
}
}

await InteractionHelper.SendAutocompleteResultAsync(Discord, result, Id, Token, options).ConfigureAwait(false);
lock (_lock)
{
_hasResponded = true;
}
}


/// <summary> /// <summary>
/// Responds to this interaction with a set of choices. /// Responds to this interaction with a set of choices.
@@ -68,7 +88,7 @@ namespace Discord.WebSocket
/// A task that represents the asynchronous operation of responding to this interaction. /// A task that represents the asynchronous operation of responding to this interaction.
/// </returns> /// </returns>
public Task RespondAsync(RequestOptions options = null, params AutocompleteResult[] result) public Task RespondAsync(RequestOptions options = null, params AutocompleteResult[] result)
=> InteractionHelper.SendAutocompleteResultAsync(Discord, result, Id, Token, options);
=> RespondAsync(result, options);


/// <inheritdoc/> /// <inheritdoc/>
[Obsolete("Autocomplete interactions cannot be deferred!", true)] [Obsolete("Autocomplete interactions cannot be deferred!", true)]


Loading…
Cancel
Save