diff --git a/src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs b/src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs index 26153c21b..c7693de26 100644 --- a/src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs +++ b/src/Discord.Net.Rest/API/Rest/UploadWebhookFileParams.cs @@ -12,9 +12,8 @@ namespace Discord.API.Rest { private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() }; - public Stream File { get; } + public IEnumerable> Files { get; } - public Optional Filename { get; set; } public Optional Content { get; set; } public Optional Nonce { get; set; } public Optional IsTTS { get; set; } @@ -24,19 +23,23 @@ namespace Discord.API.Rest public bool IsSpoiler { get; set; } = false; - public UploadWebhookFileParams(Stream file) + public UploadWebhookFileParams(IEnumerable> files) { - File = file; + Files = files; } public IReadOnlyDictionary ToDictionary() { var d = new Dictionary(); - var filename = Filename.GetValueOrDefault("unknown.dat"); - if (IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) - filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); - d["file"] = new MultipartFile(File, filename); + int i = 0; + foreach (var file in Files) + { + var filename = file.Key ?? "unknown.dat"; + if (IsSpoiler && !filename.StartsWith(AttachmentExtensions.SpoilerPrefix)) + filename = filename.Insert(0, AttachmentExtensions.SpoilerPrefix); + d[$"file{i++}"] = new MultipartFile(file.Value, filename); + } var payload = new Dictionary(); if (Content.IsSpecified) diff --git a/src/Discord.Net.Webhook/DiscordWebhookClient.cs b/src/Discord.Net.Webhook/DiscordWebhookClient.cs index 353345ded..0bbb73eae 100644 --- a/src/Discord.Net.Webhook/DiscordWebhookClient.cs +++ b/src/Discord.Net.Webhook/DiscordWebhookClient.cs @@ -102,6 +102,17 @@ namespace Discord.Webhook IEnumerable embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null, bool isSpoiler = false) => WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username, avatarUrl, options, isSpoiler); + /// Sends a message to the channel for this webhook with multiple attachments. + /// Returns the ID of the created message. + public Task SendFileAsync(IEnumerable filePaths, string text, bool isTTS = false, + IEnumerable embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null, bool isSpoiler = false) + => WebhookClientHelper.SendFileAsync(this, filePaths, text, isTTS, embeds, username, avatarUrl, options, isSpoiler); + /// Sends a message to the channel for this webhook with multiple attachments. + /// Returns the ID of the created message. + public Task SendFileAsync(IEnumerable> streams, string text, bool isTTS = false, + IEnumerable embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null, bool isSpoiler = false) + => WebhookClientHelper.SendFileAsync(this, streams, text, isTTS, embeds, username, avatarUrl, options, isSpoiler); + /// Modifies the properties of this webhook. public Task ModifyWebhookAsync(Action func, RequestOptions options = null) => Webhook.ModifyAsync(func, options); diff --git a/src/Discord.Net.Webhook/WebhookClientHelper.cs b/src/Discord.Net.Webhook/WebhookClientHelper.cs index 311d58bda..846940a73 100644 --- a/src/Discord.Net.Webhook/WebhookClientHelper.cs +++ b/src/Discord.Net.Webhook/WebhookClientHelper.cs @@ -41,10 +41,24 @@ namespace Discord.Webhook using (var file = File.OpenRead(filePath)) return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options, isSpoiler).ConfigureAwait(false); } + public static async Task SendFileAsync(DiscordWebhookClient client, IEnumerable filePaths, string text, bool isTTS, + IEnumerable embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler) + { + var files = filePaths.Select(x => new KeyValuePair(Path.GetFileName(x), File.OpenRead(x))); + var id = await SendFileAsync(client, files, text, isTTS, embeds, username, avatarUrl, options, isSpoiler).ConfigureAwait(false); + foreach (var file in files) + file.Value.Dispose(); + return id; + } public static async Task SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS, IEnumerable embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler) { - var args = new UploadWebhookFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler }; + return await SendFileAsync(client, new[] { new KeyValuePair(filename, stream) }, text, isTTS, embeds, username, avatarUrl, options, isSpoiler).ConfigureAwait(false); + } + public static async Task SendFileAsync(DiscordWebhookClient client, IEnumerable> files, string text, bool isTTS, + IEnumerable embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler) + { + var args = new UploadWebhookFileParams(files) { Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler }; if (username != null) args.Username = username; if (avatarUrl != null)