|
|
@@ -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; |
|
|
|
} |
|
|
|