Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UpdateAsync Remove Attachment Bug #2766

Merged
merged 5 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ public override string Respond(
/// </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);
Expand Down Expand Up @@ -200,32 +199,53 @@ public string Update(Action<MessageProperties> func, RequestOptions options = nu
}
}

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)
{
if (HasResponded)
{
throw new InvalidOperationException("Cannot respond, update, or defer twice to the same interaction");
}

HasResponded = true;
}

return SerializePayload(response);
HasResponded = true;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -493,7 +513,7 @@ public override string RespondWithModal(Modal modal, RequestOptions options = nu

/// <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)
Expand Down
36 changes: 29 additions & 7 deletions src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,43 @@ public async Task UpdateAsync(Action<MessageProperties> 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<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)
{
Expand All @@ -535,7 +558,6 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
}
}

await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
HasResponded = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
}
else
{
var response = new API.Rest.UploadInteractionFileParams(args.Attachments.Value.ToArray())
var attachments = args.Attachments.Value?.ToArray() ?? Array.Empty<FileAttachment>();

var response = new API.Rest.UploadInteractionFileParams(attachments)
{
Type = InteractionResponseType.UpdateMessage,
Content = args.Content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,43 @@ public async Task UpdateAsync(Action<MessageProperties> 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<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)
{
Expand All @@ -281,7 +304,6 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
}
}

await InteractionHelper.SendInteractionResponseAsync(Discord, response, this, Channel, options).ConfigureAwait(false);
HasResponded = true;
}

Expand Down