Skip to content

Commit 1fb62de

Browse files
Support Sending Message Flags (#2131)
* Add message flags * Add webhook message flags
1 parent b3370c3 commit 1fb62de

File tree

16 files changed

+515
-333
lines changed

16 files changed

+515
-333
lines changed

src/Discord.Net.Core/Entities/Channels/IMessageChannel.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public interface IMessageChannel : IChannel
3131
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
3232
/// <param name="stickers">A collection of stickers to send with the message.</param>
3333
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
34+
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
3435
/// <returns>
3536
/// A task that represents an asynchronous send operation for delivering the message. The task result
3637
/// contains the sent message.
3738
/// </returns>
38-
Task<IUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null);
39+
Task<IUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
3940
/// <summary>
4041
/// Sends a file to this message channel with an optional caption.
4142
/// </summary>
@@ -71,11 +72,12 @@ public interface IMessageChannel : IChannel
7172
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
7273
/// <param name="stickers">A collection of stickers to send with the file.</param>
7374
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
75+
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
7476
/// <returns>
7577
/// A task that represents an asynchronous send operation for delivering the message. The task result
7678
/// contains the sent message.
7779
/// </returns>
78-
Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null);
80+
Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
7981
/// <summary>
8082
/// Sends a file to this message channel with an optional caption.
8183
/// </summary>
@@ -108,11 +110,12 @@ public interface IMessageChannel : IChannel
108110
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
109111
/// <param name="stickers">A collection of stickers to send with the file.</param>
110112
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
113+
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
111114
/// <returns>
112115
/// A task that represents an asynchronous send operation for delivering the message. The task result
113116
/// contains the sent message.
114117
/// </returns>
115-
Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null);
118+
Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
116119
/// <summary>
117120
/// Sends a file to this message channel with an optional caption.
118121
/// </summary>
@@ -137,11 +140,12 @@ public interface IMessageChannel : IChannel
137140
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
138141
/// <param name="stickers">A collection of stickers to send with the file.</param>
139142
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
143+
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
140144
/// <returns>
141145
/// A task that represents an asynchronous send operation for delivering the message. The task result
142146
/// contains the sent message.
143147
/// </returns>
144-
Task<IUserMessage> SendFileAsync(FileAttachment attachment, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null);
148+
Task<IUserMessage> SendFileAsync(FileAttachment attachment, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
145149
/// <summary>
146150
/// Sends a collection of files to this message channel.
147151
/// </summary>
@@ -166,11 +170,12 @@ public interface IMessageChannel : IChannel
166170
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
167171
/// <param name="stickers">A collection of stickers to send with the file.</param>
168172
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
173+
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
169174
/// <returns>
170175
/// A task that represents an asynchronous send operation for delivering the message. The task result
171176
/// contains the sent message.
172177
/// </returns>
173-
Task<IUserMessage> SendFilesAsync(IEnumerable<FileAttachment> attachments, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null);
178+
Task<IUserMessage> SendFilesAsync(IEnumerable<FileAttachment> attachments, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None);
174179

175180
/// <summary>
176181
/// Gets a message from this message channel.

src/Discord.Net.Rest/API/Rest/CreateMessageParams.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ internal class CreateMessageParams
2828

2929
[JsonProperty("sticker_ids")]
3030
public Optional<ulong[]> Stickers { get; set; }
31+
32+
[JsonProperty("flags")]
33+
public Optional<MessageFlags> Flags { get; set; }
3134

3235
public CreateMessageParams(string content)
3336
{

src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ public static async Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsyn
266266
}
267267

268268
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
269+
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
269270
public static async Task<RestUserMessage> SendMessageAsync(IMessageChannel channel, BaseDiscordClient client,
270-
string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
271+
string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference,
272+
MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds, MessageFlags flags)
271273
{
272274
embeds ??= Array.Empty<Embed>();
273275
if (embed != null)
@@ -298,14 +300,19 @@ public static async Task<RestUserMessage> SendMessageAsync(IMessageChannel chann
298300
Preconditions.AtMost(stickers.Length, 3, nameof(stickers), "A max of 3 stickers are allowed.");
299301
}
300302

303+
304+
if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
305+
throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
306+
301307
var args = new CreateMessageParams(text)
302308
{
303309
IsTTS = isTTS,
304310
Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified,
305311
AllowedMentions = allowedMentions?.ToModel(),
306312
MessageReference = messageReference?.ToModel(),
307313
Components = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified,
308-
Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified
314+
Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified,
315+
Flags = flags
309316
};
310317
var model = await client.ApiClient.CreateMessageAsync(channel.Id, args, options).ConfigureAwait(false);
311318
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
@@ -335,29 +342,44 @@ public static async Task<RestUserMessage> SendMessageAsync(IMessageChannel chann
335342
/// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception>
336343
/// <exception cref="IOException">An I/O error occurred while opening the file.</exception>
337344
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
345+
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
338346
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
339-
string filePath, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, bool isSpoiler, Embed[] embeds)
347+
string filePath, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
348+
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
349+
bool isSpoiler, Embed[] embeds, MessageFlags flags = MessageFlags.None)
340350
{
341351
string filename = Path.GetFileName(filePath);
342352
using (var file = File.OpenRead(filePath))
343-
return await SendFileAsync(channel, client, file, filename, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, isSpoiler, embeds).ConfigureAwait(false);
353+
return await SendFileAsync(channel, client, file, filename, text, isTTS, embed, allowedMentions,
354+
messageReference, components, stickers, options, isSpoiler, embeds, flags).ConfigureAwait(false);
344355
}
345356

346357
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
358+
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
347359
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
348-
Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, bool isSpoiler, Embed[] embeds)
360+
Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
361+
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
362+
bool isSpoiler, Embed[] embeds, MessageFlags flags = MessageFlags.None)
349363
{
350364
using (var file = new FileAttachment(stream, filename, isSpoiler: isSpoiler))
351-
return await SendFileAsync(channel, client, file, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, embeds).ConfigureAwait(false);
365+
return await SendFileAsync(channel, client, file, text, isTTS, embed, allowedMentions, messageReference,
366+
components, stickers, options, embeds, flags).ConfigureAwait(false);
352367
}
353368

