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

ModifyAsync Attachments Bug Fix #2236 #2742

Merged
merged 4 commits into from
Aug 10, 2023
Merged

ModifyAsync Attachments Bug Fix #2236 #2742

merged 4 commits into from
Aug 10, 2023

Conversation

DeclanFrampton
Copy link
Contributor

@DeclanFrampton DeclanFrampton commented Jul 28, 2023

PR in relation to #2236

Edits Permitted

Simple fix, could be improved if necessary. ModifyAsync can now has access to an Attachment object if an empty array is passed the image(s) will be removed from the target interaction being modified.

@DeclanFrampton
Copy link
Contributor Author

DeclanFrampton commented Jul 28, 2023

Test interactions to make life easier for reviewer.

[SlashCommand("create_embed", "creates embed with image")]
public async Task CreateEmbed()
{
    var fileBuffer = await File.ReadAllBytesAsync("file_path_here");
    var file = new FileAttachment(new MemoryStream(fileBuffer), "test.png");

    await RespondWithFileAsync(file, embed: new EmbedBuilder()
    {
        Title = "Test",
        Description = "Test Desc",
        Color = Discord.Color.DarkBlue,
        Footer = new EmbedFooterBuilder()
        {
            Text = "Test Foot"
        },
        ImageUrl = "attachment://test.png",

    }.Build(),
    components: new ComponentBuilder()
        .WithButton("Modify - Keep Image", "keep-image")
        .WithButton("Modify - Remove Image", "remove-image", ButtonStyle.Secondary)
        .Build(),
    ephemeral: true).ConfigureAwait(false);
}

[ComponentInteraction("remove-image")]
public async Task UpdateEmbedImage()
{
    await Context.Interaction.DeferAsync(true)
        .ConfigureAwait(false);

    var origInteraction = await Context.Interaction.GetOriginalResponseAsync()
        .ConfigureAwait(false);

    await origInteraction.ModifyAsync(x =>
    {
        x.Attachments = null;
        x.Embed = new EmbedBuilder()
        {
            Title = "Remove Image Modify Test",
            Description = "Modify Test",
            Color = Discord.Color.DarkBlue,
        }.Build();
    }).ConfigureAwait(false);
}

[ComponentInteraction("keep-image")]
public async Task KeepEmbedImage()
{
    await Context.Interaction.DeferAsync(true)
        .ConfigureAwait(false);

    var origInteraction = await Context.Interaction.GetOriginalResponseAsync().ConfigureAwait(false);

    await origInteraction.ModifyAsync(x =>
    {
        x.Embed = new EmbedBuilder()
        {
            Title = "Keep Images Modify Test",
            Description = "Modify Test",
        }.Build();
    }).ConfigureAwait(false);
}

@Almighty-Shogun
Copy link
Contributor

How about settings Attachments to null instead of passing an empty array? 🤔

@DeclanFrampton
Copy link
Contributor Author

How about settings Attachments to null instead of passing an empty array? 🤔

Just reminded me that I actually tried that but instead of investigating the issue with that I got side tracked. Will push another commit later for this, the issue exists in the else statement of checking if attachments are specified, if they are not we enter the else scope and pass the attachments and convert to array without checking if the value is null.

@DeclanFrampton DeclanFrampton changed the title ModifyAsync Attatchment Bug Fix #2236 ModifyAsync Attachments Bug Fix #2236 Jul 28, 2023
Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
@Misha-133 Misha-133 merged commit 0c12079 into discord-net:dev Aug 10, 2023
3 checks passed
supitsmike added a commit to supitsmike/Discord.Net that referenced this pull request Aug 21, 2023
Misha-133 added a commit that referenced this pull request Sep 2, 2023
* Similar to PR #2742 but this now fixes the UpdateAsync attachments bug.

* Update functions.

* `Update` => `UpdateAsync`

---------

Co-authored-by: Misha133 <mihagribkov133@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants