Include the content in payload_json for file uploads

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).
This commit is contained in:
Christopher F
2018-03-19 16:29:51 -04:00
parent bfaa6fc97a
commit ac5ecd365d
2 changed files with 13 additions and 17 deletions

View File

@@ -30,28 +30,24 @@ namespace Discord.API.Rest
{
var d = new Dictionary<string, object>();
d["file"] = new MultipartFile(File, Filename.GetValueOrDefault("unknown.dat"));
var payload = new Dictionary<string, object>();
if (Content.IsSpecified)
d["content"] = Content.Value;
payload["content"] = Content.Value;
if (IsTTS.IsSpecified)
d["tts"] = IsTTS.Value.ToString();
payload["tts"] = IsTTS.Value.ToString();
if (Nonce.IsSpecified)
d["nonce"] = Nonce.Value;
payload["nonce"] = Nonce.Value;
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,
};
payload["embed"] = Embed.Value;
_serializer.Serialize(writer, map);
}
d["payload_json"] = payload.ToString();
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;
}
}

View File

@@ -180,7 +180,7 @@ namespace Discord.Rest
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
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);
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
}