Skip to content

Commit

Permalink
[Feature] Refactor SendMessageAsync & SendFile(s)Async methods & …
Browse files Browse the repository at this point in the history
…modify webhook message attachments (#2609)

* initial commit

* resolve merge conficts

* code review suggestions
  • Loading branch information
Misha-133 authored Mar 31, 2023
1 parent c950106 commit 898ee56
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 290 deletions.
11 changes: 6 additions & 5 deletions src/Discord.Net.Core/Entities/Channels/IMessageChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public interface IMessageChannel : IChannel
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
/// <param name="stickers">A collection of stickers to send with the message.</param>
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/>
/// and <see cref="MessageFlags.SuppressNotification"/> is permitted.</param>
/// <returns>
/// A task that represents an asynchronous send operation for delivering the message. The task result
/// contains the sent message.
Expand Down Expand Up @@ -72,7 +73,7 @@ public interface IMessageChannel : IChannel
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
/// <param name="stickers">A collection of stickers to send with the file.</param>
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.SuppressNotification"/> is permitted.</param>
/// <returns>
/// A task that represents an asynchronous send operation for delivering the message. The task result
/// contains the sent message.
Expand Down Expand Up @@ -110,7 +111,7 @@ public interface IMessageChannel : IChannel
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
/// <param name="stickers">A collection of stickers to send with the file.</param>
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.SuppressNotification"/> is permitted.</param>
/// <returns>
/// A task that represents an asynchronous send operation for delivering the message. The task result
/// contains the sent message.
Expand Down Expand Up @@ -140,7 +141,7 @@ public interface IMessageChannel : IChannel
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
/// <param name="stickers">A collection of stickers to send with the file.</param>
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.SuppressNotification"/> is permitted.</param>
/// <returns>
/// A task that represents an asynchronous send operation for delivering the message. The task result
/// contains the sent message.
Expand Down Expand Up @@ -170,7 +171,7 @@ public interface IMessageChannel : IChannel
/// <param name="components">The message components to be included with this message. Used for interactions.</param>
/// <param name="stickers">A collection of stickers to send with the file.</param>
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> is permitted.</param>
/// <param name="flags">A message flag to be applied to the sent message, only <see cref="MessageFlags.SuppressEmbeds"/> and <see cref="MessageFlags.SuppressNotification"/> is permitted.</param>
/// <returns>
/// A task that represents an asynchronous send operation for delivering the message. The task result
/// contains the sent message.
Expand Down
46 changes: 46 additions & 0 deletions src/Discord.Net.Core/Utils/Preconditions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Discord
{
Expand Down Expand Up @@ -69,6 +71,50 @@ public static void NotNullOrWhitespace(Optional<string> obj, string name, string

private static ArgumentException CreateNotEmptyException(string name, string msg)
=> new ArgumentException(message: msg ?? "Argument cannot be blank.", paramName: name);

#endregion

#region Message Validation

public static void WebhookMessageAtLeastOneOf(string text = null, MessageComponent components = null, ICollection<IEmbed> embeds = null,
IEnumerable<FileAttachment> attachments = null)
{
if (!string.IsNullOrEmpty(text))
return;

if (components != null && components.Components.Count != 0)
return;

if (attachments != null && attachments.Count() != 0)
return;

if (embeds != null && embeds.Count != 0)
return;

throw new ArgumentException($"At least one of 'Content', 'Embeds', 'Components' or 'Attachments' must be specified.");
}

public static void MessageAtLeastOneOf(string text = null, MessageComponent components = null, ICollection<IEmbed> embeds = null,
ICollection<ISticker> stickers = null, IEnumerable<FileAttachment> attachments = null)
{
if (!string.IsNullOrEmpty(text))
return;

if (components != null && components.Components.Count != 0)
return;

if (stickers != null && stickers.Count != 0)
return;

if (attachments != null && attachments.Count() != 0)
return;

if (embeds != null && embeds.Count != 0)
return;

throw new ArgumentException($"At least one of 'Content', 'Embeds', 'Components', 'Stickers' or 'Attachments' must be specified.");
}

#endregion

#region Numerics
Expand Down
7 changes: 1 addition & 6 deletions src/Discord.Net.Rest/API/Rest/CreateMessageParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Discord.API.Rest
internal class CreateMessageParams
{
[JsonProperty("content")]
public string Content { get; }
public Optional<string> Content { get; set; }

[JsonProperty("nonce")]
public Optional<string> Nonce { get; set; }
Expand All @@ -31,10 +31,5 @@ internal class CreateMessageParams

[JsonProperty("flags")]
public Optional<MessageFlags> Flags { get; set; }

public CreateMessageParams(string content)
{
Content = content;
}
}
}
Loading

0 comments on commit 898ee56

Please sign in to comment.