feature: Webhook message edit & delete functionality (#1753)

* feature: Webhook message edit & delete functionality

* PR fixes: Rename Edit* to Modify*; Add more detailed docstrings; Small validation fixes

* Fix spacing around docstrings

* Make ModifyWebhookMessageParams.Content Optional<string>

* Change the Webhook message edit functionality to use a object delegate method instead providing the all parameters

Co-authored-by: Desmont <desmont@users.noreply.github.com>
This commit is contained in:
Desmont
2021-04-28 15:15:16 +02:00
committed by GitHub
parent 91a906397a
commit f67cd8ea55
5 changed files with 161 additions and 1 deletions

View File

@@ -91,6 +91,35 @@ namespace Discord.Webhook
string username = null, string avatarUrl = null, RequestOptions options = null, AllowedMentions allowedMentions = null)
=> WebhookClientHelper.SendMessageAsync(this, text, isTTS, embeds, username, avatarUrl, allowedMentions, options);
/// <summary>
/// Modifies a message posted using this webhook.
/// </summary>
/// <remarks>
/// This method can only modify messages that were sent using the same webhook.
/// </remarks>
/// <param name="messageId">ID of the modified message.</param>
/// <param name="func">A delegate containing the properties to modify the message with.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
public Task ModifyMessageAsync(ulong messageId, Action<WebhookMessageProperties> func, RequestOptions options = null)
=> WebhookClientHelper.ModifyMessageAsync(this, messageId, func, options);
/// <summary>
/// Deletes a message posted using this webhook.
/// </summary>
/// <remarks>
/// This method can only delete messages that were sent using the same webhook.
/// </remarks>
/// <param name="messageId">ID of the deleted message.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous deletion operation.
/// </returns>
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
=> WebhookClientHelper.DeleteMessageAsync(this, messageId, 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> SendFileAsync(string filePath, string text, bool isTTS = false,

View File

@@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace Discord.Webhook
{
/// <summary>
/// Properties that are used to modify an Webhook message with the specified changes.
/// </summary>
public class WebhookMessageProperties
{
/// <summary>
/// Gets or sets the content of the message.
/// </summary>
/// <remarks>
/// This must be less than the constant defined by <see cref="DiscordConfig.MaxMessageSize"/>.
/// </remarks>
public Optional<string> Content { get; set; }
/// <summary>
/// Gets or sets the embed array that the message should display.
/// </summary>
public Optional<IEnumerable<Embed>> Embeds { get; set; }
/// <summary>
/// Gets or sets the allowed mentions of the message.
/// </summary>
public Optional<AllowedMentions> AllowedMentions { get; set; }
}
}

View File

@@ -36,7 +36,59 @@ namespace Discord.Webhook
var model = await client.ApiClient.CreateWebhookMessageAsync(client.Webhook.Id, args, options: options).ConfigureAwait(false);
return model.Id;
}
public static async Task<ulong> SendFileAsync(DiscordWebhookClient client, string filePath, string text, bool isTTS,
public static async Task ModifyMessageAsync(DiscordWebhookClient client, ulong messageId,
Action<WebhookMessageProperties> func, RequestOptions options)
{
var args = new WebhookMessageProperties();
func(args);
if (args.AllowedMentions.IsSpecified)
{
var allowedMentions = args.AllowedMentions.Value;
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.");
// check that user flag and user Id list are exclusive, same with role flag and role Id list
if (allowedMentions?.AllowedTypes != null)
{
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 apiArgs = new ModifyWebhookMessageParams
{
Content = args.Content.IsSpecified ? args.Content.Value : Optional.Create<string>(),
Embeds =
args.Embeds.IsSpecified
? args.Embeds.Value.Select(embed => embed.ToModel()).ToArray()
: Optional.Create<API.Embed[]>(),
AllowedMentions = args.AllowedMentions.IsSpecified
? args.AllowedMentions.Value.ToModel()
: Optional.Create<API.AllowedMentions>()
};
await client.ApiClient.ModifyWebhookMessageAsync(client.Webhook.Id, messageId, apiArgs, options)
.ConfigureAwait(false);
}
public static async Task DeleteMessageAsync(DiscordWebhookClient client, ulong messageId, RequestOptions options)
{
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)
{
string filename = Path.GetFileName(filePath);