354369
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
370+
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
355371
public static Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
356-
FileAttachment attachment, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
357-
=> SendFilesAsync(channel, client, new[] { attachment }, text, isTTS, embed, allowedMentions, messageReference, components, stickers, options, embeds);
372+
FileAttachment attachment, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
373+
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
374+
Embed[] embeds, MessageFlags flags = MessageFlags.None)
375+
=> SendFilesAsync(channel, client, new[] { attachment }, text, isTTS, embed, allowedMentions, messageReference,
376+
components, stickers, options, embeds, flags);
358377

378+
/// <exception cref="ArgumentException">The only valid <see cref="MessageFlags"/> are <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.None"/>.</exception>
359379
public static async Task<RestUserMessage> SendFilesAsync(IMessageChannel channel, BaseDiscordClient client,
360-
IEnumerable<FileAttachment> attachments, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options, Embed[] embeds)
380+
IEnumerable<FileAttachment> attachments, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions,
381+
MessageReference messageReference, MessageComponent components, ISticker[] stickers, RequestOptions options,
382+
Embed[] embeds, MessageFlags flags)
361383
{
362384
embeds ??= Array.Empty<Embed>();
363385
if (embed != null)
@@ -366,7 +388,7 @@ public static async Task<RestUserMessage> SendFilesAsync(IMessageChannel channel
366388
Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
367389
Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");
368390
Preconditions.AtMost(embeds.Length, 10, nameof(embeds), "A max of 10 embeds are allowed.");
369-
391+
370392
foreach(var attachment in attachments)
371393
{
372394
Preconditions.NotNullOrEmpty(attachment.FileName, nameof(attachment.FileName), "File Name must not be empty or null");
@@ -398,12 +420,26 @@ public static async Task<RestUserMessage> SendFilesAsync(IMessageChannel channel
398420
}
399421
}
400422

423+
if (flags is not MessageFlags.None and not MessageFlags.SuppressEmbeds)
424+
throw new ArgumentException("The only valid MessageFlags are SuppressEmbeds and none.", nameof(flags));
425+
401426
if (stickers != null)
402427
{
403428
Preconditions.AtMost(stickers.Length, 3, nameof(stickers), "A max of 3 stickers are allowed.");
404429
}
405430

406-
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 };
431+
var args = new UploadFileParams(attachments.ToArray())
432+
{
433+
Content = text,
434+
IsTTS = isTTS,
435+
Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified,
436+
AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified,
437+
MessageReference = messageReference?.ToModel() ?? Optional<API.MessageReference>.Unspecified,
438+
MessageComponent = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified,
439+
Stickers = stickers?.Any() ?? false ? stickers.Select(x => x.Id).ToArray() : Optional<ulong[]>.Unspecified,
440+
Flags = flags
441+
};
442+
407443
var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);
408444
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
409445
}

0 commit comments

Comments
 (0)