Support Sending Message Flags (#2131)
* Add message flags * Add webhook message flags
This commit is contained in:
committed by
GitHub
parent
b3370c33e2
commit
1fb62de14b
@@ -266,8 +266,10 @@ namespace Discord.Rest
|
||||
}
|
||||
|
||||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
|
||||
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
|
||||
public static async Task<RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
|
||||
string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
|
||||
string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference,
|
||||
MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds, MessageFlags flags)
|
||||
{
|
||||
embeds ??= Array.Empty<Embed>();
|
||||
if (embed != null)
|
||||
@@ -298,6 +300,10 @@ namespace Discord.Rest
|
||||
Preconditions.AtMost(stickers.Length, 3, nameof(stickers), "A max of 3 stickers are allowed.");
|
||||
}
|
||||
|
||||
|
||||
if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
|
||||
throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
|
||||
|
||||
var args = new CreateMessageParams(text)
|
||||
{
|
||||
IsTTS = isTTS,
|
||||
@@ -305,7 +311,8 @@ namespace Discord.Rest
|
||||
AllowedMentions = allowedMentions?.ToModel(),
|
||||
MessageReference = messageReference?.ToModel(),
|
||||
Components = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified,
|
||||
Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified
|
||||
Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified,
|
||||
Flags = flags
|
||||
};
|
||||
var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);
|
||||
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
|
||||
@@ -335,29 +342,44 @@ namespace Discord.Rest
|
||||
/// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception>
|
||||
/// <exception cref="IOException">An I/O error occurred while opening the file.</exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
|
||||
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
|
||||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
|
||||
string filePath, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, bool isSpoiler, Embed[] embeds)
|
||||
string filePath, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
|
||||
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
|
||||
bool isSpoiler, Embed[] embeds, MessageFlags flags = MessageFlags.None)
|
||||
{
|
||||
string filename = Path.GetFileName(filePath);
|
||||
using (var file = File.OpenRead(filePath))
|
||||
return await SendFileAsync(channel, client, file, filename, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, isSpoiler, embeds).ConfigureAwait(false);
|
||||
return await SendFileAsync(channel, client, file, filename, text, isTTS, embed, allowedMentions,
|
||||
messageReference, components, stickers, options, isSpoiler, embeds, flags).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
|
||||
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
|
||||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
|
||||
Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, bool isSpoiler, Embed[] embeds)
|
||||
Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
|
||||
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
|
||||
bool isSpoiler, Embed[] embeds, MessageFlags flags = MessageFlags.None)
|
||||
{
|
||||
using (var file = new FileAttachment(stream, filename, isSpoiler: isSpoiler))
|
||||
return await SendFileAsync(channel, client, file, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, embeds).ConfigureAwait(false);
|
||||
return await SendFileAsync(channel, client, file, text, isTTS, embed, allowedMentions, messageReference,
|
||||
components, stickers, options, embeds, flags).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
|
||||
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
|
||||
public static Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
|
||||
FileAttachment attachment, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
|
||||
=> SendFilesAsync(channel, client, new[] { attachment }, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, embeds);
|
||||
FileAttachment attachment, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
|
||||
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
|
||||
Embed[] embeds, MessageFlags flags = MessageFlags.None)
|
||||
=> SendFilesAsync(channel, client, new[] { attachment }, text, isTTS, embed, allowedMentions, messageReference,
|
||||
components, stickers, options, embeds, flags);
|
||||
|
||||
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
|
||||
public static async Task<RestUserMessage> SendFilesAsync(IMessageChannel channel, BaseDiscordClient client,
|
||||
IEnumerable<FileAttachment> attachments, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
|
||||
IEnumerable<FileAttachment> attachments, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
|
||||
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
|
||||
Embed[] embeds, MessageFlags flags)
|
||||
{
|
||||
embeds ??= Array.Empty<Embed>();
|
||||
if (embed != null)
|
||||
@@ -366,7 +388,7 @@ namespace Discord.Rest
|
||||
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.Length, 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");
|
||||
@@ -398,12 +420,26 @@ namespace Discord.Rest
|
||||
}
|
||||
}
|
||||
|
||||
if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
|
||||
throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
|
||||
|
||||
if (stickers != null)
|
||||
{
|
||||
Preconditions.AtMost(stickers.Length, 3, nameof(stickers), "A max of 3 stickers are allowed.");
|
||||
}
|
||||
|
||||
var args = new UploadFileParams(attachments.ToArray()) { Content = text, IsTTS = isTTS, Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, MessageReference = messageReference?.ToModel() ?? Optional<API.MessageReference>.Unspecified, MessageComponent = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified, Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified };
|
||||
var args = new UploadFileParams(attachments.ToArray())
|
||||
{
|
||||
Content = text,
|
||||
IsTTS = isTTS,
|
||||
Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified,
|
||||
AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified,
|
||||
MessageReference = messageReference?.ToModel() ?? Optional<API.MessageReference>.Unspecified,
|
||||
MessageComponent = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified,
|
||||
Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified,
|
||||
Flags = flags
|
||||
};
|
||||
|
||||
var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);
|
||||
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user