Regression: specifying null embed to SendMessageAsync causes NRE
Summary
We are gathered here today to blame @VelvetThePanda for yet another regression in the library, introduced as part of #921: specifying embed: null to any DiscordChannel.SendMessageAsync() causes an NRE.
Details
The problem can be seen here: https://github.com/DSharpPlus/DSharpPlus/blob/68c19e5eef3f35da83f75664f822dc7ff5946730/DSharpPlus/Entities/Channel/DiscordChannel.cs#L251
Specifically, the new[] {embed} part. There are 2 violations here:
- Style violation: all inline array and object initializers are supposed to have space inside (thus, correct formatting is
new[] { embed }. - More to the point: the embed isn't checked for null at all. So this passes an array of
{ null }to DiscordApiClient, which then throws from here:
https://github.com/DSharpPlus/DSharpPlus/blob/68c19e5eef3f35da83f75664f822dc7ff5946730/DSharpPlus/Net/Rest/DiscordApiClient.cs#L1036
Reason is simple,embedisnull, and thus cannot be deref'd.
I can verify that it worked before the change was made for 2 reasons:
- I wrote that code originally.
- I tested it before deploying, and it worked.
Steps to reproduce
Run this (where chn is an instance of a text DiscordChannel in any guild):
await chn.SendMessageAsync(content: "blame velvet", embed: null);
Notes
This can be worked around by null coalescing, but it should instead be done via proper nullcheck. If the embed is null, the array allocation should be prevented.