This resolves #987 Previous behavior was that even if `null` was passed for an embed in UploadFileAsync, the Embed property on UploadFileArgs was still specified - this meant we were always sending a payload_json. If a payload_json is specified, it seems like Discord will only read from the payload_json, and will ignore properties set outside of it. To prevent unnecessary code duplication, this commit always specifies parameters in the payload_json, and also will only include the embed if one was actually specified with real data (not null).tags/2.0
@@ -30,28 +30,24 @@ namespace Discord.API.Rest | |||||
{ | { | ||||
var d = new Dictionary<string, object>(); | var d = new Dictionary<string, object>(); | ||||
d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat")); | d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat")); | ||||
var payload = new Dictionary<string, object>(); | |||||
if (Content.IsSpecified) | if (Content.IsSpecified) | ||||
d["content"] = Content.Value; | |||||
payload["content"] = Content.Value; | |||||
if (IsTTS.IsSpecified) | if (IsTTS.IsSpecified) | ||||
d["tts"] = IsTTS.Value.ToString(); | |||||
payload["tts"] = IsTTS.Value.ToString(); | |||||
if (Nonce.IsSpecified) | if (Nonce.IsSpecified) | ||||
d["nonce"] = Nonce.Value; | |||||
payload["nonce"] = Nonce.Value; | |||||
if (Embed.IsSpecified) | if (Embed.IsSpecified) | ||||
{ | |||||
var payload = new StringBuilder(); | |||||
using (var text = new StringWriter(payload)) | |||||
using (var writer = new JsonTextWriter(text)) | |||||
{ | |||||
var map = new Dictionary<string, object>() | |||||
{ | |||||
["embed"] = Embed.Value, | |||||
}; | |||||
_serializer.Serialize(writer, map); | |||||
} | |||||
d["payload_json"] = payload.ToString(); | |||||
} | |||||
payload["embed"] = Embed.Value; | |||||
var json = new StringBuilder(); | |||||
using (var text = new StringWriter(json)) | |||||
using (var writer = new JsonTextWriter(text)) | |||||
_serializer.Serialize(writer, payload); | |||||
d["payload_json"] = json.ToString(); | |||||
return d; | return d; | ||||
} | } | ||||
} | } | ||||
@@ -180,7 +180,7 @@ namespace Discord.Rest | |||||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client, | public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client, | ||||
Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options) | Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options) | ||||
{ | { | ||||
var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() }; | |||||
var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, Embed = embed != null ? embed.ToModel() : Optional<API.Embed>.Unspecified }; | |||||
var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false); | var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false); | ||||
return RestUserMessage.Create(client, channel, client.CurrentUser, model); | return RestUserMessage.Create(client, channel, client.CurrentUser, model); | ||||
} | } | ||||