Implement multi-file upload to webhooks (#1942)
This commit is contained in:
@@ -11,9 +11,8 @@ namespace Discord.API.Rest
|
||||
{
|
||||
private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
|
||||
|
||||
public Stream File { get; }
|
||||
public FileAttachment[] Files { get; }
|
||||
|
||||
public Optional<string> Filename { get; set; }
|
||||
public Optional<string> Content { get; set; }
|
||||
public Optional<string> Nonce { get; set; }
|
||||
public Optional<bool> IsTTS { get; set; }
|
||||
@@ -21,22 +20,16 @@ namespace Discord.API.Rest
|
||||
public Optional<string> AvatarUrl { get; set; }
|
||||
public Optional<Embed[]> Embeds { get; set; }
|
||||
public Optional<AllowedMentions> AllowedMentions { get; set; }
|
||||
public Optional<ActionRowComponent[]> MessageComponents { get; set; }
|
||||
|
||||
public bool IsSpoiler { get; set; } = false;
|
||||
|
||||
public UploadWebhookFileParams(Stream file)
|
||||
public UploadWebhookFileParams(params FileAttachment[] files)
|
||||
{
|
||||
File = file;
|
||||
Files = files;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, object> ToDictionary()
|
||||
{
|
||||
var d = new Dictionary<string, object>();
|
||||
var filename = Filename.GetValueOrDefault("unknown.dat");
|
||||
if (IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix))
|
||||
filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix);
|
||||
|
||||
d["file"] = new MultipartFile(File, filename);
|
||||
|
||||
var payload = new Dictionary<string, object>();
|
||||
if (Content.IsSpecified)
|
||||
@@ -49,11 +42,34 @@ namespace Discord.API.Rest
|
||||
payload["username"] = Username.Value;
|
||||
if (AvatarUrl.IsSpecified)
|
||||
payload["avatar_url"] = AvatarUrl.Value;
|
||||
if (MessageComponents.IsSpecified)
|
||||
payload["components"] = MessageComponents.Value;
|
||||
if (Embeds.IsSpecified)
|
||||
payload["embeds"] = Embeds.Value;
|
||||
if (AllowedMentions.IsSpecified)
|
||||
payload["allowed_mentions"] = AllowedMentions.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
|
||||
});
|
||||
}
|
||||
|
||||
payload["attachments"] = attachments;
|
||||
|
||||
var json = new StringBuilder();
|
||||
using (var text = new StringWriter(json))
|
||||
using (var writer = new JsonTextWriter(text))
|
||||
|
||||
@@ -123,14 +123,35 @@ namespace Discord.Webhook
|
||||
/// <returns> Returns the ID of the created message. </returns>
|
||||
public Task<ulong> SendFileAsync(string filePath, string text, bool isTTS = false,
|
||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null)
|
||||
=> WebhookClientHelper.SendFileAsync(this, filePath, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler);
|
||||
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null,
|
||||
MessageComponent components = null)
|
||||
=> WebhookClientHelper.SendFileAsync(this, filePath, text, isTTS, embeds, username, avatarUrl,
|
||||
allowedMentions, options, isSpoiler, components);
|
||||
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
||||
/// <returns> Returns the ID of the created message. </returns>
|
||||
public Task<ulong> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false,
|
||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null)
|
||||
=> WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler);
|
||||
RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null,
|
||||
MessageComponent components = null)
|
||||
=> WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username,
|
||||
avatarUrl, allowedMentions, options, isSpoiler, components);
|
||||
|
||||
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
||||
/// <returns> Returns the ID of the created message. </returns>
|
||||
public Task<ulong> SendFileAsync(FileAttachment attachment, string text, bool isTTS = false,
|
||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||
RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null)
|
||||
=> WebhookClientHelper.SendFileAsync(this, attachment, text, isTTS, embeds, username,
|
||||
avatarUrl, allowedMentions, components, options);
|
||||
|
||||
/// <summary> Sends a message to the channel for this webhook with an attachment. </summary>
|
||||
/// <returns> Returns the ID of the created message. </returns>
|
||||
public Task<ulong> SendFilesAsync(IEnumerable<FileAttachment> attachments, string text, bool isTTS = false,
|
||||
IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null,
|
||||
RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null)
|
||||
=> WebhookClientHelper.SendFilesAsync(this, attachments, text, isTTS, embeds, username, avatarUrl,
|
||||
allowedMentions, components, options);
|
||||
|
||||
|
||||
/// <summary> Modifies the properties of this webhook. </summary>
|
||||
public Task ModifyWebhookAsync(Action<WebhookProperties> func, RequestOptions options = null)
|
||||
|
||||
@@ -97,24 +97,51 @@ namespace Discord.Webhook
|
||||
await client.ApiClient.DeleteWebhookMessageAsync(client.Webhook.Id, messageId, options).ConfigureAwait(false);
|
||||
}
|
||||
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
|
||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
|
||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler, MessageComponent components)
|
||||
{
|
||||
string filename = Path.GetFileName(filePath);
|
||||
using (var file = File.OpenRead(filePath))
|
||||
return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler).ConfigureAwait(false);
|
||||
return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, allowedMentions, options, isSpoiler, components).ConfigureAwait(false);
|
||||
}
|
||||
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
|
||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
|
||||
public static Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS,
|
||||
IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler,
|
||||
MessageComponent components)
|
||||
=> SendFileAsync(client, new FileAttachment(stream, filename, isSpoiler: isSpoiler), text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options);
|
||||
|
||||
public static Task<ulong> SendFileAsync(DiscordWebhookClient client, FileAttachment attachment, string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, MessageComponent components, RequestOptions options)
|
||||
=> SendFilesAsync(client, new FileAttachment[] { attachment }, text, isTTS, embeds, username, avatarUrl, allowedMentions, components, options);
|
||||
|
||||
public static async Task<ulong> SendFilesAsync(DiscordWebhookClient client,
|
||||
IEnumerable<FileAttachment> attachments, string text, bool isTTS, IEnumerable<Embed> embeds, string username, string avatarUrl, AllowedMentions allowedMentions, MessageComponent components, RequestOptions options)
|
||||
{
|
||||
var args = new UploadWebhookFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler };
|
||||
if (username != null)
|
||||
args.Username = username;
|
||||
if (avatarUrl != null)
|
||||
args.AvatarUrl = avatarUrl;
|
||||
if (embeds != null)
|
||||
args.Embeds = embeds.Select(x => x.ToModel()).ToArray();
|
||||
if(allowedMentions != null)
|
||||
args.AllowedMentions = allowedMentions.ToModel();
|
||||
embeds ??= Array.Empty<Embed>();
|
||||
|
||||
Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
|
||||
Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");
|
||||
Preconditions.AtMost(embeds.Count(), 10, nameof(embeds), "A max of 10 embeds are allowed.");
|
||||
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
|
||||
}
|
||||
|
||||
// check that user flag and user Id list are exclusive, same with role flag and role Id list
|
||||
if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
|
||||
{
|
||||
if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
|
||||
allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
|
||||
{
|
||||
throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
|
||||
}
|
||||
|
||||
if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
|
||||
allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
|
||||
{
|
||||
throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
|
||||
}
|
||||
}
|
||||
|
||||
var args = new UploadWebhookFileParams(attachments.ToArray()) {AvatarUrl = avatarUrl, Username = username, Content = text, IsTTS = isTTS, Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified };
|
||||
var msg = await client.ApiClient.UploadWebhookFileAsync(client.Webhook.Id, args, options).ConfigureAwait(false);
|
||||
return msg.Id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user