From 9030a5b2bb15ba356739d6f57dbf71f97d0edc80 Mon Sep 17 00:00:00 2001 From: Bence Szucs <99207620+WhyNot180@users.noreply.github.com> Date: Thu, 16 May 2024 17:25:21 -0400 Subject: [PATCH] [Feature] Config Bypass for CanSendResponse (#2932) * Adds bool switch for DeferAsyncTimeCheck in DiscordConfig * Adds DeferAsyncTimeCheck bool to DiscordSocketClient * Adds DeferAsyncTimeCheck to DeferAsync in SocketCommandBase * Adds DeferAsyncTimeCheck to DeferAsync in SocketModal * Adds DeferAsyncTimeCheck to DeferAsync in SocketMessageComponent * Changes name of DeferAsyncTimeCheck to be more general * Moves ResponseInternalTimeCheck from socket client to base client * Adds ResponseInternalTimeCheck to Rest interactions * Adds ResponseInternalTimeCheck to socket interactions --- src/Discord.Net.Core/DiscordConfig.cs | 8 ++++++++ src/Discord.Net.Rest/BaseDiscordClient.cs | 2 ++ .../Interactions/CommandBase/RestCommandBase.cs | 6 +++--- .../MessageComponents/RestMessageComponent.cs | 10 +++++----- .../Entities/Interactions/Modals/RestModal.cs | 8 ++++---- .../SlashCommands/RestAutocompleteInteraction.cs | 2 +- .../MessageComponents/SocketMessageComponent.cs | 12 ++++++------ .../Entities/Interaction/Modals/SocketModal.cs | 10 +++++----- .../SlashCommands/SocketAutocompleteInteraction.cs | 2 +- .../SocketBaseCommand/SocketCommandBase.cs | 8 ++++---- 10 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/Discord.Net.Core/DiscordConfig.cs b/src/Discord.Net.Core/DiscordConfig.cs index 4056484e39..c98b7c44d8 100644 --- a/src/Discord.Net.Core/DiscordConfig.cs +++ b/src/Discord.Net.Core/DiscordConfig.cs @@ -205,6 +205,14 @@ public class DiscordConfig /// public bool UseInteractionSnowflakeDate { get; set; } = true; + /// + /// Gets or sets whether or not any responses to Discord will make an internal expiration check. + /// + /// + /// This should generally be set to in a development environment due to potential latency issues. + /// + public bool ResponseInternalTimeCheck { get; set; } = true; + /// /// Gets or sets if the Rest/Socket user override formats the string in respect to bidirectional unicode. /// diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs index d369050e72..372dea35a2 100644 --- a/src/Discord.Net.Rest/BaseDiscordClient.cs +++ b/src/Discord.Net.Rest/BaseDiscordClient.cs @@ -45,6 +45,7 @@ public abstract class BaseDiscordClient : IDiscordClient public TokenType TokenType => ApiClient.AuthTokenType; internal bool UseInteractionSnowflakeDate { get; private set; } internal bool FormatUsersInBidirectionalUnicode { get; private set; } + internal bool ResponseInternalTimeCheck { get; private set; } /// Creates a new REST-only Discord client. internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client) @@ -59,6 +60,7 @@ internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient cl UseInteractionSnowflakeDate = config.UseInteractionSnowflakeDate; FormatUsersInBidirectionalUnicode = config.FormatUsersInBidirectionalUnicode; + ResponseInternalTimeCheck = config.ResponseInternalTimeCheck; ApiClient.RequestQueue.RateLimitTriggered += async (id, info, endpoint) => { diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs index ab51adda02..dae9b890d7 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs @@ -86,7 +86,7 @@ public override string Respond( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -317,7 +317,7 @@ public override Task FollowupWithFilesAsync( /// public override string Defer(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); var response = new API.InteractionResponse @@ -352,7 +352,7 @@ public override string Defer(bool ephemeral = false, RequestOptions options = nu /// public override string RespondWithModal(Modal modal, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index c3d0ded3ff..434be40db3 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -82,7 +82,7 @@ public override string Respond( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -153,7 +153,7 @@ public async Task UpdateAsync(Action func, RequestOptions opt if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); if (args.AllowedMentions.IsSpecified) @@ -424,7 +424,7 @@ public override Task FollowupWithFilesAsync( /// public string DeferLoading(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse @@ -458,7 +458,7 @@ public string DeferLoading(bool ephemeral = false, RequestOptions options = null /// public override string Defer(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse @@ -490,7 +490,7 @@ public override string Defer(bool ephemeral = false, RequestOptions options = nu /// public override string RespondWithModal(Modal modal, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse diff --git a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs index 182e9bc7f9..edeeea0b90 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs @@ -51,7 +51,7 @@ internal RestModal(DiscordRestClient client, ModelBase model) /// public override string Defer(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); var response = new API.InteractionResponse @@ -91,7 +91,7 @@ public override string Defer(bool ephemeral = false, RequestOptions options = nu /// public string DeferLoading(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse @@ -320,7 +320,7 @@ public override string Respond( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -486,7 +486,7 @@ public async Task UpdateAsync(Action func, RequestOptions opt if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); if (args.AllowedMentions.IsSpecified) diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs index 9b9096a5eb..dcad1dbd43 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs @@ -55,7 +55,7 @@ internal RestAutocompleteInteraction(DiscordRestClient client, Model model) /// public string Respond(IEnumerable result, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); lock (_lock) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs index 82d6e8d00a..f24a4be6fd 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs @@ -87,7 +87,7 @@ public override async Task RespondWithFilesAsync( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -154,7 +154,7 @@ public override async Task RespondAsync( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -218,7 +218,7 @@ public async Task UpdateAsync(Action func, RequestOptions opt if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); if (args.AllowedMentions.IsSpecified) @@ -418,7 +418,7 @@ public override Task FollowupWithFilesAsync( /// public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse @@ -442,7 +442,7 @@ public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions optio /// public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse @@ -469,7 +469,7 @@ public override async Task RespondWithModalAsync(Modal modal, RequestOptions opt if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); var response = new API.InteractionResponse diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs index 8f87fae1b1..7b1b0a6502 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs @@ -84,7 +84,7 @@ public override async Task RespondWithFilesAsync( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -151,7 +151,7 @@ public override async Task RespondAsync( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -215,7 +215,7 @@ public async Task UpdateAsync(Action func, RequestOptions opt if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); if (args.AllowedMentions.IsSpecified) @@ -420,7 +420,7 @@ public override Task FollowupWithFilesAsync( /// public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse @@ -450,7 +450,7 @@ public override async Task DeferAsync(bool ephemeral = false, RequestOptions opt /// public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement"); var response = new API.InteractionResponse diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs index 0889d00c29..7e6b484ab9 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs @@ -57,7 +57,7 @@ internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, /// public async Task RespondAsync(IEnumerable result, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); lock (_lock) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs index 1f3f4928ce..3b6da47742 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs @@ -82,7 +82,7 @@ public override async Task RespondAsync( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -143,7 +143,7 @@ public override async Task RespondWithModalAsync(Modal modal, RequestOptions opt if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); var response = new API.InteractionResponse @@ -188,7 +188,7 @@ public override async Task RespondWithFilesAsync( if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); embeds ??= Array.Empty(); @@ -352,7 +352,7 @@ public override Task FollowupWithFilesAsync( /// public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null) { - if (!InteractionHelper.CanSendResponse(this)) + if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck) throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds!"); var response = new API.InteractionResponse