Browse Source

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).
tags/2.0
Christopher F 7 years ago
parent
commit
ac5ecd365d
2 changed files with 15 additions and 19 deletions
  1. +14
    -18
      src/Discord.Net.Rest/API/Rest/UploadFileParams.cs
  2. +1
    -1
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs

+ 14
- 18
src/Discord.Net.Rest/API/Rest/UploadFileParams.cs 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,
};

_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;
}
}


+ 1
- 1
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs 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);
}


Loading…
Cancel
Save