From 4e16a43ae0c124958b4277850feb094ebd68da73 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 20 Aug 2023 23:25:15 -0400 Subject: [PATCH 1/5] Similar to PR #2742 but this now fixes the UpdateAsync attachments bug. --- src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs | 3 +++ src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs | 3 ++- .../Entities/Interaction/Modals/SocketModal.cs | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs b/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs index 3685d7a99f..b3048d0699 100644 --- a/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs +++ b/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs @@ -30,5 +30,8 @@ internal class InteractionCallbackData [JsonProperty("custom_id")] public Optional CustomId { get; set; } + + [JsonProperty("attachments")] + public Optional Attachments { get; set; } } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs index 37abc22dba..d33e350566 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs @@ -523,7 +523,8 @@ public async Task UpdateAsync(Action func, RequestOptions opt Components = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() : Optional.Unspecified, - Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified, + Attachments = args.Attachments.Value?.ToArray() ?? Array.Empty() } }; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs index 20daf7a059..91b5a69416 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs @@ -269,7 +269,8 @@ public async Task UpdateAsync(Action func, RequestOptions opt Components = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() : Optional.Unspecified, - Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified, + Attachments = args.Attachments.Value?.ToArray() ?? Array.Empty() } }; From 93e6e2a36db501e5b5ab92f2bff19deb09953dea Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 22 Aug 2023 21:19:51 -0400 Subject: [PATCH 2/5] Update functions. --- .../API/Common/InteractionCallbackData.cs | 3 -- .../MessageComponents/RestMessageComponent.cs | 36 +++++++++++++---- .../Entities/Interactions/Modals/RestModal.cs | 39 ++++++++++++++----- .../Interaction/Modals/SocketModal.cs | 39 ++++++++++++++----- 4 files changed, 89 insertions(+), 28 deletions(-) diff --git a/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs b/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs index b3048d0699..3685d7a99f 100644 --- a/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs +++ b/src/Discord.Net.Rest/API/Common/InteractionCallbackData.cs @@ -30,8 +30,5 @@ internal class InteractionCallbackData [JsonProperty("custom_id")] public Optional CustomId { get; set; } - - [JsonProperty("attachments")] - public Optional Attachments { get; set; } } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index 7280c7618c..c5c6616db7 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -200,20 +200,42 @@ public string Update(Action func, RequestOptions options = nu } } - var response = new API.InteractionResponse + string result; + if (!args.Attachments.IsSpecified) { - Type = InteractionResponseType.UpdateMessage, - Data = new API.InteractionCallbackData + var response = new API.InteractionResponse + { + Type = InteractionResponseType.UpdateMessage, + Data = new API.InteractionCallbackData + { + Content = args.Content, + AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional.Unspecified, + Embeds = apiEmbeds?.ToArray() ?? Optional.Unspecified, + Components = args.Components.IsSpecified + ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() + : Optional.Unspecified, + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + } + }; + result = SerializePayload(response); + } + else + { + var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty(); + + var response = new API.Rest.UploadInteractionFileParams(attachments) { + Type = InteractionResponseType.UpdateMessage, Content = args.Content, AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional.Unspecified, Embeds = apiEmbeds?.ToArray() ?? Optional.Unspecified, - Components = args.Components.IsSpecified + MessageComponents = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() : Optional.Unspecified, Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified - } - }; + }; + result = SerializePayload(response); + } lock (_lock) { @@ -225,7 +247,7 @@ public string Update(Action func, RequestOptions options = nu HasResponded = true; } - return SerializePayload(response); + return result; } /// diff --git a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs index d33e350566..33220d8841 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs @@ -512,21 +512,43 @@ public async Task UpdateAsync(Action func, RequestOptions opt } } - var response = new API.InteractionResponse + if (!args.Attachments.IsSpecified) { - Type = InteractionResponseType.UpdateMessage, - Data = new API.InteractionCallbackData + var response = new API.InteractionResponse + { + Type = InteractionResponseType.UpdateMessage, + Data = new API.InteractionCallbackData + { + Content = args.Content, + AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional.Unspecified, + Embeds = apiEmbeds?.ToArray() ?? Optional.Unspecified, + Components = args.Components.IsSpecified + ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() + : Optional.Unspecified, + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + } + }; + + await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); + } + else + { + var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty(); + + var response = new API.Rest.UploadInteractionFileParams(attachments) { + Type = InteractionResponseType.UpdateMessage, Content = args.Content, AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional.Unspecified, Embeds = apiEmbeds?.ToArray() ?? Optional.Unspecified, - Components = args.Components.IsSpecified + MessageComponents = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() : Optional.Unspecified, - Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified, - Attachments = args.Attachments.Value?.ToArray() ?? Array.Empty() - } - }; + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + }; + + await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); + } lock (_lock) { @@ -536,7 +558,6 @@ public async Task UpdateAsync(Action func, RequestOptions opt } } - await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); HasResponded = true; } } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs index 91b5a69416..ff1d4dc81c 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs @@ -258,21 +258,43 @@ public async Task UpdateAsync(Action func, RequestOptions opt } } - var response = new API.InteractionResponse + if (!args.Attachments.IsSpecified) { - Type = InteractionResponseType.UpdateMessage, - Data = new API.InteractionCallbackData + var response = new API.InteractionResponse + { + Type = InteractionResponseType.UpdateMessage, + Data = new API.InteractionCallbackData + { + Content = args.Content, + AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional.Unspecified, + Embeds = apiEmbeds?.ToArray() ?? Optional.Unspecified, + Components = args.Components.IsSpecified + ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() + : Optional.Unspecified, + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + } + }; + + await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); + } + else + { + var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty(); + + var response = new API.Rest.UploadInteractionFileParams(attachments) { + Type = InteractionResponseType.UpdateMessage, Content = args.Content, AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional.Unspecified, Embeds = apiEmbeds?.ToArray() ?? Optional.Unspecified, - Components = args.Components.IsSpecified + MessageComponents = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty() : Optional.Unspecified, - Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified, - Attachments = args.Attachments.Value?.ToArray() ?? Array.Empty() - } - }; + Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified + }; + + await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); + } lock (_lock) { @@ -282,7 +304,6 @@ public async Task UpdateAsync(Action func, RequestOptions opt } } - await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); HasResponded = true; } From c99d40cfd050557b773ee0e602001f217c20eafc Mon Sep 17 00:00:00 2001 From: Misha133 Date: Sun, 27 Aug 2023 22:58:10 +0300 Subject: [PATCH 3/5] some fixes on the REST part --- .../MessageComponents/RestMessageComponent.cs | 15 +++++++-------- .../MessageComponents/SocketMessageComponent.cs | 4 +++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index c5c6616db7..c6ed46b307 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -142,7 +142,7 @@ public override string Respond( /// A delegate containing the properties to modify the message with. /// The request options for this request. /// A string that contains json to write back to the incoming http request. - public string Update(Action func, RequestOptions options = null) + public async Task Update(Action func, RequestOptions options = null) { var args = new MessageProperties(); func(args); @@ -200,7 +200,6 @@ public string Update(Action func, RequestOptions options = nu } } - string result; if (!args.Attachments.IsSpecified) { var response = new API.InteractionResponse @@ -217,7 +216,8 @@ public string Update(Action func, RequestOptions options = nu Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified } }; - result = SerializePayload(response); + + await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); } else { @@ -234,7 +234,8 @@ public string Update(Action func, RequestOptions options = nu : Optional.Unspecified, Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional.Unspecified : Optional.Unspecified }; - result = SerializePayload(response); + + await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false); } lock (_lock) @@ -243,11 +244,9 @@ public string Update(Action func, RequestOptions options = nu { throw new InvalidOperationException("Cannot respond, update, or defer twice to the same interaction"); } - - HasResponded = true; } - return result; + HasResponded = true; } /// @@ -515,7 +514,7 @@ public override string RespondWithModal(Modal modal, RequestOptions options = nu /// Task IComponentInteraction.UpdateAsync(Action func, RequestOptions options) - => Task.FromResult(Update(func, options)); + => Update(func, options); /// Task IComponentInteraction.DeferLoadingAsync(bool ephemeral, RequestOptions options) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs index 8758b98788..6691bc6e1d 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs @@ -286,7 +286,9 @@ public async Task UpdateAsync(Action func, RequestOptions opt } else { - var response = new API.Rest.UploadInteractionFileParams(args.Attachments.Value.ToArray()) + var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty(); + + var response = new API.Rest.UploadInteractionFileParams(attachments) { Type = InteractionResponseType.UpdateMessage, Content = args.Content, From 8584cdb515054cd62c819eeb614701823a9ed952 Mon Sep 17 00:00:00 2001 From: Misha133 Date: Sun, 27 Aug 2023 22:59:34 +0300 Subject: [PATCH 4/5] fix xmldoc --- .../Interactions/MessageComponents/RestMessageComponent.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index c6ed46b307..4be40a996e 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -141,7 +141,6 @@ public override string Respond( /// /// A delegate containing the properties to modify the message with. /// The request options for this request. - /// A string that contains json to write back to the incoming http request. public async Task Update(Action func, RequestOptions options = null) { var args = new MessageProperties(); From d5eea6eb4cde9c711a9a2c7a9f27b746e0f4a366 Mon Sep 17 00:00:00 2001 From: Misha133 Date: Sun, 27 Aug 2023 23:22:17 +0300 Subject: [PATCH 5/5] `Update` => `UpdateAsync` --- .../Interactions/MessageComponents/RestMessageComponent.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index 4be40a996e..a59767c0f8 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -141,7 +141,7 @@ public override string Respond( /// /// A delegate containing the properties to modify the message with. /// The request options for this request. - public async Task Update(Action func, RequestOptions options = null) + public async Task UpdateAsync(Action func, RequestOptions options = null) { var args = new MessageProperties(); func(args); @@ -513,7 +513,7 @@ public override string RespondWithModal(Modal modal, RequestOptions options = nu /// Task IComponentInteraction.UpdateAsync(Action func, RequestOptions options) - => Update(func, options); + => UpdateAsync(func, options); /// Task IComponentInteraction.DeferLoadingAsync(bool ephemeral, RequestOptions options)