Files
Discord.Net/src/Discord.Net.Rest/API/Rest/UploadFileParams.cs
Mihail Gribkov ba78e0c1f0 [Feature] Components V2 (#3065)
* oh well. at least it runs

* well that's some breakings

* all the models (i think)

* barebones builder impl

* commit

* initial take on `WithX` component builder design

* actually usable stuff now

* few more changes

* fixes, changes, syntax sugar

* rework message flags + auto add `ComponentsV2`

* apparently it's also nullable

* fix build

* webhook component supports + builder validations + some xmldoc

* `ResolvedUnfurledMediaItem` + fix missing accessory on sections

* missing xmldoc

* fix modal + whatever change

* yeet unused dep
2025-04-26 23:40:03 +03:00

99 lines
3.8 KiB
C#

using Discord.Net.Converters;
using Discord.Net.Rest;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Discord.API.Rest
{
internal class UploadFileParams
{
private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
public FileAttachment[] Files { get; }
public Optional<string> Content { get; set; }
public Optional<string> Nonce { get; set; }
public Optional<bool> IsTTS { get; set; }
public Optional<Embed[]> Embeds { get; set; }
public Optional<AllowedMentions> AllowedMentions { get; set; }
public Optional<MessageReference> MessageReference { get; set; }
public Optional<IMessageComponent[]> MessageComponent { get; set; }
public Optional<MessageFlags?> Flags { get; set; }
public Optional<ulong[]> Stickers { get; set; }
public Optional<CreatePollParams> Poll { get; set; }
public UploadFileParams(params Discord.FileAttachment[] attachments)
{
Files = attachments;
}
public IReadOnlyDictionary<string, object> ToDictionary()
{
var d = new Dictionary<string, object>();
if (Files.Any(x => x.Waveform is not null && x.DurationSeconds is not null))
Flags = Flags.GetValueOrDefault(MessageFlags.None) | MessageFlags.VoiceMessage;
var payload = new Dictionary<string, object>();
if (Content.IsSpecified)
payload["content"] = Content.Value;
if (IsTTS.IsSpecified)
payload["tts"] = IsTTS.Value;
if (Nonce.IsSpecified)
payload["nonce"] = Nonce.Value;
if (Embeds.IsSpecified)
payload["embeds"] = Embeds.Value;
if (AllowedMentions.IsSpecified)
payload["allowed_mentions"] = AllowedMentions.Value;
if (MessageComponent.IsSpecified)
payload["components"] = MessageComponent.Value;
if (MessageReference.IsSpecified)
payload["message_reference"] = MessageReference.Value;
if (Stickers.IsSpecified)
payload["sticker_ids"] = Stickers.Value;
if (Flags.IsSpecified)
payload["flags"] = Flags.Value;
if (Poll.IsSpecified)
payload["poll"] = Poll.Value;
List<object> attachments = new();
for (int n = 0; n != Files.Length; n++)
{
var attachment = Files[n];
var filename = attachment.FileName ?? "unknown.dat";
if (attachment.IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix))
filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix);
d[$"files[{n}]"] = new MultipartFile(attachment.Stream, filename);
attachments.Add(new
{
id = (ulong)n,
filename = filename,
description = attachment.Description ?? Optional<string>.Unspecified,
duration_secs = attachment.DurationSeconds ?? Optional<double>.Unspecified,
waveform = attachment.Waveform is null
? Optional<string>.Unspecified
: Convert.ToBase64String(attachment.Waveform)
});
}
payload["attachments"] = attachments;
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;
}
}
}