Implement multi-file upload to webhooks (#1942)
This commit is contained in:
@@ -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