Visitar URL original
Fix #1325 - WebhookBuilder Throws Bad Request by waffle-lord · Pull Request #1363 · DSharpPlus/DSharpPlus · GitHub
Skip to content

Fix #1325 - WebhookBuilder Throws Bad Request - #1363

Merged
OoLunar merged 3 commits into
DSharpPlus:masterfrom
waffle-lord:master
Aug 5, 2022
Merged

OoLunar merged 3 commits into
DSharpPlus:masterfrom
waffle-lord:master

Conversation

@waffle-lord

Copy link
Copy Markdown
Contributor

Summary

Fixes #1325 by Changing RestWebhookPayloads.Mentions to use DiscordMentions instead of IEnumerable<IMention>

Details

the api expects "users":[], "roles":[], or "parse":[] for the allowed_mentions field. The DiscordJson serializer is populating this field with "id": 123456... instead which is causing an exception when sending the request. This inadvertently keeps the interaction that created the request alive, seemingly forever.

Notes

I haven't tested this to any real extent and know very little of this lib... so you know... *shrug

Comment thread DSharpPlus/Net/Rest/DiscordApiClient.cs Outdated
Content = builder.Content,
Embeds = builder.Embeds,
Mentions = builder.Mentions,
Mentions = new DiscordMentions(builder.Mentions ?? Mentions.All, builder.Mentions?.Any() ?? false),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentions should still be null if the builder's Mentions are null.
Not only would this otherwise be inconsistent with previous behavior, but editing a webhook without specifying mentions on the builder would now set it to have everyone in the message mentioned.
(Note the NullValueHandling on the payload property.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truly a flashback of #1054.

Comment thread DSharpPlus/Net/Rest/DiscordApiClient.cs Outdated
{
builder.Validate(true);

var mentions = builder.Mentions != null ? new DiscordMentions(builder.Mentions, builder.Mentions?.Any() ?? false) : null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builder.Mentions?.Any() ?? false should just be builder.Mentions.Any() since null is already checked, though it shouldn't have a functional impact.
Otherwise, LGTM.

@DPlayer234 DPlayer234 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@OoLunar

OoLunar commented Aug 4, 2022

Copy link
Copy Markdown
Contributor

Tested?

@waffle-lord

Copy link
Copy Markdown
Contributor Author

I've only tested it with InteractionContext.EditResponseAsync, since it uses the webhook builder.

I'm not familiar with how I go about proving this... here's a picture?

image

My garbage test code if you want it

public class SlashMentionTest : ApplicationCommandModule
    {
        [SlashCommand("mention_test", "Test the webhookbuilder mentions")]
        public async Task MentionTestAsync(InteractionContext context, [Option("Array", "Use mention array option")] bool array = false)
        {
            // your own role ID here
            DiscordRole testRole = context.Guild.GetRole(1004897882253570158);

            var mentions = new IMention[] { new UserMention(context.User), new UserMention(context.Client.CurrentUser), new RoleMention(testRole) };

            // how do you get a mention back from IMention anyway? wish I knew right now ...
            string message = array ? $"Hi {context.User.Mention} {context.Client.CurrentUser.Mention} {testRole.Mention}" : $"Hi {context.User.Mention}";

            await context.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);

            // doin stuff
            Thread.Sleep(2000);

            var webhookBuilder = new DiscordWebhookBuilder().WithContent(message);

            if(array)
            {
                // use mentions array
                webhookBuilder.AddMentions(mentions);
            }
            else
            {
                // use single mention
                webhookBuilder.AddMention(new UserMention(context.User));
            }

            await context.EditResponseAsync(webhookBuilder);
        }
    }

@OoLunar

OoLunar commented Aug 5, 2022

Copy link
Copy Markdown
Contributor

Yeah that works. Thanks for your contributions!

@OoLunar
OoLunar merged commit 168dbf9 into DSharpPlus:master Aug 5, 2022
VelvetToroyashi pushed a commit that referenced this pull request Jan 28, 2023
* Change GuildMemberUpdatedEventArgs to contain before and after DiscordMember objects (#1359)

* Fix #1325 - WebhookBuilder Throws Bad Request (#1363)

* use DiscordMentions on RestWebhookPayloads

* keep null value when null

* remove null check from discordmentions arg

* Implemented PATCH /guilds/:guild_id/roles (tested) (#1311)

* Fix compile errors caused by #1333

* Update hosting.md

* Remove unused DSharpPlus.MSTest folder

* Cache threads upon thread creation (#1366)

* categorization for the cnext default help command (#1371)

* Fix memberBefore missing roles in GuildMemberUpdateEventArgs when not… (#1364)

* Fix memberBefore missing roles in GuildMemberUpdateEventArgs when not in cache

* Update cached guild member in OnGuildMemberUpdateEventAsync internal event handler

* Update DiscordClient.Dispatch.cs

Co-authored-by: Lunar Starstrum <lunar@forsaken-borders.net>

* Ported CommandsNext cooldown attribute to Slash Commands (#1375)

* Ported CommandsNext cooldown attribute to Slash Commands; Fixed compiler warnings and suggestions

* Still untested; Attempted to resolve @Erisa's review

* Made all exceptions inherit from ApplicationCommandExecutionChecksFailedException (previously Exception)

* Start on converters

* Added custom converter support:

- No, I did not implement the checks in the converters. I felt that should be done in a separate area of code
- Yes, I did break my Enter, Backspace, D and Apostrophe keys because of this PR. Don't ask how.

* Fixed startup errors; Added DiscordMessageArgumentConverter; Added (un)register converter methods (untested).

* Fix registering converters

* (untested) Add number converters; Fix enums not registering choices correctly

* Add DiscordMember converter; Switch from ArgumentException to new MissingArgumentConverterException

* Actually fix Nullable and update TimeSpan converter to match with CNext

* Add params/array support

* Boundry checks

* [ci-skip] Resolve naming errors

* "One less variable allocation"

* Remove unsupport NETSTANDARD1_3 support; Un-Emziware those variable names

* Remove unused property

* Extra parameter limit attribute check

* Remove UnregisterConverter(s) methods

* Changed BCL types to use framework names

* Pass InnerException to prevent passing TargetInvocationException

* Fix enum support (again)

---------

Co-authored-by: Bob Loblaw <razvanmandache@gmail.com>
Co-authored-by: waffle-lord <76401815+waffle-lord@users.noreply.github.com>
Co-authored-by: Iris Féanorá <61018569+ExaInsanity@users.noreply.github.com>
OoLunar pushed a commit that referenced this pull request Oct 3, 2024
* use DiscordMentions on RestWebhookPayloads

* keep null value when null

* remove null check from discordmentions arg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using Mention in the WebHookBuilder throws a bad request

4 participants