From a8625076308b559d79174cba1d9ccf9e13807d26 Mon Sep 17 00:00:00 2001 From: quin lynch Date: Tue, 12 Oct 2021 20:58:34 -0300 Subject: [PATCH] Add response check to socket auto complete --- .../SocketAutocompleteInteraction.cs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteraction.cs index 6aa18db23..e58589c08 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketAutocompleteInteraction.cs @@ -18,6 +18,9 @@ namespace Discord.WebSocket /// public new SocketAutocompleteInteractionData Data { get; } + internal override bool _hasResponded { get; set; } + private object _lock = new object(); + internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel) : base(client, model.Id, channel) { @@ -50,8 +53,25 @@ namespace Discord.WebSocket /// /// A task that represents the asynchronous operation of responding to this interaction. /// - public Task RespondAsync(IEnumerable result, RequestOptions options = null) - => InteractionHelper.SendAutocompleteResultAsync(Discord, result, Id, Token, options); + public async Task RespondAsync(IEnumerable 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; + } + } /// /// 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. /// public Task RespondAsync(RequestOptions options = null, params AutocompleteResult[] result) - => InteractionHelper.SendAutocompleteResultAsync(Discord, result, Id, Token, options); + => RespondAsync(result, options); /// [Obsolete("Autocomplete interactions cannot be deferred!", true)]