Fix UpdateAsync Remove Attachment Bug (#2766)

* Similar to PR #2742 but this now fixes the UpdateAsync attachments bug.

* Update functions.

* `Update` => `UpdateAsync`

---------

Co-authored-by: Misha133 <mihagribkov133@gmail.com>
This commit is contained in:
Mike
2023-09-02 13:18:35 -04:00
committed by GitHub
parent 589c58adb0
commit a668757669
3 changed files with 90 additions and 26 deletions

View File

@@ -141,8 +141,7 @@ namespace Discord.Rest
/// </summary>
/// <param name="func">A delegate containing the properties to modify the message with.</param>
/// <param name="options">The request options for this <see langword="async"/> request.</param>
/// <returns>A string that contains json to write back to the incoming http request.</returns>
public string Update(Action<MessageProperties> func, RequestOptions options = null)
public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions options = null)
{
var args = new MessageProperties();
func(args);
@@ -200,20 +199,43 @@ namespace Discord.Rest
}
}
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<API.AllowedMentions>.Unspecified,
Embeds = apiEmbeds?.ToArray() ?? Optional<API.Embed[]>.Unspecified,
Components = args.Components.IsSpecified
? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty<API.ActionRowComponent>()
: Optional<API.ActionRowComponent[]>.Unspecified,
Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional<MessageFlags>.Unspecified : Optional<MessageFlags>.Unspecified
}
};
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
}
else
{
var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty<FileAttachment>();
var response = new API.Rest.UploadInteractionFileParams(attachments)
{
Type = InteractionResponseType.UpdateMessage,
Content = args.Content,
AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional<API.AllowedMentions>.Unspecified,
Embeds = apiEmbeds?.ToArray() ?? Optional<API.Embed[]>.Unspecified,
Components = args.Components.IsSpecified
MessageComponents = args.Components.IsSpecified
? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty<API.ActionRowComponent>()
: Optional<API.ActionRowComponent[]>.Unspecified,
Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional<MessageFlags>.Unspecified : Optional<MessageFlags>.Unspecified
}
};
};
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
}
lock (_lock)
{
@@ -221,11 +243,9 @@ namespace Discord.Rest
{
throw new InvalidOperationException("Cannot respond, update, or defer twice to the same interaction");
}
HasResponded = true;
}
return SerializePayload(response);
HasResponded = true;
}
/// <inheritdoc/>
@@ -493,7 +513,7 @@ namespace Discord.Rest
/// <inheritdoc />
Task IComponentInteraction.UpdateAsync(Action<MessageProperties> func, RequestOptions options)
=> Task.FromResult(Update(func, options));
=> UpdateAsync(func, options);
/// <inheritdoc />
Task IComponentInteraction.DeferLoadingAsync(bool ephemeral, RequestOptions options)

View File

@@ -512,20 +512,43 @@ namespace Discord.Rest
}
}
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<API.AllowedMentions>.Unspecified,
Embeds = apiEmbeds?.ToArray() ?? Optional<API.Embed[]>.Unspecified,
Components = args.Components.IsSpecified
? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty<API.ActionRowComponent>()
: Optional<API.ActionRowComponent[]>.Unspecified,
Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional<MessageFlags>.Unspecified : Optional<MessageFlags>.Unspecified
}
};
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
}
else
{
var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty<FileAttachment>();
var response = new API.Rest.UploadInteractionFileParams(attachments)
{
Type = InteractionResponseType.UpdateMessage,
Content = args.Content,
AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional<API.AllowedMentions>.Unspecified,
Embeds = apiEmbeds?.ToArray() ?? Optional<API.Embed[]>.Unspecified,
Components = args.Components.IsSpecified
MessageComponents = args.Components.IsSpecified
? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty<API.ActionRowComponent>()
: Optional<API.ActionRowComponent[]>.Unspecified,
Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional<MessageFlags>.Unspecified : Optional<MessageFlags>.Unspecified
}
};
};
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
}
lock (_lock)
{
@@ -535,7 +558,6 @@ namespace Discord.Rest
}
}
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
HasResponded = true;
}
}

View File

@@ -258,20 +258,43 @@ namespace Discord.WebSocket
}
}
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<API.AllowedMentions>.Unspecified,
Embeds = apiEmbeds?.ToArray() ?? Optional<API.Embed[]>.Unspecified,
Components = args.Components.IsSpecified
? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty<API.ActionRowComponent>()
: Optional<API.ActionRowComponent[]>.Unspecified,
Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional<MessageFlags>.Unspecified : Optional<MessageFlags>.Unspecified
}
};
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
}
else
{
var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty<FileAttachment>();
var response = new API.Rest.UploadInteractionFileParams(attachments)
{
Type = InteractionResponseType.UpdateMessage,
Content = args.Content,
AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value?.ToModel() : Optional<API.AllowedMentions>.Unspecified,
Embeds = apiEmbeds?.ToArray() ?? Optional<API.Embed[]>.Unspecified,
Components = args.Components.IsSpecified
MessageComponents = args.Components.IsSpecified
? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty<API.ActionRowComponent>()
: Optional<API.ActionRowComponent[]>.Unspecified,
Flags = args.Flags.IsSpecified ? args.Flags.Value ?? Optional<MessageFlags>.Unspecified : Optional<MessageFlags>.Unspecified
}
};
};
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
}
lock (_lock)
{
@@ -281,7 +304,6 @@ namespace Discord.WebSocket
}
}
await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
HasResponded = true;
}