fix: UploadWebhookFileParams: Move most things into a json_payload (#1186)

This commit is contained in:
OatmealDome
2018-11-04 17:43:36 -05:00
committed by Christopher F
parent 8e6e0e9ad2
commit c1d5152212

View File

@@ -1,12 +1,17 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text;
using Discord.Net.Converters;
using Discord.Net.Rest; using Discord.Net.Rest;
using Newtonsoft.Json;
namespace Discord.API.Rest namespace Discord.API.Rest
{ {
internal class UploadWebhookFileParams internal class UploadWebhookFileParams
{ {
private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
public Stream File { get; } public Stream File { get; }
public Optional<string> Filename { get; set; } public Optional<string> Filename { get; set; }
@@ -27,18 +32,27 @@ 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 (Username.IsSpecified) if (Username.IsSpecified)
d["username"] = Username.Value; payload["username"] = Username.Value;
if (AvatarUrl.IsSpecified) if (AvatarUrl.IsSpecified)
d["avatar_url"] = AvatarUrl.Value; payload["avatar_url"] = AvatarUrl.Value;
if (Embeds.IsSpecified) if (Embeds.IsSpecified)
d["embeds"] = Embeds.Value; payload["embeds"] = Embeds.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;
} }
} }