You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadWebhookFileParams.cs 3.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using Discord.Net.Converters;
  5. using Discord.Net.Rest;
  6. using Newtonsoft.Json;
  7. namespace Discord.API.Rest
  8. {
  9. internal class UploadWebhookFileParams
  10. {
  11. private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
  12. public FileAttachment[] Files { get; }
  13. public Optional<string> Content { get; set; }
  14. public Optional<string> Nonce { get; set; }
  15. public Optional<bool> IsTTS { get; set; }
  16. public Optional<string> Username { get; set; }
  17. public Optional<string> AvatarUrl { get; set; }
  18. public Optional<Embed[]> Embeds { get; set; }
  19. public Optional<AllowedMentions> AllowedMentions { get; set; }
  20. public Optional<ActionRowComponent[]> MessageComponents { get; set; }
  21. public UploadWebhookFileParams(params FileAttachment[] files)
  22. {
  23. Files = files;
  24. }
  25. public IReadOnlyDictionary<string, object> ToDictionary()
  26. {
  27. var d = new Dictionary<string, object>();
  28. var payload = new Dictionary<string, object>();
  29. if (Content.IsSpecified)
  30. payload["content"] = Content.Value;
  31. if (IsTTS.IsSpecified)
  32. payload["tts"] = IsTTS.Value.ToString();
  33. if (Nonce.IsSpecified)
  34. payload["nonce"] = Nonce.Value;
  35. if (Username.IsSpecified)
  36. payload["username"] = Username.Value;
  37. if (AvatarUrl.IsSpecified)
  38. payload["avatar_url"] = AvatarUrl.Value;
  39. if (MessageComponents.IsSpecified)
  40. payload["components"] = MessageComponents.Value;
  41. if (Embeds.IsSpecified)
  42. payload["embeds"] = Embeds.Value;
  43. if (AllowedMentions.IsSpecified)
  44. payload["allowed_mentions"] = AllowedMentions.Value;
  45. List<object> attachments = new();
  46. for (int n = 0; n != Files.Length; n++)
  47. {
  48. var attachment = Files[n];
  49. var filename = attachment.FileName ?? "unknown.dat";
  50. if (attachment.IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix))
  51. filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix);
  52. d[$"files[{n}]"] = new MultipartFile(attachment.Stream, filename);
  53. attachments.Add(new
  54. {
  55. id = (ulong)n,
  56. filename = filename,
  57. description = attachment.Description ?? Optional<string>.Unspecified
  58. });
  59. }
  60. payload["attachments"] = attachments;
  61. var json = new StringBuilder();
  62. using (var text = new StringWriter(json))
  63. using (var writer = new JsonTextWriter(text))
  64. _serializer.Serialize(writer, payload);
  65. d["payload_json"] = json.ToString();
  66. return d;
  67. }
  68. }
  69. }