| @@ -12,9 +12,8 @@ namespace Discord.API.Rest | |||||
| { | { | ||||
| private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() }; | private static JsonSerializer _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() }; | ||||
| public Stream File { get; } | |||||
| public IEnumerable<KeyValuePair<string, Stream>> Files { get; } | |||||
| public Optional<string> Filename { get; set; } | |||||
| public Optional<string> Content { get; set; } | public Optional<string> Content { get; set; } | ||||
| public Optional<string> Nonce { get; set; } | public Optional<string> Nonce { get; set; } | ||||
| public Optional<bool> IsTTS { get; set; } | public Optional<bool> IsTTS { get; set; } | ||||
| @@ -24,19 +23,23 @@ namespace Discord.API.Rest | |||||
| public bool IsSpoiler { get; set; } = false; | public bool IsSpoiler { get; set; } = false; | ||||
| public UploadWebhookFileParams(Stream file) | |||||
| public UploadWebhookFileParams(IEnumerable<KeyValuePair<string, Stream>> files) | |||||
| { | { | ||||
| File = file; | |||||
| Files = files; | |||||
| } | } | ||||
| public IReadOnlyDictionary<string, object> ToDictionary() | public IReadOnlyDictionary<string, object> ToDictionary() | ||||
| { | { | ||||
| var d = new Dictionary<string, object>(); | 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); | |||||
| 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<string, object>(); | var payload = new Dictionary<string, object>(); | ||||
| if (Content.IsSpecified) | if (Content.IsSpecified) | ||||
| @@ -102,6 +102,17 @@ namespace Discord.Webhook | |||||
| IEnumerable<Embed> embeds = null, string username = null, string avatarUrl = null, RequestOptions options = null, bool isSpoiler = false) | IEnumerable<Embed> 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); | => WebhookClientHelper.SendFileAsync(this, stream, filename, text, isTTS, embeds, username, avatarUrl, options, isSpoiler); | ||||
| /// <summary> Sends a message to the channel for this webhook with multiple attachments. </summary> | |||||
| /// <returns> Returns the ID of the created message. </returns> | |||||
| public Task<ulong> SendFileAsync(IEnumerable<string> filePaths, string text, bool isTTS = false, | |||||
| IEnumerable<Embed> 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); | |||||
| /// <summary> Sends a message to the channel for this webhook with multiple attachments. </summary> | |||||
| /// <returns> Returns the ID of the created message. </returns> | |||||
| public Task<ulong> SendFileAsync(IEnumerable<KeyValuePair<string, Stream>> streams, string text, bool isTTS = false, | |||||
| IEnumerable<Embed> 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); | |||||
| /// <summary> Modifies the properties of this webhook. </summary> | /// <summary> Modifies the properties of this webhook. </summary> | ||||
| public Task ModifyWebhookAsync(Action<WebhookProperties> func, RequestOptions options = null) | public Task ModifyWebhookAsync(Action<WebhookProperties> func, RequestOptions options = null) | ||||
| => Webhook.ModifyAsync(func, options); | => Webhook.ModifyAsync(func, options); | ||||
| @@ -41,10 +41,24 @@ namespace Discord.Webhook | |||||
| using (var file = File.OpenRead(filePath)) | using (var file = File.OpenRead(filePath)) | ||||
| return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options, isSpoiler).ConfigureAwait(false); | return await SendFileAsync(client, file, filename, text, isTTS, embeds, username, avatarUrl, options, isSpoiler).ConfigureAwait(false); | ||||
| } | } | ||||
| public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, IEnumerable<string> filePaths, string text, bool isTTS, | |||||
| IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler) | |||||
| { | |||||
| var files = filePaths.Select(x => new KeyValuePair<string, Stream>(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<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS, | public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, Stream stream, string filename, string text, bool isTTS, | ||||
| IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler) | IEnumerable<Embed> 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<string, Stream>(filename, stream) }, text, isTTS, embeds, username, avatarUrl, options, isSpoiler).ConfigureAwait(false); | |||||
| } | |||||
| public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, IEnumerable<KeyValuePair<string, Stream>> files, string text, bool isTTS, | |||||
| IEnumerable<Embed> embeds, string username, string avatarUrl, RequestOptions options, bool isSpoiler) | |||||
| { | |||||
| var args = new UploadWebhookFileParams(files) { Content = text, IsTTS = isTTS, IsSpoiler = isSpoiler }; | |||||
| if (username != null) | if (username != null) | ||||
| args.Username = username; | args.Username = username; | ||||
| if (avatarUrl != null) | if (avatarUrl != null) | ||||